Advertisement

Need help tracking memory leak

Started by March 04, 2013 01:47 PM
4 comments, last by WitchLord 11 years, 9 months ago

I cannot for the life of me find this memory leak. I keep getting this:

(0, 0) : ERR : GC cannot free an object of type 'RenderComponent', it is kept alive by the application

(0, 0) : ERR : GC cannot free an object of type '_builtin_objecttype_', it is kept alive by the application

(0, 0) : ERR : GC cannot free an object of type 'dictionary', it is kept alive by the application

(0, 0) : ERR : GC cannot free an object of type 'Sprite', it is kept alive by the application


I implement all of these functions:
void addRef();

void release();

int getRefCount();

void setGCFlag();

bool getGCFlag();

in the constructor i set the refCount to 1

and when I create a script object I notify the garbage collector like so:


SpriteScripted* ScriptObjectCreationCache::SSFactory() {
    SpriteScripted* obj = new SpriteScripted();
    
    if (spriteType == NULL)
        spriteType = engine->GetObjectTypeByName("Sprite");
    engine->NotifyGarbageCollectorOfNewObject(obj, spriteType);
    
    return obj;
}

this happens whenever I use that object type as a member property of a complete angelscript class (a class not declared in c++). However, i dont have a problem when I use a c++ defined class in a limited scope such as 'Texture' in setSpriteWithName:


(angelscript code; I dont have problems with Texture but I do have leaks with Sprite)

class RenderComponenet : Component
{
    Sprite sprite;

    void setSpriteWithName(string name) {
        Texture texture(name);
        sprite.setTexture(@texture);
    }
}

If anyone can see that I'm missing anything please let me know.

Thank you.

Have you registered the type Sprite with the flag asOBJ_GC?

If so you also need to implement and register the behaviours asBEHAVE_ENUMREFS and asBEHAVE_RELEASEREFS.

If you notify the GC of the creation of the objects and don't implement all the behaviours needed by the GC, then it will not be able to detect and break circular references.

Manual: Garbage Collected Objects

From the error messages my guess is that you have an instance of a CScriptDictionary that is probably only released after the engine is released. The dictionary object would hold on to a reference of the RenderComponent, and also to the built-in object that describes the RenderComponent (i.e. asIObjectType). The RenderComponent is then holding on the the Sprite object.

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

Not really the dictionary object just resides in a Testcomponent class which is just a container of class members and does nothing atm.

Sprite registers the proper behaviors. It does implement enum references, but it doesn't hold any references as of yet.


It really weird because the RenderComponent destructor gets called, but I still get the error.

This is the only thing angelscript script code that I run:


asIScriptModule* mod = builder.GetModule();
int testTypeId = mod->GetTypeIdByDecl("TestComponent");
asIObjectType *type = mod->GetObjectTypeByName("TestComponent");
testTypeId = type->GetTypeId();
asIScriptObject* objLogicComp = static_cast<asIScriptObject *>(engine->CreateScriptObject(testTypeId));
engine->NotifyGarbageCollectorOfNewObject(objLogicComp, type);
std::cout << "Ref Count: " << objLogicComp->Release() << std::endl;
std::cout << "Ref Count: " << objLogicComp->Release() << std::endl;
engine->GarbageCollect(asGC_FULL_CYCLE | asGC_DETECT_GARBAGE | asGC_DESTROY_GARBAGE);

TestComponent is just an empty class with a Render Component inside of it.

So the destructor of both TestComponent and RenderComponent gets called . Yet I still get these errors in my logs:


add - 2 - instanceId:1 - Sprite
add - 3 - instanceId:1 - Sprite
rel - 2 - instanceId:1
RenderComp Create
add - 2 - instanceId:2 - RenderComponent
add - 3 - instanceId:2 - RenderComponent
rel - 2 - instanceId:2
TestComp Create
rel - 1 - instanceId:2 //This release seems unusual
Ref Count: 2
Ref Count: 1
TestComp Destroy
RenderComp Destroy
//Sprite does not get destroyed
//Shutdown Engine
(0, 0) : ERR : GC cannot free an object of type 'TestComponent', it is kept alive by the application
(0, 0) : ERR : GC cannot free an object of type 'RenderComponent', it is kept alive by the application
(0, 0) : ERR : GC cannot free an object of type 'Sprite', it is kept alive by the application
rel - 1 - instanceId:1
(0, 0) : ERR : GC cannot free an object of type '_builtin_objecttype_', it is kept alive by the application

Ah, there's the problem. :)

The test component is a script class. It will automatically add itself to the GC when it is created in the call to engine->CreateScriptObject(). You must not do this manually.

A second problem is that you're calling Release() twice, but the application only owns 1 reference. That is the one you received from CreateScriptObject(). The application must never call Release() for references it doesn't own.

Try this:

asIScriptModule* mod = builder.GetModule();
int testTypeId = mod->GetTypeIdByDecl("TestComponent");
asIScriptObject* objLogicComp = static_cast<asIScriptObject *>(engine->CreateScriptObject(testTypeId));
std::cout << "Ref Count: " << objLogicComp->Release() << std::endl; // Release the reference received from CreateScriptObject
engine->GarbageCollect(asGC_FULL_CYCLE | asGC_DETECT_GARBAGE | asGC_DESTROY_GARBAGE);

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

Well I'm glad I'm making progress and understanding angelscript more. That code brings the ERRs down by 1 though



(0, 0) : ERR : GC cannot free an object of type 'RenderComponent', it is kept alive by the application

(0, 0) : ERR : GC cannot free an object of type 'Sprite', it is kept alive by the application

(0, 0) : ERR : GC cannot free an object of type '_builtin_objecttype_', it is kept alive by the application

However, I will figure it out.. I think I might just rebuild a minimal project to replicate the problem and try to isolate it. In situations like this I would normally breakpoint Release and AddRef and see which is being called when it isn't supposed to be called, but theres so much I don't know about the engine for me to dig through it.. Hopefully I figure this out soon xD

If you can reproduce the problem with a minimal project please post it or send it to me by e-mail. I'll help figure out what the problem is.

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