Chris
Chris

Reputation: 53

Another iPhone memory leak question!

I have a simple example of what I don't understand about memory management on the iPhone:

- (IBAction)AssignAndReleaseOne :(id)sender {
    for (int i=0;i<10;i++) {
        someString = [[NSString alloc] initWithString:@"String Assigned"];
    }
    [someString release];
}

- (IBAction)AssignAndReleaseTen :(id)sender {
    for (int i=0;i<10;i++) {
        someString = [[NSString alloc] initWithString:@"String Assigned"];
        [someString release];
    }
}

I would expect to get a memory leak in the first method because I alloc 10 times (or is it 11 :) with only one release, but Instruments doesn't report any errors?

Am I or is Instruments correct?

Thanks Chris.

Upvotes: 1

Views: 159

Answers (2)

Shaggy Frog
Shaggy Frog

Reputation: 27601

You won't get a leak, surprisingly enough. See :

Memory issue of NSString

Upvotes: 2

fbrereto
fbrereto

Reputation: 35925

You should be getting the memory leak you expect.

Instruments' leak detection algorithm is expensive to run, so it is only executed after a specified amount of time (I think it's defaulted to 10 seconds). You may have to let the application run for a while before Instruments picks up the leak.

Upvotes: 0

Related Questions