ID:109852
 
Resolved
Calling an external DLL caused a small memory leak. (Please note: DLLs are still responsible for freeing any memory they allocate themselves.)
BYOND Version:at least since 2009
Operating System:Windows XP Home
Web Browser:Firefox 3.6.13
Applies to:Dream Daemon
Status: Resolved (480)

This issue has been resolved.
Descriptive Problem Summary:
Calling a Dynamic Link Library causes a Memory Leak.

This issue is known since 2009 and mentioned here:
http://www.byond.com/developer/forum/?id=703849

(please look into the few folowing posts for further details)

For the causual calls, this might not be a real problem, but i try to accomplish something with a few hundred cals per tick, and that results indeed in a problem.

I'm going on and try to find a workaround, but it would be nice, to have a way to get that Memory back somehow.

Numbered Steps to Reproduce Problem:
just call a method from any dll

Code Snippet (if applicable) to Reproduce Problem:
mob/verb/Repeat()
var/s = input(src, "What string?") as null|text
if(s)
var/i = input(src, "How many repeats?") as num
src << call("repeat.dll", "repeatString")(s, num2text(i))
for(var/j in 1 to 10000)
call("repeat.dll", "repeatString")(s, num2text(i))

from: http://www.byond.com/developer/forum/?id=703849
Please look into this thread, for further information.

Expected Results:

Actual Results:
Going to post some memory statistics later

Does the problem occur:
Every time? Or how often?
Everytime
In other games?
--
In other user accounts?
--
On other computers?
Most likely.

When does the problem NOT occur?
Couldn't find any Data on that.

Did the problem NOT occur in any earlier versions? If so, what was the last version that worked? (Visit http://www.byond.com/download/build to download old versions for testing.)
Yes since 2009 most likely even before.

Workarounds:
None Yet, but maybe it's possible to reuse the memory by passing the pointer between BYOND and the DLL somehow but i don't know if that would work somehow.
In that very same forum thread, it was pointed out that the assumption that BYOND takes ownership of the return pointer is incorrect; the DLL has to do its own cleanup. Therefore the memory leak is within your DLL.

To do proper cleanup you should free any memory when the DLL is unloaded, or you can also do it on a subsequent call.

For future reference, when posting a bug report you should not put in fields like "since 2009". Just put in the version you're currently using for testing, and if you know the issue dates back to an earlier version that information should go in the text of the report itself.
Ok, i kind of get it now, I think i need to get more into memory management before I cause drama like that again.
Further Feed back from the forums:
http://www.byond.com/developer/forum/?id=775938
Based on the fact that you're seeing a memory leak even when returning a constant string, it's reasonable to reopen this bug report. That should never happen.

From the initial report however I'm sure you see why this was closed in the first place. The post did not include a sample of the DLL code and pointed to a forum thread where the only point of confusion was an incorrect assumption that BYOND takes ownership of dynamically allocated pointers passed to it. If BYOND doesn't free up memory the DLL allocates, that's normal; if it's using up additional memory each time the DLL gives it back a constant string, that's not.