Game receiving signal: "EXC_BAD_ACCESS"
by Ricky Hopper · in iTorque 2D · 01/04/2010 (10:36 pm) · 10 replies
I've recently discovered that in my game, if you play multiple levels, the game will crash. I'm not entirely sure what the cause is, but the debugger says the program received signal "EXC_BAD_ACCESS", and highlights this line of code in t2dSceneWindow.cc:
I do not know what the highlighted code has to do with this (some of my objects do use mouse events, though), but I believe some memory leakage may be at fault. Might this be the case? How can I check for this/fix it? I'm new to Torque, so this may be common knowledge that I am not yet informed of...
lastObject->onMouseEvent( "onMouseLeave" , event, worldMousePoint );
I do not know what the highlighted code has to do with this (some of my objects do use mouse events, though), but I believe some memory leakage may be at fault. Might this be the case? How can I check for this/fix it? I'm new to Torque, so this may be common knowledge that I am not yet informed of...
About the author
Recent Threads
#2
1) Calls a delete command for all active objects
2) Checks to make sure there are no objects left in the scene
3) Ends the level?
I don't think endLevel checks to make sure the objects are deleted before it ends, even with many onLevelEnded delete functions for object classes.
01/09/2010 (3:46 pm)
Ok thanks, I think this is a memory leak. Would I be able to write a function that:1) Calls a delete command for all active objects
2) Checks to make sure there are no objects left in the scene
3) Ends the level?
I don't think endLevel checks to make sure the objects are deleted before it ends, even with many onLevelEnded delete functions for object classes.
#3
01/11/2010 (2:48 pm)
I would actually recommend never ending the level. I've found many problems in loading / unloading levels in iTGB- enough to make me no longer rely on it. What I would actually suggest doing is creating commands like you suggested, but then just repopulate the scene with the new objects rather than loading a new level. So load one master level when the game starts, then populate and depopulate it as needed. Otherwise you will probably come across some nasty crash bugs, especially if you are using behaviors.
#4
01/11/2010 (5:52 pm)
Interesting concept, but I was more asking how would I implement the function I described earlier? I'm learning Torquescript as I go, and don't know how to check to make sure there are no objects remaining (or how to delete the ones that are detected as still being there).
#5
To delete all objects:
Then to have a watcher function that checks and sees when it's done to continue...
01/11/2010 (6:11 pm)
Objects for a scene are stored in $lastLoadedScene, so you could just do this:To delete all objects:
for(%i = 0; %i < $lastLoadedScene.getCount(); %i++)
{
%localObj = $lastLoadedScene.getObject(%i);
if(%localObj.getName() $= %objName)
{
$lastLoadedScene.remove(%localObj);
%localObj.delete();
}
}Then to have a watcher function that checks and sees when it's done to continue...
function waitTilDone()
{
if($lastLoadedScene.getCount() == 0)
{
doSomething();
return;
}
else
schedule(500,0,waitTilDone);
}
#6
01/11/2010 (6:16 pm)
Thanks! And this would go right after I ended the scene, but before I loaded another?
#7
01/11/2010 (6:21 pm)
Again, I typically never unload and reload scenes as it's too problematic. But if you are going to go that route, then put this before you end the scene, as I believe that ending the scene will destroy $lastLoadedScene to replace it with another.
#8
01/12/2010 (5:40 pm)
Ok, I've tried implementing this code, and I noticed that it would only delete certain objects (text objects, and one of those still remains) and that waitTilDone would only run once even if the objects were not finished deleting (which they never were). It did not even get to your doSomething() command (and yes, I did replace that with my own code). Are you sure all the names of variables are correct? I do not know where you got the %objName variable from.
#9
Here is the corrected code:
Try THAT code, now. That should work.
(NOTE: Made one further edit. Didn't like how that code worked... this should be better.)
01/12/2010 (5:53 pm)
Woah! My apologies... I grabbed that code from another project I produced and looks like it needed a quick edit. :)Here is the corrected code:
for(%i = $lastLoadedScene.getCount(); %i > -1.0; %i--)
{
%localObj = $lastLoadedScene.getObject(%i);
$lastLoadedScene.remove(%localObj);
%localObj.delete();
}Try THAT code, now. That should work.
(NOTE: Made one further edit. Didn't like how that code worked... this should be better.)
#10
01/12/2010 (6:02 pm)
Awesome! The console complains about -1 not being an object, but that doesn't seem like a problem since loading and unloading levels works now. Thanks for the help!
Associate Dave Calabrese
Cerulean Games
Remember these simple rules when doing development, especially for mobile:
- If you don't need it right now, it shouldn't be in memory.
- Have safety code in your program that verifies that something has been removed from memory, or else you may get memory leaks in places you don't expect it.
- If you are loading or removing assets from memory, then make sure that your code pays attention to things being added / removed, and that the game does not attempt to continue on until it has finished processing the assets it is working with. Otherwise, you will most likely crash.
- Especially on the iPhone, don't just exec all your GUIs at load - all their graphics gets stored in memory! Instead, exec a GUI just before you .push it, and always .delete it when you're done. Also, since TGB allows you to have multiple objects of the same name, you may end up having multiple GUIs in memory, all with the same name. So instead of using the .delete command, actually use this code snipped:
function superDelete(%obj) { while( isObject(%obj) ) %obj.delete(); }