I have a situation where I have copied a string in an instance of a helper class, retained it, and later released it during the dealloc
of the view controller instance that alloc'd the helper class. This results in the dreaded EXC_BAD_ACCESS. I then went to Instruments to debug zombies. This gave me the following error:
An Objective-C message was sent to a deallocated 'CFString (immutable)' object (zombie) at address: blah blah
When I then look at the allocation summary within Instruments and work backwards from the zombie detection, the first time my code is listed is in the deallocation of the helper class instance. Here is what the helper class looks like. First the .h file:
@interface channelButtonTitles : NSObject {
NSString *channelTitle;
...
}
@property (nonatomic,copy) NSString *channelTitle;
...
@end
Then the .m file:
@implementation channelButtonTitles
@synthesize channelTitle;
...
- (void)dealloc {
[channelTitle release];
...
}
@end
Now relevant code from the view controller that uses the helper class looks like the following. In the .h file I have an array that will hold multiple objects of the helper class as follows:
@interface MyVC : UIViewController {
NSMutableArray *channelTitles;
...
}
@property (retain, nonatomic) NSMutableArray *channelTitles;
Then in the .m code, I synthesize channelTitles
. I also have a dealloc
method as follows:
- (void)dealloc {
[channelTitles release];
...
}
Finally, I alloc object instances of the helper class and store them in channelTitles
with strings stored in the channelTitle
elements of channelButtonTitles
as follows:
[channelTitles removeAllObjects];
self.channelTitles = nil;
channelTitles = [[NSMutableArray alloc] init];
...
for (int i=0; i<numberOfTitles; i++) {
// For each mediaItem, get the title and subtitle info
channelButtonTitles *aChannelButtonTitle = [[channelButtonTitles alloc] init]; // create an object to hold the title and miscellaneous data
aChannelButtonTitle.channelTitle = @"some title";
[channelTitles addObject: aChannelButtonTitle]; // add the title
[aChannelButtonTitle release];
}
So, this is a technique I have used many times before, but seems to not be happy now. When the view controller is popped and I return to the root view controller, the dealloc
method in my view controller is called. That releases channelTitles
which results in calling dealloc
on the channelButtonTitles
helper class objects that are stored in channelTitles
.
Since I have used copy in the property of my helper class, I assume I own this string. Hence, I am releasing it. If I comment out the [channelTitle release]
line from my dealloc
, the EXC_BAD_ACCESS goes away, but I suspect I have a memory leak now. Please help me see what I am doing wrong.