ent_remove(my) crash?

Posted By: Germanunkol

ent_remove(my) crash? - 03/03/12 13:49

Hello,

I've been spending the last 6 months on and off on bug fixing. I've boiled it down to a few, which I cannot fix.
They might both be connected with me doing something wrong with sys_malloc and sys_free commands, but I have no idea how to find this. Some of these issues I have posted before, and you offered me that I could send you the code and you would have a look at it. I did not do it at the time, because I had too many other things to finish before getting to these bugs. Does that offer still stand?
1)
Click to reveal.. (level_load crash)

Code:
posPredictDummy = NULL;
	ship = NULL;
	
	logNewMessage("loading level:",gameLog,OFF);
	logNewMessage(level, gameLog, OFF);
	
	sys_marker("ls0");
	level_load(level); //loads the level which has been loaded on the server
	sys_marker("ls1");
	camEnt = NULL;
	wait(1);
	sc_shadows_sunLookAt = ent_create(NULL,nullvector,NULL);		//important: if missing, shadeC will move random objects next round.

	wait(3); //wait until the level is loaded
	sys_marker("ls2");
	if(disconnectedFromServer) return;


Here, I sometimes get a "crash in level_load 'ls0'". As I have no idea what exactly level_load does, I have no clue how to fix this.

2)
The second one is a crash in ent_remove. This happens in 3 (rather unrelated) functions, seemingly at random.
First example (crashes in se6):
Click to reveal..
Code:
void shieldEffectEnt()
{
	var randID = random(9999);
	logNewValue("shieldEffectEnt", randID, debugLog, OFF);
	logNewValue("my:", my, debugLog, OFF);
	sys_marker("se0");
	set(my,PASSABLE);
	vec_set(my.pan,vector(you.lastHitPan, you.lastHitTilt,0));
	my.flags2 |= UNTOUCHABLE;
	
	my_playsound(my,shieldHitOGG, volumeMain*volumeFX*500*soundBrightFlash);
	sys_marker("se1");
	my.parent = you;
	//sc_ent_shieldImpact(my, getShieldEffectColor(you.shieldEffectType), 15, 1);

	var timePassed = 0;
	while(my != NULL && you != NULL && timePassed < 8)
	{
		sys_marker("se3");
		timePassed += time_frame;
		vec_set(my.x, you.x);
		sys_marker("se4");
		wait(1);
	}
	sys_marker("se2");
	logNewValue("shieldEffectEnt", randID, debugLog, OFF);
	logNewValue("my:", my, debugLog, OFF);
	sys_marker("se5");	
	if(my) 
	{
		sys_marker("se6");
		ent_remove(my);
	}
	sys_marker("se8");
}


Second example of ent_remove crash (crashes in w31):
Click to reveal..
Code:
weaponArray[userID*7 + you.upgradeType - 1] = NULL;
			sys_marker("w29");
			if(snd_playing(lightningGunSound[userID])) snd_stop(lightningGunSound[userID]);
			sys_marker("w30");
			if(snd_playing(machineGunSound[userID])) snd_stop(machineGunSound[userID]);
			sys_marker("w31");
			if(you) ent_remove(you);
			sys_marker("w32");
		}


The third problem is that the debugger crashes on me with an error message "out of memory". I have 4 gigabytes of RAM (can use about 3.1 of that, since my PC is a 32 bit machine) and my game takes a max of 1, usually. This one we have talked about before, and you asked me to send you my code.

P.S. I use Version 7.86.6
Edit: Here's what I've tried regarding the ent_remove problem: http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=395538#Post395538
Posted By: FBL

Re: ent_remove(my) crash? - 03/03/12 14:04

Possibly stating the obvious, but anyway....

1) did you check the content of "level"? What was inside this string while the crash occured?
2) log the addresses where "me" and "you" are pointing to at that stage. I assume the pointers are not NULL while pointing at some entity which has already been removed. That would explain why the array access above (2nd example) does not yet crash, because you're not accessing some NULL pointer there.
Posted By: Germanunkol

Re: ent_remove(my) crash? - 03/03/12 14:28

1) I did check the content of level. It was valid, in fact, it was the exact same string as the previous level load function call got, around 5 minutes earlier.
2) Is also a valid point, but if the entity was removed, all of the functions which have it as a "my" pointer would have been stopped and the my pointer set to NULL, according to the manual and all other tests I have made.

I've included a link at the end of my post above to a post where we've already tried quite a few things regarding the ent_remove problem. I've done quite a bit of logging, and the only thing that I can think of now is that only part of the entity memory has been overwritten, because the entity does still exist.
Posted By: FBL

Re: ent_remove(my) crash? - 03/03/12 14:36

2) not necessarily. When I developed Four Little Warriors I had some similar problem where entity functions continued running even after entity removal. The problem was, that at some point in the code, the my pointer could get wrecked and then would point to another entity.
The looping function checking for my therefore continued and so to say was reattached to a different entity.
Posted By: Germanunkol

Re: ent_remove(my) crash? - 03/03/12 16:34

Checked that as well, though. I log the my pointer at the beginning of the function and just before the ent_remove(my) is called. In both cases, it holds exactly the same value...
Posted By: FBL

Re: ent_remove(my) crash? - 03/03/12 16:53

Guess I'm not of any help then.
Posted By: Germanunkol

Re: ent_remove(my) crash? - 03/03/12 18:25

Thanks for your ideas, either way!
Posted By: TechMuc

Re: ent_remove(my) crash? - 03/04/12 10:54

I have no idea if the CRT Functions work with 3D Gamestudio or only with Visual Studio, though there might be a small probability that they work :-) So maybe you could try:
_CrtSetDbgFlag(_CrtSetDebgFlag() | _CRTDBG_CHECK_ALWAYS_DF);
(anywhere at the beginning of your code). This will check EVERY following allocation / free instruction for heap corruption and will therefore identify any mallicious function call.
If this does not work, you could try _CrtCheckMemory(); which checks the validity of the heap (though this will probably not work either in this case laugh )

Sources: http://msdn.microsoft.com/en-us/library/e73x0s4b(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/5at7yxcs(v=vs.71).aspx

PS: I'm also very interested in debugging your code with my cool editor (though it's not in work anymore frown ), so you can send it to me (if you want to) and i can check if I can find the error for you.
PS:2 this will dramatically slow down the whole game, so if you use it, try to reproduce the crash and activate the "always check heap" flag only directly before (one frame before).
If the heap-checking is just to slow, use the CrtCheckMemory function call.
Posted By: Spirit

Re: ent_remove(my) crash? - 03/04/12 14:27

Is your level also crashing when its the first level in your game? If not, both problems likely are the same, because when you load a level, the old level is unloaded and its entities are removed before.
Posted By: Germanunkol

Re: ent_remove(my) crash? - 03/04/12 15:44

I am hoping it is the same problem, yes. The first level load never crashes, but future level load calls sometimes do. It's all very unregular, but I do agree, it might be the same problem.
Posted By: FBL

Re: ent_remove(my) crash? - 03/04/12 19:47

I'm remembering one thing now...
I had such a crash problem with decals and using level_free(). The terrain with decals attached was removed, but the decal function was still running and then crashed.
Adding a wait(1) before level_free() fixed the issue.

Are you doing anything like this somewhere?
Posted By: jcl

Re: ent_remove(my) crash? - 03/05/12 08:57

Simple errors, like freeing an already-freed entity, are normally caught by the engine and will not cause a crash. If the error is not obvious, it could be something more sinister, such as something overwriting some internal memory areas.

For finding the reason of such a problem, reduce your application step by step until the crash disappears. First remove all external plugins, then disable all functions that access internal memory structs, then remove all effects. If then the problem still is there, send me that project and I'll look into it. I'm meanwhile sort of an expert in finding hidden bugs in user projects.
Posted By: jcl

Re: ent_remove(my) crash? - 03/05/12 09:45

Originally Posted By: Firoball
Adding a wait(1) before level_free() fixed the issue.

Normally not, otherwise we'd named the function not "wait()", but "fixbug()". If a wait(1) changes the behavior of a bug and you don't know why, it can give a hint where to look for the bug. But it's normally not a fix.
Posted By: FBL

Re: ent_remove(my) crash? - 03/05/12 18:58

problem was that the terrain was removed from memory (level_free!) and the decal routine seems to lag one frame behind. Of course it accessed the non existant terrain then.

That was the only plausible reason I found. it is directly related to a decal on a terrain.

Code:
ent_purge(map__sLevel.entTerrain); /* remove decals */
		ptr_remove(map__sLevel.entTerrain);

		wait (2); /* decals seem to need time... */
		level_free();


Posted By: jcl

Re: ent_remove(my) crash? - 03/05/12 19:04

What is a decal routine?
Posted By: FBL

Re: ent_remove(my) crash? - 03/05/12 19:09

whatever the decal does during its lifespan.

partCursor = ent_decal(NULL, bmapTemp, 40, vTemp + vAngle);
partCursor->lifespan = 0.001; /* only 1 frame */

I'm spamming the terrain with decals in a while loop.
Posted By: jcl

Re: ent_remove(my) crash? - 03/06/12 09:23

The lifespan only tells the renderer whether this decal must be rendered in the current frame. Unlike particles, decals are not doing something, they are just a passive list of objects.

If you had some function in your project that crashes when a new level is loaded, putting a wait(1) before level_load would obviously not help - it would then only crash 1 frame later. But if you have a while loop that is generating decals, stop that loop 1 frame before level change. Otherwise, placing a decal on an invalid terrain pointer can indeed cause a crash.
Posted By: FBL

Re: ent_remove(my) crash? - 03/06/12 18:40

So stopping the function in the same frame before level_free() is not enough?
Well this tells then where the wait(1) comes from.

Without level_free() no wait(1) is needed.
© 2024 lite-C Forums