Samui
Samui

Reputation: 1384

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];

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!

Upvotes: 0

Views: 886

Answers (4)

Kasaname
Kasaname

Reputation: 1501

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

Upvotes: 0

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.

Upvotes: 0

Steven Kramer
Steven Kramer

Reputation: 8513

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.

Upvotes: 0

Lily Ballard
Lily Ballard

Reputation: 185801

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.

Upvotes: 3

Related Questions