[Kde-bindings] QtRuby memleak continued
Richard Dale
rdale at foton.es
Fri Sep 1 10:54:54 UTC 2006
On Friday 01 September 2006 08:32, Ashley Winters wrote:
> --- Ashley Winters <jahqueel at yahoo.com> wrote:
> > In the latest Qt4 version, the m->cleanup() logic has been commented
> > out. Is there a reason?
>
> Found it. I was looking in the wrong spot. Here's one leak that I think
> I see:
>
> template <>
> static char* ruby_to_primitive<char *>(VALUE v)
> {
> if(v == Qnil)
> return 0;
>
> // hello strdup() leak
> return strdup(StringValuePtr(v));
> }
>
> template <>
> static void marshall_from_ruby<char *>(Marshall *m)
> {
> VALUE obj = *(m->var());
>
> // new pointer assigned, and then lost?
> m->item().s_voidp = ruby_to_primitive<char*>(obj);
> }
>
> The proper fix would be to remove the strdup() from
> ruby_to_primitive<char *>:
>
> - return strdup(StringValuePtr(v));
> + return StringValuePtr(v);
>
> and move it to the marshall_from_ruby<char *> function:
>
> - m->item().s_voidp = ruby_to_primitive<char*>(obj);
> + char *s = ruby_to_primitive<char*>(obj);
> + m->item().s_voidp = m->cleanup() ? strdup(s) : s;
>
> At least, I'd guess it was the fix. Not that I've tested it, or
> anything. :)
I added the strdup() to fix a bug in KAboutData:
KAboutData( const char *appName,
const char *programName,
const char *version,
const char *shortDescription = 0,
int licenseType = License_Unknown,
const char *copyrightStatement = 0,
const char *text = 0,
const char *homePageAddress = 0,
const char *bugsEmailAddress = "submit at bugs.kde.org"
);
QtRuby was deleting the 'char *'s after they were passed to the call, but that
meant they were junk in the app's Help->About menu. So perhaps it was the
wrong way to fix it, but how can you tell whether or not you are supposed to
free the 'char *' after the call? In Ruby the C api always takes a copy of
the 'char *' and so it would be correct to delete it. I've no idea why these
args aren't QStrings anyway, it all seems pretty ugly and won't handle
unicode in people's names.
-- Richard
More information about the Kde-bindings
mailing list