不应该发生的双重释放 [英] Double releasing when it shouldn't be happening

查看:88
本文介绍了不应该发生的双重释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此感到非常困惑.我相信我以正确的方式管理内存,但是执行代码表明我正在两次释放该对象.这是代码,然后我将解释发生了什么.

@protocol SomeDelegate <NSObject>
@required
- (id)initWithCols:(NSUInteger)Cols Rows:(NSUInteger)Rows;
@end


@interface SomeObject : NSObject <SomeDelegate> {
}
- (id)initWithCols:(NSUInteger)Cols Rows:(NSUInteger)Rows;
@end


@interface Layout : UIView {
  id<SomeDelegate> someDelegate;
}
@property(retain) id<SomeDelegate> someDelegate;
- (id)initWithFrame:(CGRect)aRect Cols:(NSUInteger)Cols Rows:(NSUInteger)Rows;
@end


@implementation Layout
@synthesize someDelegate;
- (id)initWithFrame:(CGRect)aRect Cols:(NSUInteger)Cols Rows:(NSUInteger)Rows {

  if(self = [super initWithFrame:aRect]) {
    cols = Cols; 
    rows = Rows;
    id<SomeDelegate> delegate = [[SomeObject alloc] initWithCols:cols Rows:rows];
    [self setSomeDelegate:delegate];
    //[delegate release];
  }
  return self;
}

-(void)dealloc {
    [someDelegate release];
    [super dealloc];
}

@end

现在,当我取消注释"//[delegate release]"时;行在Layout类的构造函数中,则出现"EXC_BAD_ACCESS"错误,并且当应用程序尝试取消分配时,该应用程序崩溃.我已经将崩溃追溯到Layout类的dealloc方法中someDelegate对象的发布.如果我留下评论,则该应用程序可以正常运行.

有人可以解释一下为什么会发生这种情况吗,因为这似乎与我在Objective-C中阅读的有关内存管理的所有内容背道而驰.

请注意,该代码示例实际上是有效的,但是我的代码不遵循该示例.在我的实际SomeObject内可能会引起自动释放的东西吗?

谢谢.

解决方案

首先,返回并重新阅读

然后将dealloc方法添加到您的delagate中(如果尚未添加)(请确保您调用[super dealloc]!),然后在该处放置一个断点-这将告诉您何时delagate被释放,这将告诉您当它被释放时.

或者,在您的委托类中添加一些琐碎的release/autorelease方法,这些方法除了调用外什么也不做,然后对它们进行断点操作,这将告诉您确切的发布时间.

最后三点评论:在Objective C/Cocoa的标准命名约定中,您应该具有小写的参数字段,即应为:

- (id)initWithFrame:(CGRect)aRect cols:(NSUInteger)Cols rows:(NSUInteger)Rows;

当您的ivar和属性命名相同时,很容易意外使用错误的名称,因此我建议使用不同的ivar名称和属性名称以避免混淆,可以使用_前缀(如Apple)或其他前缀为了避免与Apple混淆:

  id<SomeDelegate> _someDelegate;

@synthesize someDelegate = _someDelegate;

Apple建议不要在init/dealloc中使用setters/getters,因此您的init代码应为:

_someDelegate = [[SomeObject alloc] initWithCols:cols Rows:rows];

I am really puzzled by this. I believe I am managing memory the correct way but executing the code suggests that I am double releasing the object. Here is the code and then I'll explain what is happening.

@protocol SomeDelegate <NSObject>
@required
- (id)initWithCols:(NSUInteger)Cols Rows:(NSUInteger)Rows;
@end


@interface SomeObject : NSObject <SomeDelegate> {
}
- (id)initWithCols:(NSUInteger)Cols Rows:(NSUInteger)Rows;
@end


@interface Layout : UIView {
  id<SomeDelegate> someDelegate;
}
@property(retain) id<SomeDelegate> someDelegate;
- (id)initWithFrame:(CGRect)aRect Cols:(NSUInteger)Cols Rows:(NSUInteger)Rows;
@end


@implementation Layout
@synthesize someDelegate;
- (id)initWithFrame:(CGRect)aRect Cols:(NSUInteger)Cols Rows:(NSUInteger)Rows {

  if(self = [super initWithFrame:aRect]) {
    cols = Cols; 
    rows = Rows;
    id<SomeDelegate> delegate = [[SomeObject alloc] initWithCols:cols Rows:rows];
    [self setSomeDelegate:delegate];
    //[delegate release];
  }
  return self;
}

-(void)dealloc {
    [someDelegate release];
    [super dealloc];
}

@end

Now when I uncomment out the "//[delegate release];" line in the constructor of the Layout class, then I get a "EXC_BAD_ACCESS" error and the application crashes when it attempts to dealloc. I have traced the crash to the release of the someDelegate object in the dealloc method of Layout class. If I leave it commented then the application works fine.

Can someone please explain why this is happening as it appears to be going against everything I have read about memory management in Objective-C.

Just a note that the code example actually works, however my code doesn't which follows the example. Could there be something inside of my actual SomeObject that is causing an autorelease?

Thanks in advance.

解决方案

First, go back and reread the memory management rules just to make sure you are not missing anything obvious in your use of delegate elsewhere.

Next, turn on NSZombieEnabled (in your executable settings, Arguments panel, add an environment variable NSZombieEnabled set to YES).

Then add a dealloc method to your delagate if it does not have one already (make sure you call [super dealloc]!) and put a break point on there - that will tell you when your delagate is deallocated which will tell you when it is being released.

Alternatively, add trivial release/autorelease methods to your delegate class which do nothing but call through, and then breakpoint them and that will tell you exactly when it is being released.

Three final comments: in the standard naming convention for Objective C/Cocoa, you should have lowercase parameter fields, ie it should be:

- (id)initWithFrame:(CGRect)aRect cols:(NSUInteger)Cols rows:(NSUInteger)Rows;

When your ivar and property are named identically, it is very easy to accidently use the wrong one, so I recommend using a different ivar name and property name to avoid confusion, either use an _ prefix like Apple, or some other prefix to avoid confusion with Apple as well:

  id<SomeDelegate> _someDelegate;

@synthesize someDelegate = _someDelegate;

And Apple recomends against using setters/getters in init/dealloc, so your init code should be:

_someDelegate = [[SomeObject alloc] initWithCols:cols Rows:rows];

这篇关于不应该发生的双重释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆