Jump to content
  • Advertisement
Sign in to follow this  
dxj19831029

Memory leak

This topic is 3444 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 &in)", 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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!