iphone - Memory leak for variables created inside methods -



iphone - Memory leak for variables created inside methods -

to me, next code looks create leak -- might wrong though:

-(nsstring*) mystring{ nsstring* foo = @"bar"; homecoming foo; }

my question is:

1) create memory leak foo not released?

2) if memory leak how avoid it?

short answer. code not give leak.

long answer:

with nsstring not visible leak, because of strings intern , because not phone call alloc/new/copy methods. yes, classic point of memory leak in general.

there 2 ways of dealing it.

tracing objects returning (or similar) method , releasing them. that's extremely buggy possibility , bad 1 every time. returning autoreleased instance. in fact, did implicitly here. string assignment similar to: nsstring *foo = [nsstring stringwithstring:@"bar"];

and 1 similar to:

nsstring *foo = [[[nsstring alloc] initwithstring:@"bar"] autorelease];

so, homecoming object, has retain count 1, autoreleased. so, when nsautoreleasepool drained, object go away.

iphone objective-c ios4 iphone-sdk-3.0 apple

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -