【发布时间】:2011-05-11 18:08:47
【问题描述】:
我使用 Nib 作为几个按钮的模板。它似乎工作正常,他们每个人都有自己独立的状态。但是,当我去释放按钮时,我会在 dealloc 中崩溃。这是代码...
mSoundBtns = new cSoundButton*[mNumSounds];
for(unsigned int i = 0 ; i < mNumSounds; ++i) {
mSoundBtns[i] = nil;
}
for(unsigned int s = 0; s < mNumSounds; ++s) {
[[NSBundle mainBundle] loadNibNamed:@"InstanceSoundButton" owner:self options:nil];
//Auto Loads via Outlet into 'soundNib'
mSoundBtns[s] = soundNib;
soundNib = nil;
uint32 count = mSoundBtns[s].retainCount;
NSLog(@"Last Count: %d", count);
}
for(unsigned int j = 0; j < mNumSounds; ++j) {
[mSoundBtns[j] release]; //**** Crash here on 7th (of 8) release
mSoundBtns[j] = nil;
}
标题:
@interface cLocationContext {
...
cSoundButton** mSoundBtns;
IBOutlet cSoundButton* soundNib;
}
@property (nonatomic, assign) IBOutlet cSoundButton* soundNib;
@end
Nib 非常简单,它只包含一个自定义视图类型的父视图和一个子视图。
cSoundButton 只跟踪名称和布尔状态 Mute or Not。这是dealloc
- (void)dealloc {
delete[] mSoundTag;
// Call the inherited implementation
[super dealloc]; //****Crashes in here
}
崩溃发生在对 super dealloc 的调用中,在 UIButton -> UIButtonContent dealloc 中。我假设我的内存管理做得很差,比如释放两次,但我不知道在哪里。
我多次加载笔尖的做法合法吗?
【问题讨论】:
-
retainCount 没用。不要叫它。
标签: iphone objective-c memory-management refcounting