Women in Technology

Hear us Roar



Article:
  Animating Graphics in Cocoa, Part 1
Subject:   Memory leak
Date:   2003-03-26 18:05:02
From:   anonymous2
This example contains a memory leak.
Each iteration of the animation creates a new NSDate object that is never released.
Something like this is requires:


NSDate *run_time;


run_time = [NSDate dateWithTimeIntervalSinceNow:0.04];
[NSThread sleepUntilDate:run_time];
[Run_time release];


Full Threads Oldest First

Showing messages 1 through 3 of 3.

  • Memory leak
    2003-08-22 15:18:45  anonymous2 [View]

    I'm not an expert, but I think this is not right. I tried it, and adding the release message makes everything crash after the loop ends. I think that dateWithTimeIntervalSince must be creating an already autoreleased object, no?
    • Memory leak
      2005-07-29 18:28:09  valiantsoul [View]

      Thats correct, watch NSCFDate in ObjectAlloc while the program is running - it gets called all the time but the peak amount of times an object of that type exists is 1 meaning no leak.
      • Memory leak
        2005-07-29 18:39:01  valiantsoul [View]

        Oops sorry I forgot to watch CFDate and yes there is a memory leak.

        Here is my modified animate: with no leak:
        - (void)animate:(id)anObject
        {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSDate *date = nil;

        while (YES)
        {
        [self stepAnimation:nil];
        [self setNeedsDisplay:YES];
        if (date != nil)
        [date release];
        date = [NSDate dateWithTimeIntervalSinceNow:0.04];
        [NSThread sleepUntilDate:date];
        }

        [pool release];
        [NSThread exit];
        }