🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Memory leak

Started by
2 comments, last by WitchLord 16 years, 9 months ago
Hi there!! I have a problem with a memory leak that I don't know if it is a problem on how I register a class or is it an AngelScript problem. It only happens when I have an object from a registered C++ class. In this example, I have a Sprite class registered. Here is an example of the code I'm using:

interface Actor {
	bool Initialize();
	void Unload();
} 

class Pepe : Actor {
	Sprite spr;
	
	bool Initialize() {
		if (!spr.Load("data/graphics/uridium.spr"))
			return false;
			
		return true;
	}
	
	void Unload() {
		spr.Unload();
	}
	
} 

Actor@[] pepe;


const int COUNT = 1000;
//==============================================
// Actualización
bool Update(float dt) {

	if (KeyDown(KEY_1)) {
		pepe.resize(COUNT);
	
		for (int i=0; i < COUNT; i++) {
			Pepe caca;
			if (!caca.Initialize())
				return false;
				
			@pepe = @caca;
		}
	}
	
	if (KeyDown(KEY_2)) {
	
		for (int i=0; i < COUNT; i++) {
			pepe.Unload();
			@pepe = null;
		}
		while (GC(true) != 0);
		pepe.resize(0);
	}

	return true;
}
//==============================================

So, when I press "1", the application memory usage increases by 3 mb but when I press "2" it doesn't decrease. Here's how I register the Sprite class:

		if (ScriptMgr::RegisterObjectType(_className, sizeof(Sprite), asOBJ_CLASS_CD) < 0)
			return false;

		if (ScriptMgr::RegisterObjectBehaviour(_className, asBEHAVE_CONSTRUCT,"void f()", asFUNCTION(Constructor), asCALL_CDECL_OBJLAST) < 0)
			return false;
		if (ScriptMgr::RegisterObjectBehaviour(_className, asBEHAVE_DESTRUCT,"void f()", asFUNCTION(Destructor), asCALL_CDECL_OBJLAST) < 0)
			return false;
		if (ScriptMgr::RegisterObjectBehaviour(_className, asBEHAVE_ADDREF,"void f()", asMETHOD(Sprite, AddRef), asCALL_THISCALL) < 0)
			return false;
		if (ScriptMgr::RegisterObjectBehaviour(_className, asBEHAVE_RELEASE,"void f()", asMETHOD(Sprite, Release), asCALL_THISCALL) < 0)
			return false;

I don't know what's going on here. See you soon!
=====================================Regards,Juan Pablo (McKrackeN) Bettini Psychoban Official Site:http://www.psychoban.comPsychoban on iTunes App Store:http://itunes.apple.com/us/app/psychoban/id378692853?mt=8
Advertisement
I've found a problem in my registration code, so for this example the problem is fixed. However, I'm still having trouble with a game i'm developing where this problem persist and I'm trying to isolate it to show you.
=====================================Regards,Juan Pablo (McKrackeN) Bettini Psychoban Official Site:http://www.psychoban.comPsychoban on iTunes App Store:http://itunes.apple.com/us/app/psychoban/id378692853?mt=8
I've solved the problem. It was my fault!

I was registering AddRef and Release methods with asCALL_GENERIC instead of asCALL_THISCALL in other classes. So, the reference counter was filled by garbage.

Sorry! ;)
=====================================Regards,Juan Pablo (McKrackeN) Bettini Psychoban Official Site:http://www.psychoban.comPsychoban on iTunes App Store:http://itunes.apple.com/us/app/psychoban/id378692853?mt=8
I'm glad you found the problem. :)

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