views:

110

answers:

1

i usually dont check the retain count untill the program leaks. But today (i donno y) i checked the retain count and now i am confused.

NSString *strErrorMessage; 
    strErrorMessage= [NSString  stringWithFormat:@"Email and Password are mandatory"];
            NSLog(@"%d", [strErrorMessage retainCount]); // retain count 1
    [objAppDelegate ShowMesssage:strErrorMessage];
    NSLog(@"%d", [strErrorMessage retainCount]); // retain count 3
    return;

I don get this. As i dont alloc strErrorMessage, i don have to release it rite?

who has the owner ship of strErrorMessage.

-(void) ShowMesssage: (NSString *)strError
 {
 UIAlertView *alertError = [[UIAlertView alloc] initWithTitle:strTitle   message:strError delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
 [alertError show];
 [alertError release];
   }

Do i have to release strError here?(i thought no, but what abt the retain count 3).

+1  A: 

Checking the retain count generally isn't a good way to debug memory leaks, so I wouldn't worry too much about it as long as you understand where you're allocating/releasing memory. Here you're autoreleasing the error message and manually releasing the alertError so I think you're okay.

I would guess that the two extra retains are in the UIAlertView. It's going to have to retain the error message string at least once to work correctly. The second retain is probably just some internal implementation detail.

Stephen Darlington
Also, the extra retains might be handled by an autorelease at some point so even if it looks like the retain count should be lower, you can never be sure.
deanWombourne
so i should not worry abt the retaincount right? should i be releasing the string-strErrorMessage after i make the function call in the first code?
BK
also it would be helpful if u cud tell me, who has the ownership of any string(variable) if i pass it as a variable to a function?
BK
@BK `strErrorMessage` is autoreleased (since you don't `alloc` it) so you don't need to release it again. Unless you create it (alloc) or claim it (retain) you don't own an object and don't need to release it. Apple has some good documentation on the subject and there are lots of other questions here on SO.
Stephen Darlington
Also, if you `copy` an object you own the copy.
rpetrich
@rpetrich Absolutely. I wasn't trying to write a complete guide to memory management!
Stephen Darlington
@STEPHEN i have read those docs, i got confused wen i saw the retain count jump to 3. Also, could u plz tell me abt the ownership of a variable if i pass it as a argument?
BK
@BK Without knowing how `UIAlertView` is implemented there is just no way of knowing why the retain count is incremented twice. The key point, though, is that it doesn't matter. As long as you keep your end of the deal -- releasing any memory you alloc/retain/copy -- then you can assume that the system does its bit too. Tracking the retain count is not a good way of doing this. Passing a variable as an argument does not change its ownership.
Stephen Darlington