Advertisement

crash when calling engine->Release()

Started by November 26, 2011 10:06 PM
4 comments, last by WitchLord 13 years, 1 month ago
hi again,

so i studied the game tutorial a little bit and tried to adjust it a little bit for my needs. the problem is, that within the scriptmgr the application always crashes when it comes to the engine->Release() line and i have no idea why.

http://codepad.org/4Z5ovVZl <-- that's the main. i try to create the scriptmgr, create an object(in my case a unit), call the onattack method of the script and then release everything.
http://codepad.org/3aV40mGt <-- that's the uni object (similiar to the gameobj of the game sample). i don't get it, why the sample uses a gameobjlink? what are the advantages of using an extra link class?
http://codepad.org/w0XuBzXy <-- that's the scriptmgr class. line 33 causes the crash. it's nearly a 1:1 copy of the game sample scriptmgr.
http://codepad.org/5xoPJMsG <-- the sample scriptfile Frosch.as

the debugmsgs are:
finished successfully (line228 of scriptmgr) which tells me, that it loads the file successfully and the script file itself is without errors. then it prints 1(line28) and 2 (line31) so the cleanup of the contexts and scriptunits succeeds to. after that the application crashes and due to the debugger it crashes when calling the engine->Release method (line 33).

what am i doing wrong again? :( please help me.

best regards

edit:
debugged some more, seems like it only crashes when it successfully loads the scriptfile. when i test it with an invalid path, everything is finde. additionaly i got this error msg when it comes to the release line:
"Type asCScriptEngine has no component named _contextsType asCScriptEngine has no component named _contexts"

edit2:
k it seems like it DOES need this gameobjlink class in order to work correctly. if i add a similar class to my application, everything works just finde. i don't quite get it why, but i hope i will soon get it why ... hm :)
From the code comments on the link object:

[source]
// The CGameObjLink is a thin wrapper on the game object
// that provides a weak link that can be severed at any
// time by the application without having to worry about
// who might be holding a reference to the object
[/source]

It is not necessary for you to use it in your own design. I use it because I wanted to be able to easily destroy the game object, but without having to worry about where it might still be referenced. It is not something I do just because of the scripting, as even without the scripting I would still use the same for references coming from other parts of the engine, e.g. physics system, gui, other game objects, etc.




The crash you were experiencing is most likely due to some object being destroyed incorrectly, most likely you're calling Release() on an object pointer when you shouldn't have. I would need a little more detail on the crash to tell, for example the stack trace when the crash happened, in order to tell.

I have absolutely no idea what the error message "Type asCScriptEngine has no component named _contextsType" means. The library doesn't have any variable named _contextsType. My guess is that this is something from the C++ run time library. What compiler are you using? I found reports of similar error messages on Google, and it seems to come from gdb, are you debugging with gdb? If gdb gives this error it probably means the memory got corrupted somehow.

So introducing the link class, got rid of the crash? This reinforces the theory that the problem originated from a problem with the reference counting.



To find the root cause of the problem I would try to validate the reference count of the objects. Both your own Unit object and the script object. One or the other is most likely destroyed to early. I didn't find any obvious error from looking at your code though, so you'll need to debug it.



Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
the thing i don't like with this approach, that you have to define each method 2 times. once for the "baseclass" and once for the wrapperclass which is imo not a good way but maybe i'm just 2 unexperienced in the world of scripting and this solution is for whatever reasons one of the best ones :)

compiler is mingw and i'm using the eclipse indigo IDE for developing. and yes, introduction a linkclass solved the problem.

regards
simon
I'm not saying it is the best, it is just my way of doing it. :)

Anyway, it is is definitely not necessary to use it. I suggest you try without it again and then pay attention on the refererence counters.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

found the error and everything is working now without the additional wrapper class. however i don't really understand why it was a problem, so maybe you can explain it to me?

first of all the current code:

http://codepad.org/dKvHxfwz <-- unit.cpp
http://codepad.org/I6IGz7x1 <-- AScriptMgr.cpp
http://codepad.org/UTsDNAPG <-- main.cpp
http://codepad.org/QTUSFExK <-- Frosch.as

when i removed line 89 of AScriptMgr (obj->AddRef()) the problem was solved. but as far as i understood it, the addref is necesarry to not remove the reference instantly after the function has finished? and furthermore, why do i need this addref, when i use a wrapperclass and when i don't use it i have to remove it oO

this was the output of the program:


u1 created
u2 created
Unit1 AddRef(2)
Unit1 AddRef(3)
Unit1 AddRef(4)
Unit1Release(3)
Unit1Release(2)
call on attack u1 -> u2
Unit2 AddRef(2)
Unit1 AddRef(3)
Unit1Release(2)
Unit1 hat Unit2 getoetet!

Unit2Release(1)
Unit1 100 : 100
1
Unit2 0 : 0
0
Release u1
Unit1Release(1)
Release u2
Unit2Release(0)
Unit2Delete
Release engine
Unit1Release(0)
Unit1Delete
Finished
The AddRef is needed. Without it the script engine doesn't know you hold a reference to the object. In this case the object is not destroyed right away when the context is unprepared, because the garbage collector is not run immediately. Probably you would see a problem if you had created a second script object for Unit2.

You don't get any crash as it is now, because the destructor in Unit never releases the script object. The destructor in Unit will only be called when _refCount is 0, so the while( _refCount > 0 ) will never be true.

I modified my own game sample yesterday to remove the use of GameObjLink. I suggest you download the latest revision from the svn to take a look at how it works now.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement