1

我有一种情况,我在辅助类的实例中复制了一个字符串,保留它,然后在dealloc分配辅助类的视图控制器实例期间释放它。这会导致可怕的 EXC_BAD_ACCESS。然后我去 Instruments 调试僵尸。这给了我以下错误:

一条 Objective-C 消息被发送到地址为:blah blah 的已释放 'CFString (immutable)' 对象(僵尸)

然后,当我查看 Instruments 中的分配摘要并从僵尸检测向后工作时,我的代码第一次被列出是在帮助程序类实例的释放中。这是助手类的样子。首先是 .h 文件:

@interface channelButtonTitles : NSObject {
    NSString *channelTitle;
    ...
}

@property (nonatomic,copy)  NSString *channelTitle;
...
@end

然后是 .m 文件:

@implementation channelButtonTitles
@synthesize channelTitle;
...

- (void)dealloc {
    [channelTitle release];
    ...
}
@end

现在,视图控制器中使用辅助类的相关代码如下所示。在 .h 文件中,我有一个数组,它将保存辅助类的多个对象,如下所示:

@interface MyVC : UIViewController {
    NSMutableArray *channelTitles;
    ...
}
@property (retain, nonatomic)   NSMutableArray *channelTitles;

然后在 .m 代码中,我合成channelTitles. 我还有一个dealloc方法如下:

- (void)dealloc {
    [channelTitles release];
    ...
}

最后,我分配帮助类的对象实例,并将它们channelTitles与存储在channelTitle元素中的字符串一起存储,channelButtonTitles如下所示:

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

}

所以,这是我以前用过很多次的技巧,但现在似乎不太高兴。当视图控制器被弹出并返回根视图控制器时,dealloc我的视图控制器中的方法被调用。该版本channelTitles会导致调用存储dealloc在.channelButtonTitleschannelTitles

由于我在我的助手类的属性中使用了副本,我假设我拥有这个字符串。因此,我正在发布它。如果我从我的 中注释掉该[channelTitle release]dealloc,EXC_BAD_ACCESS 就会消失,但我怀疑我现在有内存泄漏。请帮我看看我做错了什么。

4

0 回答 0