Skip navigation.
 
mlBig leak in -[NSFileHandle availableData]?
FROM : Larry Campbell
DATE : Mon Nov 18 19:56:01 2002

Sure seems like it. Here's some code, kind of useless but it's about
the smallest I could make the example:

//////////////////////////////////////
#import <Foundation/Foundation.h>
#import <unistd.h>

static void doTest(void)
{
    NSPipe *pipe = [NSPipe pipe];
    NSTask *task = [[NSTask alloc] init];
    NSData *d;

    [task setLaunchPath:@"/usr/bin/who"];            // just some random command
that writes output
    [task setStandardOutput:pipe];
    [task launch];
    d = [[pipe fileHandleForReading] availableData];
    NSLog(@"1 Got %u bytes", [d length]);
    d = [[pipe fileHandleForReading] availableData];        // this seems to
leak
    NSLog(@"1 Got %u bytes (0x%x)", [d length], d);
    [task waitUntilExit];
    [task release];
}

int main(const int argc, const char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSLog(@"Starting...");

    doTest();
    doTest();
    [pool release];

    NSLog(@"Sleeping...");
    while (1)
        sleep(1);
}
//////////////////////////////////////

If you run this under MallocDebug, you'll see that NSFileHandle is
leaking an NSData object of 4096 bytes. It only leaks when you call
-[NSFileHandle availableData] when there's no more data left. If you
comment out the second call to -[NSFileHandle availableData], the leak
goes away. It's a real leak, not some MallocDebug artifact: if you wrap
this in a loop and run it for a while you'll see your process VSIZE
grow and grow and grow.

Anyone else run into this? Anyone know a cure?

(I call doTest twice because if I only call it once there's no leak,
but this does seem to be a MallocDebug artifact; even when I
deliberately leak with a malloc call, MallocDebug only finds it when
it's called twice.)


Related mailsAuthorDate
No related mails found.