Subject:
[ruby-ffi] Re: Freeing memory allocated by underlying library (libxml dumpDocFormatMemory)
From:
Wayne Meissner
Date:
2/5/13 4:22 PM
To:
ruby-ffi@googlegroups.com

You're correct that MemoryPointer#free won't do anything for you - it only frees the memory allocated for itself.

From looking at other C based examples, it should be as simple as adding the following (after reading result from the strptr):

  C::LibXML.xmlFree(strptr) unless strptr.null?


Additionally, sizeptr should be allocated as :int, and you should probably read the string via:

    size = sizeptr.read_int
    result = strptr.null? ? nil : strptr.read_string(size)


On Wednesday, February 6, 2013 4:39:12 AM UTC+10, Paul Hinze wrote:
Hi folks!

I'm trying to figure out how i'm supposed to free memory when i get a pointer to a string buffer allocated by my underlying library.

https://github.com/phinze/xml_security/blob/ebd1743f7ab493b6565137ace8d60ad3e5bac52b/lib/xml_security.rb#L216-L224

The docs for xmlDocDumpFormatMemory say it's my responsibility to free the allocated memory with xmlFree [1]. But if i try to call xmlFree on the ptr or the strptr i get segfaults, and MemoryPointer#free seems to not cork the leak.

Any guidance would be much appreciated,

Paul Hinze


[1] http://xmlsoft.org/html/libxml-tree.html#xmlDocDumpFormatMemory


--
 
---
You received this message because you are subscribed to the Google Groups "ruby-ffi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-ffi+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.