Advertisement

Memory leak

Started by January 14, 2009 07:28 PM
1 comment, last by dxj19831029 15 years, 10 months ago
Hi Angel, I got memory leak. Cost me a few days to trace this. Here is a part of code cause the problem:

for (int i = 0; i < 10000; i++) {
		asIScriptContext *scriptContext = engine->CreateContext();
		engine->ExecuteString("testMemory", "printStr();", &scriptContext);
		scriptContext->Release();
	}
if the context is NULL, there is memory leak. otherwise it keep leaking the memory. wat's wrong here? and also, is there any compiling option for angelscript to detect the memory leak? do u use malloc, new in the angelscript? I saw you use #include <crtdbg.h> in the test future project. I believe angelscript support default memory leak detection. Anyway, here is the full code just in case u need it:

asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
	engine->SetEngineProperty(asEP_ALLOW_UNSAFE_REFERENCES, 1);

	//int result = engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL);

	RegisterStdString(engine);

	asERetCodes nRet = (asERetCodes) engine->RegisterGlobalFunction("void print(const string ∈)", asFUNCTIONPR(printString, (string&), void), asCALL_CDECL);
	assert( nRet >= 0 );
	
	asIScriptModule *module = engine->GetModule("testMemory", asGM_CREATE_IF_NOT_EXISTS);

	char* code = "bool printStr()"
				"{ "
					"print('how are you'); "
					"return true; "
				"}";
	nRet = (asERetCodes) module->AddScriptSection("section1", code, strlen(code));

	nRet = (asERetCodes) module->Build();

	
	
	for (int i = 0; i < 10000; i++) {
		asIScriptContext *scriptContext = engine->CreateContext();
		engine->ExecuteString("testMemory", "printStr();", &scriptContext);
		scriptContext->Release();
	}
	engine->Release();
here is the print message:

void printString(string &str) {
	printf("%s", str.c_str());
}
Cheers
The call

engine->ExecuteString("testMemory", "printStr();", &scriptContext);


will allocate a new context and return the pointer in the scriptContext argument. That way you're not releasing your own context.

If you want to use your own context instead of having ExecuteString allocate a new one, you need to inform this in the fourth parameter.

engine->ExecuteString("testMemory", "printStr()", &scriptContext, asEXECSTRING_USE_MY_CONTEXT);



As AngelScript is a library it will use the heap from the application, so any memory leak detection that your application uses will also verify the allocations made by AngelScript. In the test application I use crtdbg to verify the memory leaks, but I've also created a custom memory manager so that I can monitor the memory consumption of the library. You'll find it in tests/test_feature/source/utils.cpp.

The function asSetGlobalMemoryFunctions can be used to customize the memory management that the library uses. Simply supply two functions with the same signature as malloc() and free(), then AngelScript will use your functions instead of the default ones.

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
Cool, thanks!

Cheers
Quote: Original post by WitchLord
The call

engine->ExecuteString("testMemory", "printStr();", &scriptContext);


will allocate a new context and return the pointer in the scriptContext argument. That way you're not releasing your own context.

If you want to use your own context instead of having ExecuteString allocate a new one, you need to inform this in the fourth parameter.

engine->ExecuteString("testMemory", "printStr()", &scriptContext, asEXECSTRING_USE_MY_CONTEXT);



As AngelScript is a library it will use the heap from the application, so any memory leak detection that your application uses will also verify the allocations made by AngelScript. In the test application I use crtdbg to verify the memory leaks, but I've also created a custom memory manager so that I can monitor the memory consumption of the library. You'll find it in tests/test_feature/source/utils.cpp.

The function asSetGlobalMemoryFunctions can be used to customize the memory management that the library uses. Simply supply two functions with the same signature as malloc() and free(), then AngelScript will use your functions instead of the default ones.

Regards,
Andreas


This topic is closed to new replies.

Advertisement