Hi,
I've defined a few custom structs for a game I'm working on, and one of them seems to be causing problems.
The problem occurs sporadically, but I've been able to trace it down to one line of my code.
Basically, the program crashes when I attempt to check if an entity pointer assigned to my struct is NULL. I do check if the struct pointer is NULL before I check the entity pointer, but that doesn't seem to prevent the errors.
My assumption is that the pointer to my struct is not NULL but also not valid.
What am I doing wrong?

The code section containing the error is:

Code:
if (inst == NULL) return;
	
	sys_marker("204");
	if (inst.linkedEnt != NULL) { //The game crashes here
		sys_marker(206); safe_remove(inst.linkedEnt);
		sys_marker(207); inst.linkedEnt = NULL;
	}



inst is a pointer to an Instance object, which is defined as follows:

Code:
//Instance struct: Used to keep track of all player-usable objects in the game
typedef struct Instance {
	ENTITY* linkedEnt;			//A linked entity for instances in the 3D space
	STRING* name;			//The visible name
	STRING* class;			//The instance type
	
	STRING* parameters[32];		//Parameter names
	int parTypes[32];			//Parameter types
	var numericPars[32];		//Any numeric parameters are saved here
	STRING* stringPars[32];		//Any string parameters are saved here
	VECTOR* vectorPars[32]; 		//Any vector parameters are saved here
	struct Instance* instPars[32];	//Any instance parameters are saved here
	char numPars;			//Total number of parameters
	char expanded;			//Children visible in the explorer?
	char selected;			//Is this instance selected in the explorer?
	
	struct Instance* parent;		//Pointer to the parent struct
	struct Instance* children[65535];	//Pointer array to any child instances
	unsigned int numChildren;		//Amount of children of this instance
	var luaState;			//Ability to assign a Lua script to any object
	var events[32];			//Pointer vars to events
	var id;				//ID assigned to the object
} Instance;



These objects are all stored in an array, and created through a function which are defined as follows:

Code:
Instance* gameInsts[65535];

function newInstance(STRING* class) {
	Instance* inst = malloc(sizeof(Instance));
	inst.class = str_create(class);
	inst.linkedEnt = NULL;
	
	unsigned int count = 0;
	for (count = 0; count < 65535; count ++) {
		if (gameInsts[count] == NULL) {
			gameInsts[count] = inst;
			inst.id = count;
			break;
		}
	}
	return inst;
}



Additionally, the gameInsts array is initialized by the following function when the game starts up:
Code:
function setGameInstsNULL() {
	unsigned int count = 0;
	for (count = 0; count < 65535; count ++) {
		gameInsts[count] = NULL;
	}
}



If you need any more information, let me know.

Last edited by TehV; 01/06/14 15:39.