Advertisement

Garbage Collection Segfault

Started by October 14, 2010 09:56 AM
2 comments, last by WitchLord 14 years, 1 month ago
The last problem I submitted was on 64 Linux, now I have one on Win32 (at least confirmed, need to double check on Linux now).

Basically I have a garbage collected class that inherits from this helper class

class asGarbageCollected{public:    virtual ~asGarbageCollected(){}    void SetGCFlag()    {        ref_count |= 0x80000000;    }    bool GetGCFlag()    {        return (ref_count & 0x80000000) ? true : false;    }    int GetRefCount()    {        return (ref_count & 0x7FFFFFFF);    }    virtual void EnumReferences()=0;    void AddRef()    {        ref_count = (ref_count&0x7FFFFFFF) + 1;    }    void Release()    {        ref_count &= 0x7FFFFFFF;        if( --ref_count == 0 )            delete this;    }    virtual void ReleaseAllReferences()=0;protected:    int ref_count;    asIScriptEngine* engine; // set by the inherited class.};


However, when the garbage collector runs its cycle, it segfaults. Here's the callstack as reported by GDB
#0 00000000    0x00000051 in ??() (??:??)#1 0044AC24    asCScriptEngine::CallObjectMethod(this=0x2bdf9e8, obj=0x2c03378, param=0x2bdf9e8, i=0x2bf2058, s=0x2bf20a8) (../../source/as_scriptengine.cpp:3179)#2 0044AAE9    asCScriptEngine::CallObjectMethod(this=0x2bdf9e8, obj=0x2c03378, param=0x2bdf9e8, func=152) (../../source/as_scriptengine.cpp:3159)#3 0046021E    asCGarbageCollector::IdentifyGarbageWithCyclicRefs(this=0x2be0070) (../../source/as_gc.cpp:355)#4 0045FBEB    asCGarbageCollector::GarbageCollect(this=0x2be0070, flags=1) (../../source/as_gc.cpp:90)#5 0044AFA3    asCScriptEngine::GarbageCollect(this=0x2bdf9e8, flags=1) (../../source/as_scriptengine.cpp:3284)#6 0041F4BF    mengi::Stage::Run(this=0x2bdf878) (C:/Documents and Settings/orm/Desktop/mengi/engine/src/Stage.cpp:498)#7 0041E4F3    mengi::Stage::Update(this=0x2bdf878, t=2.50237131) (C:/Documents and Settings/orm/Desktop/mengi/engine/src/Stage.cpp:371)#8 0040FA86    mengi::Engine::Run(this=0x2ae5e48) (C:/Documents and Settings/orm/Desktop/mengi/engine/src/Engine.cpp:325)#9 004352BE    main(argc=1, argv=0x2ae4588) (C:/Documents and Settings/orm/Desktop/mengi/engine/src/main.cpp:7)


I'll post more info from valgrind when i get back to my linux machine, because I doubt this is limited to just windows (building from svn)
Does the class implementation inherit from anything else besides the asGarbageCollected helper class?

How is the object pointer informed to the GC? Make sure you don't have any invalid casts going, especially if you have multiple or virtual inheritances.

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
AS a matter of fact, the class does implement a good number of interfaces as well as inheriting from another class used for numerical identification. How does that affect the garbage collector?
It doesn't affect the GC itself. But with multiple inheritances the 'this' pointer changes depending on which base class you're refering to, so you will need to be careful with any casts of the object pointer. If you have an incorrect cast somewhere you may actually pass an incorrect 'this' pointer to the GC, and this might be causing the errors you're seeing.

Casts to and from void* is especially easy to mix up. You must make sure to always cast from void* to the same type it was before the pointer was cast to void*.

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