Search code examples
objective-cmemory-leaksxcode4release

Cannot release an object


I have a very simple class called Profile. It contains NSMutableArray called avatar which in its turn contains UIImages. This NSMutableArray is declared as nonatomic,retain property. It is allocated in Profile's init method like this:

avatar    = [[NSMutableArray alloc] initWithObjects:
                 [UIImage imageNamed:@"image1.png"], 
                 [UIImage imageNamed:@"image2.png"],
                 [UIImage imageNamed:@"image3.png"],
                 [UIImage imageNamed:@"image4.png"],
                 [UIImage imageNamed:@"image5.png"],nil] ;

and released in Profile's dealloc method.

in my AppDelegate i have global Profile object called currentProfile.It is used in various files. And once i need to release the avatar array of this object in order to copy it with another array. But the copying is never done cause i got runtime error while releasing the array. When i analyze the code i get the following:

Property returns an objective- с object with a + 0 retain count (non-owning reference)

Incorrect decrement of the reference count of an object that is not owned at this time by the caller

What's wrong? Why can't i release the array? just because it is a member of a class? if so how can i replace the content of the array with new one?


Solution

  • well.. if you can't release it you can always set it to nil, so that it drops the whole content and then you can reassign the array with the new load of data. Another solution would be to make a method in the Profile class that makes the = nil + release of the array, so that it can be called directly by the other classes that uses that kind of object.