Hi,
This is something that doesn't affect linux, though there could well be other operating systems in addition to Win32 that *are* affected.
Let's say I have a static libfoo library that has a my_foo() function that returns a newly allocated (with
malloc) string. In C, we can code as follows:
char * string;
string = my_foo();
// Do stuff
free(string);
No problem with that but, on Win32 perl 5.10 and 5.8, in XS (Inline::C) code that's built against that libfoo library, the same code causes a
"Free to wrong pool error..." when the free() call is made.
Remove the free() call, and everything is fine - except, of course, that you've then got memory leaks to contend with.
How can this be dealt with in XS code ? That is, how can this memory be freed in XS code ?
I've written a simple libfoo library that demonstrates the issue (which I can post, if that helps). My simple libfoo library also demonstrates that if the library itself has a function to free the memory, then the problem is solved. That is, if libfoo has a function called foo_free() that looks like this:
void foo_free(char * string) {
free(string);
}
then, instead of calling
free(string) in the XS code, I just call
foo_free(string) and everything is fine.
But the real life
library I'm looking at (libidn) doesn't appear to have such a function (should it ?). That library has a number of functions that return a newly allocated string. For most of those functions, the way that the memory is supposed to be freed is not specified - but where it is specified, the documentation says that the memory should be freed
by calling free(), and I surmise that calling free() is the only way that the memory can be freed. As I've outlined above, that works fine for C programs (on both linux and win32), but not for XS code (on win32 only).
Cheers,
Rob
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.