NSDate appears to leak

  • I'm trying to track down a problem on 10.4.11 using MallocDebug. When
    I had tested this with 10.4.10 I don't think I had this problem, but
    now I get the following:

    (Column-wise going down)

    _pthread_body
    forkThreadDataForFunction
    [Thread3 userThread:]
    +[NSDate dateWithTimeIntervalSinceNow:]

    This is the code that is causing said leak:

    - (void) userThread: (id) param
    {

        //----- create the autorelease pool so we don't leak
        NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];

        sleep(10);

        while (1)
        {
              [[NSRunLoop currentRunLoop] runUntilDate: [NSDate
    dateWithTimeIntervalSinceNow: 1]];
        }

        [localPool release];
    }

    Is NSDate the culprit or is it the fact that NSRunLoop is not thread
    safe?

    Or is MallocDebug making me chase my tail?

    Thanks for any help on this one!
  • On Nov 21, 2007 10:51 AM, Lloyd Sargent <lloyd...> wrote:
    > I'm trying to track down a problem on 10.4.11 using MallocDebug. When
    > I had tested this with 10.4.10 I don't think I had this problem, but
    > now I get the following:
    >
    > This is the code that is causing said leak:
    >
    > - (void) userThread: (id) param
    > {
    >
    > //----- create the autorelease pool so we don't leak
    > NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];
    >
    > sleep(10);
    >
    > while (1)
    > {
    > [[NSRunLoop currentRunLoop] runUntilDate: [NSDate
    > dateWithTimeIntervalSinceNow: 1]];
    > }
    >
    > [localPool release];
    > }
    >
    > Or is MallocDebug making me chase my tail?

    Not sure but the above code is filling up an autorelease pool with
    temporary objects... your loop makes no attempt to manage an
    autorelease pool.

    -Shawn
  • On Nov 21, 2007 10:51 AM, Lloyd Sargent <lloyd...> wrote:
    > I'm trying to track down a problem on 10.4.11 using MallocDebug. When
    > I had tested this with 10.4.10 I don't think I had this problem, but
    > now I get the following:
    >
    > (Column-wise going down)
    >
    > _pthread_body
    > forkThreadDataForFunction
    > [Thread3 userThread:]
    > +[NSDate dateWithTimeIntervalSinceNow:]
    >
    > This is the code that is causing said leak:
    >
    > - (void) userThread: (id) param
    > {
    >
    > //----- create the autorelease pool so we don't leak
    > NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];
    >
    > sleep(10);

    First, this sleep, with a magic-number smells bad to me, why do you need it?

    > while (1)
    > {
    > [[NSRunLoop currentRunLoop] runUntilDate: [NSDate
    > dateWithTimeIntervalSinceNow: 1]];
    > }
    >
    > [localPool release];
    > }

    This loop will create one NSDate instance per second, and none of them
    will be released until the [localPool release] line. There are two
    ways around this:

    1) Don't use an auto-released NSDate instance:

    - (void) userThread: (id) param
    {
        //----- create the autorelease pool so we don't leak
        NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];

        sleep(10);

        while (1) {
            NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow: 1];
            [[NSRunLoop currentRunLoop] runUntilDate: date];
            [date release];
        }
        [localPool release];
    }

    2) Release the pool more often:

    - (void) userThread: (id) param
    {
        sleep(10);

        while (1)
        {
            NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];
            [[NSRunLoop currentRunLoop] runUntilDate: [NSDate
    dateWithTimeIntervalSinceNow: 1]];
            [localPool release];
        }
    }

    >
    > Is NSDate the culprit or is it the fact that NSRunLoop is not thread
    > safe?
    >
    > Or is MallocDebug making me chase my tail?

    --
    Clark S. Cox III
    <clarkcox3...>
  • On Nov 21, 2007, at 1:11 PM, Shawn Erickson wrote:

    > On Nov 21, 2007 10:51 AM, Lloyd Sargent  wrote:
    >> I'm trying to track down a problem on 10.4.11 using MallocDebug. When
    >> I had tested this with 10.4.10 I don't think I had this problem, but
    >> now I get the following:
    >>
    >> This is the code that is causing said leak:
    >>
    >> - (void) userThread: (id) param
    >> {
    >>
    >> //----- create the autorelease pool so we don't leak
    >> NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];
    >>
    >> sleep(10);
    >>
    >> while (1)
    >> {
    >> [[NSRunLoop currentRunLoop] runUntilDate: [NSDate
    >> dateWithTimeIntervalSinceNow: 1]];
    >> }
    >>
    >> [localPool release];
    >> }
    >>
    >> Or is MallocDebug making me chase my tail?
    >
    > Not sure but the above code is filling up an autorelease pool with
    > temporary objects... your loop makes no attempt to manage an
    > autorelease pool.

    Yup... that was the problem. Changed the code to the following:

    - (void) userThread: (id) param
    {
        //----- create the autorelease pool so we don't leak
        NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];

        sleep(10);

        while (1)
        {
            NSAutoreleasePool *mylocalPool = [[NSAutoreleasePool alloc]
    init];
            [[NSRunLoop currentRunLoop] runUntilDate: [NSDate
    dateWithTimeIntervalSinceNow: 1]];
            [mylocalPool release];
        }

        [localPool release];
    }

    Seems to have fixed the unit testing code...

    Cheers,

    Lloyd
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today