ID:155880
 
Ok, i will be honest, i am realy a novice wehen it comes to concepts of C/C++.

I played around a bit with calling a dll and I am a bit confused about how and when to clean up the memory used by the dll.

The dll i call (just for testing) is nothing more than this:
extern "C" __declspec(dllexport) char *myFunction(int argc, char *argv[])
{
return "blabla";
}


Actually calling this function creates a small memory leak and i am not even sure what causes that leak here in this case, since that function does almost nothing.

Maybe someone can give me a hint, or maybe show me a example of a library that does proper cleaning up.

How are you determining that there is a memory leak? The code you've got there should be fine...
This does actually bring up an interesting point with the DLL interface, that I have no answer for. Should we be cleaning up the strings provided in argv?

I would assume they are the DLL's responsibility to clean up, not BYOND's.
In response to Stephen001
Actually, it seems to be the returned value causing the problem:
extern "C" __declspec(dllexport) char* myFunction(int argc, char *argv[])
{

return NULL; //or return ""; or return "abcdefg"; gives same results
}

Creates Overhead

extern "C" __declspec(dllexport) char* myFunction(int argc, char *argv[])
{

return *argv;
//return argv[0]; works too
}

seems to work

Seems like i just failed in understanding, that i need to return a pointer.
In response to Amsel
Well I suppose your initial example was providing a new string each time. Can you show us your DM code for the first example, that you used to determine a memory leak? It may be that BYOND isn't releasing the string when you'd expect it to.
In response to Stephen001
"asfasfas" is a const char* to an entry in the strings section in the DLL. He's not returning a new string each time - he's returning the same one every time. There should be no leak (unless your speculations re: having to clean up argv are correct. Although that would be bizarre.)
In response to Jp
Now the magic of a black box comes into play. As part of incorporating that string into the VM, does BYOND clone it and shove it in a data structure?
In response to Stephen001
And if it does, how can the DLL clean it up? How can DM clean it up?

If there is a memory leak here, it is a bug in DM.
In response to Jp

Diagramm for the dreamseeker process.

mob
proc
doCalls(var/num)
for(var/i = 10000, i > 0, i--)
set background = 0
call("mylib.dll","myFunction")()

verb
stop()
runn = FALSE

start()
runn = TRUE
while(runn)
src.doCalls(rand(1000, 13000))
sleep(1)


Basically I am doing each tick a random amount of calls.
Up to 12000 calls per tick is what my pc can barely handle.
After using the stop verb, memory stays allocated.
Even a restart of the server (by using the host command) doesn't free the memory.
In response to Amsel
That is distinctly strange. I suggest you report a BYOND bug on the bug tracker, including the DM source code and the DLL source code you've shown us on the forums. If those cause that memory behaviour, it's either a bug in BYOND or it's a bug in the documentation (because you have to do something crazy like del the returned string or clean up argv and it's not documented).
In response to Jp
In response to Jp
I tested a few things and now i'm realy confused. This is the real [expletive deleted], maybe I'll try to get more info tomorrow.