Objective C: Releasing a list, will it cause potential leaks? - objective-c

Tried finding the answer online, but couldn't. So i'm wondering if anyone else knows and why?
Say I have an NSDictionary, or NSArray, that stores objects inside of them. If I release the NSDictionary, is there a potential leak because I didn't release the objects inside of the NSDictionary list?
For example:
NSDictionary *dict = [NSDictionary alloc] init];
// Create a bunch of objects, NSStrings, etc.
// Store it into dict.
[dict release];
Will that also release everything inside of the dict? (objects, nsstrings, etc).
Thanks in advance people!

All items in an NSDictionary or NSArray are automatically retained when they're added and released when removed, or when the list is destroyed.
For example:
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
MyObject *obj = [[MyObject alloc] init];
[dict setObject:obj forKey:#"foo"]; // the dictionary retains "obj"
[obj release]; // this matches the "alloc/init"
// but "obj" still is retained by the dictionary
[dict release]; // now "obj" gets released

When you do a release on a NSDictionary or NSArray, as long as the retain count of the objects inside the array is 1 (meaning as long as you released the objects after you inserted them inside the data structure), then once you release the dictionary or array, those objects will be released as well.

Related

NSArray copy for undo /redo purposes

If I have an nsarray full of custom objects and I make a second array using:
NSArray *temp = [NSArray arrayWithArray:original];
then work with some properties of the objects inside the original array, then decide to roll back, I am then using the reverse:
original = [NSArray arrayWithArray:temp];
I am finding the objects I changed in the array also effected my temp array. I also tried implementing copyWithZone on my custom class, and using copyItems and it did not help. What else should I try?
To be clear, in order to use copyWithZone, I changed my array creation command to:
NSArray *temp = [[NSArray alloc] initWithArray:original copyItems:YES];
My copyWithZone:
-(id)copyWithZone:(NSZone *)zone{
CustomObject *ret = [[CustomObject allocWithZone: zone] init];
//copy properties
return ret;
}

Instruments not detecting obvious memory leak

I have created a very obvious memory leak in application didFinishLaunchingWithOptions.
NSArray *temp = [[NSArray alloc] initWithArray:[[NSArray alloc] initWithArray:#[]]];
temp = [[NSArray alloc] initWithArray:#[]];
temp = nil;
However the tools show no memory leaks at any point, which leads me to believe there not working properly. Anybody else experienced this?
From my observation: the empty NSArray is basically a singleton instance. You can't create memory leaks in this way, because your app will always have a reference to the empty array "singleton".
This will show you that all empty arrays point to the same memory address:
NSArray *array1 = [NSArray array];
NSArray *array2 = [NSArray arrayWithArray:array1];
NSArray *array3 = [NSArray arrayWithArray:#[]];
NSArray *array4 = #[];
NSArray *array5 = [#[] copy];
NSArray *array6 = [[NSArray alloc] initWithArray:[[NSArray alloc] initWithArray:#[]]];
NSLog(#"%p", array1);
NSLog(#"%p", array2);
NSLog(#"%p", array3);
NSLog(#"%p", array4);
NSLog(#"%p", array5);
NSLog(#"%p", array6);
No matter where in your application lifecycle you log the address of the empty array, it will always be the same.
You should try your test with NSMutableArray. Or, even better, use an class you created yourself.
Sometimes there is heavy optimization going on in the background if you use built in classes. For example class clusters, where you can't be sure what exact class an initializer returns, or singleton instances like in this case.

Objective-c NSMutableArray release behaviour

Does the release, recursively releases all inner objects? or must it be done manualy?
Can I do just this?
NSMutableArray *list = [[NSArray alloc] init];
// ...
// fill list with elements
//...
[list release];
Or must I release all inner objects one by one before releasing the NSMutableArray? // Suposing there isn't any other reference to the contained objects, except on the list itself.
Yes it does. It retains them when added, and releases them when dealloc'd. This is actually one of the most common questions I see here.
If you are owning the object then you will have to release it.
NSMutableArray *list = [[NSArray alloc] init];
NSString *str = [[NSString alloc] init] // you are the owner of this object
[list addObject:str];
[str release]; // release the object after using it
[list release];
If you are not the owner of the object then you should not release.
NSMutableArray *list = [[NSArray alloc] init];
NSString *str = [NSString string]; // you are not owning this object
[list addObject:str]; // str retain count is incremented
[list release]; // str retain count is decremented.
This is the concept which even array also uses. When you add any object to the array, array will retain it. In the sense it becomes the owner of that object and It will release that object when you release the array.

Does NSString componentsSeparatedByString: return autoreleased array?

In the following method, I'm unsure of why releasing one of the arrays leads to an exception. The only reason that I could see, would be if componentsSeparatedByString returns an autoreleased array, but I can't see that the documentation mentions that it do.
-(void)addRow:(NSString *)stringWithNumbers;
{
NSArray *numbers = [stringWithNumbers componentsSeparatedByString:#" "];
NSMutableArray *row = [[NSMutableArray alloc] initWithCapacity:[numbers count]];
for (NSString *number in numbers) {
Number *n = [[Number alloc] initWithNumber:number];
[row addObject:n];
[n release];
}
[rows addObject:row];
[row release];
// [numbers release]; <-- leads to exception
}
Can anyone confirm if the array is autoreleased? If so, how can I know/why should I have known?
Is it possible to check if any one instance of an object is autoreleased or not by code?
Yes, because the name of the method:
does not start with new
does not start with alloc
is not retain
does not contain copy
This is commonly known as the "NARC" rule, and is fully explained here: http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html#//apple_ref/doc/uid/20000043-SW1
unless you specifically allocate memory, a system method will give you back an autoreleased method.
By convention all methods with init or copy in their names return non-autoreleased objects.

Initializing an instance variable

With an instance variable myArray:
#interface AppController : NSObject
{
NSArray *myArray;
}
Sometimes I see myArray initialized like this:
- (void)init
{
[super init];
self.myArray = [[NSArray alloc] init];
return self;
}
and sometimes I see it with a more complicated method:
- (void)init
{
[super init];
NSArray *myTempArray = [[NSArray alloc] init];
self.myArray = myTempArray
[myTempArray release];
return self;
}
I know that there's no difference in the end result, but why do people bother to do the longer version?
My feeling is that the longer version is better if the instance variable is set up with a #property and #synthesize (possibly because the variable has already been alloced). Is this part of the reason?
Thanks.
If myArray is a property and it's set to retain or copy (as it should be for a property like this), then you'll end up double-retaining the variable when you do this:
self.myArray = [[NSArray alloc] init];
The alloc call sets the reference count to 1, and the property assignment will retain or copy it. (For an immutable object, a copy is most often just a call to retain; there's no need to copy an object that can't change its value) So after the assignment, the object has retain count 2, even though you're only holding one reference to it. This will leak memory.
I would expect to see either a direct assignment to the instance variable
myArray = [[NSArray alloc] init];
Or proper handling of the retain count:
NSArray *newArray = [[NSArray alloc] init];
self.myArray = newArray;
[newArray release];
Or the use of autoreleased objects:
self.myArray = [[[NSArray alloc] init] autorelease]; // Will be released once later
self.myArray = [NSArray array]; // Convenience constructors return autoreleased objects
This is an idiom used in mutators (sometimes called "setters"), but I think you typed it slightly wrong. Usually it looks like this:
-(void)setMyName:(NSString *)newName
{
[newName retain];
[myName release];
myName = newName;
}
The new name is retained, since this instance will need to keep it around; the old name is released; and finally the instance variable is assigned to point to the new name.
I have a feeling you mean this:
NSArray* a = [NSArray arrayWithObjects:#"foo", #"bar", nil];
and this
NSArray* a = [[NSArray alloc] initWithObjects:#"foo", #"bar", nil];
//...
[a release];
With the first style, the static method performs an alloc/init/autorelease on it for you so you don't have to. With the second style, you have more control over when the memory is released instead of automatically releasing when you exit the current block.
That code will crash your application. The second version only copies the pointer then releases the instance. You need to call [object retain] before releasing the reference.