Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (ChrstphFr, AndrewAMD), 868 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Working with structs: Checking for NULL pointer causes crash #435340
01/06/14 14:14
01/06/14 14:14
Joined: Mar 2010
Posts: 120
Switzerland
T
TehV Offline OP
Member
TehV  Offline OP
Member
T

Joined: Mar 2010
Posts: 120
Switzerland
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.
Re: Working with structs: Checking for NULL pointer causes crash [Re: TehV] #435348
01/06/14 16:19
01/06/14 16:19
Joined: Jul 2008
Posts: 2,107
Germany
rayp Offline

X
rayp  Offline

X

Joined: Jul 2008
Posts: 2,107
Germany
Does this change anything ?
Code:
if (inst) if (inst.linkedEnt != NULL)



cheers


Acknex umgibt uns...zwischen Dir, mir, dem Stein dort...
"Hey Griswold ... where u gonna put a tree that big ?"
1998 i married my loved wife ... Sheeva from Mortal Kombat, not Evil-Lyn as might have been expected
rayp.flags |= UNTOUCHABLE;
Re: Working with structs: Checking for NULL pointer causes crash [Re: rayp] #435364
01/06/14 18:52
01/06/14 18:52
Joined: Mar 2010
Posts: 120
Switzerland
T
TehV Offline OP
Member
TehV  Offline OP
Member
T

Joined: Mar 2010
Posts: 120
Switzerland
Doesn't that do the same as this?
Code:
if (inst == NULL) return;



Also, the problem persists with your solution.

Re: Working with structs: Checking for NULL pointer causes crash [Re: TehV] #435371
01/06/14 20:11
01/06/14 20:11
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
Well, if (inst.linkedEnt != NULL) crashes that means inst is a vagabonding pointer. You're of course right that the NULL check before that line is sufficient.

A remark concerning newInstance: When you use malloc you have to manually zero out the newly allocated memory. I'd suggest using sys_malloc instead which does this work for you already and uses the engines native memory managment...


Always learn from history, to be sure you make the same mistakes again...
Re: Working with structs: Checking for NULL pointer causes crash [Re: Uhrwerk] #435372
01/06/14 20:27
01/06/14 20:27
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Fyi, your struct is half an mb in size! That is incredibly huge, and I wouldn't necessarily go via sys_malloc() here (I don't know the implementation of it, and I don't know how well it handles allocations of that size. Memory managers are incredibly hard to write in a good way, and the OS system allocators have improved quite a lot in the recent years, in a way that I wouldn't count on a home brewed alternative unless I knew it was using something like tcmalloc internally).

In all seriousness though, you really ought to improve your struct and allocations. Your static allocations are way out of hand. Even if that's not the source of your error, it's going to be a problem at some point! (as Uhrwerk pointed out, it seems like you ended up either making inst a dangling pointer or forget to initialize it somewhere while dropping the other instance or...).


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1