views:

95

answers:

2

Hi. 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.

A: 

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.

fbrereto
Yes, thanks, I had done and also manually refreshed.
Chris
+2  A: 

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

http://stackoverflow.com/questions/613868/memory-issue-of-nsstring

Shaggy Frog
Excellent. Thanks. Ahhh - life has returned to nice simple logic again :)
Chris
First time I ran into that, it also spun me for a loop. Even more so because I was trying to test how memory leaks are detected!
Shaggy Frog