开发者

Memory Management in iOS

Hello everyone (sorry for my english). I'm new with objetive-C and iOS programming. May you help me?

@interface RootViewController : UITableViewController 
{
    NSMutableArray *keysFromQuery;
}

@property (nonatomic, retain) NSMutableArray *keysFromQuery;
....
....
NSString *category = [keysFromQuery objectAtIndex:0];
开发者_Go百科

What will happen with [keysFromQuery objectAtIndex:0] if I do: [category release];?

What will happen with category if I do after that: [keysFromQuery release];?

I don't know well how references and memory mechanisms work.

Thanks!


Don't call -release on category. You don't own the object. You must only release objects which you have taken ownership of, via NARC (new/alloc/retain/copy). For more info read the Memory Management Programming Guide.


If the method that returned you the object does not explicitly state that it returns a "new", "retain"ed, "alloc"ed, "created" or "copy"-ied object, you must not release it. It's that simple.

So do a release (or autorelease) only for cases like

[object retain];
[object copy];
[SomeClass newInstanceWithProperty: @"a"];
[[SomeClass alloc] initWithProperty: @"a"];

etc.

So in this case, you don't have to do anything and the "release" you mention would crash your app in all likelihood.


What will happen with [keysFromQuery objectAtIndex:0] if I do: [category release];? You shouldn't do that, because it is an autoreleased object.

What will happen with category if I do after that: [keysFromQuery release];? The NSString is still saved, when you release keysFromQuery after the initialization.


Dont release the objects as you don't own them and you have not even allocated them .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜