多个Nib加载后无法释放(Failing to Release after Multiple Nib loads)

编程入门 行业动态 更新时间:2024-10-23 09:27:59
多个Nib加载后无法释放(Failing to Release after Multiple Nib loads)

我使用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只是跟踪名称和布尔状态静音或不静音。 这是dealloc

- (void)dealloc { delete[] mSoundTag; // Call the inherited implementation [super dealloc]; //****Crashes in here }

崩溃是在UIButton - > UIButtonContent dealloc的super dealloc调用中。 我假设我在做内存管理方面做得很差,比如两次解除分配,但我无法找到原因。

通过多次加载笔尖是合法的,我正在做什么?

I am using a Nib as a template for several buttons. It seemed to work fine, they each have their own independent state. However when I went to release the buttons I would crash in the dealloc. Here is the code...

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; }

Header:

@interface cLocationContext { ... cSoundButton** mSoundBtns; IBOutlet cSoundButton* soundNib; } @property (nonatomic, assign) IBOutlet cSoundButton* soundNib; @end

The Nib is very simple, it just include a parent view and a child view of a custom view type.

cSoundButton simply keeps track of a name and a boolean state Mute or Not. Here is the dealloc

- (void)dealloc { delete[] mSoundTag; // Call the inherited implementation [super dealloc]; //****Crashes in here }

The crash is inside the call to super dealloc, in UIButton -> UIButtonContent dealloc. I assume I am doing something poor with my memory management like deallocing twice but I can't spot where.

Is what I am doing by loading the nib multiple times legal?

最满意答案

从NIB加载按钮后,您必须立即保留按钮。 如果不这样做,则不允许以后释放它,并且一旦代码将控制权返回到runloop(当自动释放池耗尽时),您将无法访问该按钮。

PS:使用Cocoa集合( NSMutableArray )来存储对按钮的引用会不会更容易? 你的代码对我来说太复杂了。

You have to retain the button as soon as you load it from the NIB. If you don't, you are not allowed to release it later, and you won't be able to access the button once your code returns control to the runloop (when the autorelease pool is drained).

PS: Wouldn't it be easier to use a Cocoa collection (NSMutableArray) to store the references to the buttons? Your code looks too complicated to me.

更多推荐

本文发布于:2023-04-27 16:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327162.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   加载   Nib   Failing   Multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!