🎉 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!

String leaks

Started by
4 comments, last by WitchLord 16 years, 10 months ago
I have a script that does alot of string manipulations, after execution i have a large amount of memory leaks :-( There are 2 methods registered in your string class StringAlloc and StringFree i put break point on StringFree and it never been called.... Also i have some questions about using nRefCount i have the folowing func that loads file into the string: void CScripting::LoadFile_Generic(asIScriptGeneric *gen) { asCScriptString* p = (asCScriptString*)gen->GetArgObject(0); asCScriptString * res = new asCScriptString(); CString strArg = p->buffer; CScripting *pScripting = ObjectByEngine(gen->GetEngine()); if (!PathFileExists(strArg)) { strArg.Insert(0,"\\"); strArg.Insert(0,CStatFuncs::ExtractFilePath(pScripting->m_strMainFile)); if (!PathFileExists(strArg)) goto DoRet; } HANDLE hFile = CreateFile(strArg,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0x20,0); if (hFile==INVALID_HANDLE_VALUE) return; DWORD dwSize = GetFileSize(hFile,NULL); DWORD red; char *pChar = new char[dwSize+1]; ZeroMemory((void*)pChar,dwSize+1); //res->refCount = 0; ReadFile(hFile,(void*)pChar,dwSize,&red,NULL); CloseHandle(hFile); res->buffer = pChar; delete [] pChar; DoRet: *(asCScriptString**)gen->GetReturnPointer() = res; } its delclared as "string@ LoadFile(string&)" should i take care of freing this string or script will free it?
Advertisement
Indeed, the StringFree is never called, nor should it be. AngelScript will only call the release behaviour, which is responsible for freeing the memory.

For your function you shouldn't release the reference of the string you're receiving, since you're receiving it by reference.

You have a memory leak in your LoadFile_Generic implementation:

The res variable is allocated at the beginning, but if CreateFile returns an invalid file handle the res variable is never passed to AngelScript. I'm not sure if this is the memory leak you've experiencing. If it's not, let me know so we can try to figure out what's wrong.



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

Yep, i see "return", but should be goto DoRet.
But file is always exists for reading and CreateFile never fails,
so it is not a real solution.
Well, then you have to hunt down the leak. Try to narrow it down by temporarily removing parts of your code/scripts until you know in what situation the leak occurs. Concentrate on the interface between AngelScript and your application, i.e. either where you call script functions or where AngelScript calls registered functions, as these are the most probable locations for the leaks.

If you're using MSVC you can also use the built-in methods for tracking down memory leaks, i.e.:

#include <crtdbg.h>void DetectMemoryLeaks(){	_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);	_CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);	_CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);	// Use _CrtSetBreakAlloc(n) to find a specific memory leak}


Make the above calls the first thing you do in the main function. When the application ends, the memory leaks will be automatically dumped to the debug stream, together with the allocation number and a bit of the memory. Once you know the allocation number, you can use _CrtSetBreakAlloc to set a break point on exactly that allocation so that you can try to determine where it is leaking.

If you determine that the leak must be in AngelScript, then let me know so I can fix it. If so, I'd appreciate it if you can supply me with a small example code that reproduces the leak, as it will make it much easier for me to fix it.

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

Ok.
I done it by overwriting my code.
My code supports that script can execute another script,
privious version just created new instances of iAsScriptEngine,
now i used modules for that purposes.

And i have another question :-)))
Is there any way to know what module is initialized, something like GetActiveModule,i need this because my classes have callbacks and them should know wich module to search for a CB func.

Currently im just call SetUserData(void*) of context, it is almost solution, but
not for global variables, for example my script executes another script and it is built so globals are created and if i call GetActiveContext inside constructor of global var i will get a context of previous script.
I'm sorry, but I can't quite understand what you need.

All the modules that are built are also initialized, i.e. the global variables are initialized before the Build() method returns to the application.

You can get the module from the current script function being executed.

asIScriptContext *ctx = asGetActiveContext();int func = ctx->GetCurrentFunction();const char *module = ctx->GetEngine()->GetFunctionModule(func);

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