__weak似乎保留了对象

时间:2013-10-04 12:55:32

标签: ios objective-c objective-c-blocks

如果对象被释放,Objective-C弱属性应该指向nil,但在这种情况下,弱属性似乎保留了对象。考虑一下这个案例:

@interface SillyObject : NSObject

@property (nonatomic, assign) NSInteger val;

-(void)printVal;

@end

@implementation SillyObject

-(void)printVal
{
  NSLog(@"%d", self.val);
}

@end

-(void)saveReference
{
  SillyObject* s = [SillyObject new];
  s.val = 100;

  [[ObjectCache sharedInstance] addWeakRef:s callback:^(NSString* junk) {
    [s printVal];
  }];
}

callSillyObjectBlocks遍历添加到缓存中的所有对象并调用相应的块(见下文)

-(void)callDeadObject
{
  [self saveReference];
  [[ObjectCache sharedInstance] callSillyObjectBlocks];
}

现在saveReference退出并且应该释放SillyObject,但它没有,弱引用不是nil。

缓存的相关实现细节:

typedef void (^Callback)(NSString* junk);

@interface CacheSlot : NSObject

@property (nonatomic, copy) Callback callback;
@property (nonatomic, weak) id source;
// More irrelevant properties.

-(instancetype)initWithRef:(__weak id)obj callback:(Callback)cb;

@end

@implementation CacheSlot

@synthesize callback, source;

-(instancetype)initWithRef:(__weak id)obj callback:(Callback)cb
{
  self = [super init];
  if(self)
  {
    self.callback = cb;
    self.source = obj;
  }
  return self;
}

@end

@interface ObjectCache()

// This array contains CacheSlot objects
@property (nonatomic, strong) NSMutableArray* savedObjects;
@end

// Implementation.
-(void)addWeakRef:(id)obj callback:(Callback)block
{
  __weak id src = obj;
  [self.savedObjects addObject:[[CacheSlot alloc] initWithRef:src callback:block]];
}

-(void)callSillyObjectBlocks
{
  for(CacheSlot* slot in self.savedObjects)
  {
    if(slot.source)
    {
      slot.callback(@"Whatever!");
    }
    else
    {
      // Remove the slot from cache
    }
  }
}

最初调用saveReference应该创建一个临时对象,一旦函数退出就会被释放(如果我调用addWeakRef:nil,它就会被释放)。

在调用saveReference之后,我运行callSillyObjectBlocks并且不应该调用添加的对象的相应块,但是它会被对象的值调用。 输出:

100

1 个答案:

答案 0 :(得分:3)

每当你有一个引用块外部变量的块时,你也需要将它声明为 weak 。否则你会获得强有力的参考。所以它应该是:

-(void)saveReference
{
  SillyObject* s = [SillyObject new];
  s.val = 100;
  SillyObject * __weak weakSilly = s;

  [[ObjectCache sharedInstance] addWeakRef:s callback:^(NSString* junk) {
    [weakSilly printVal];
  }];
}

同时,您可以通过从其他几个位置删除__weak来简化代码,因为它们只会影响方法参数,而不会在相关时间内保留实例:

-(instancetype)initWithRef:(id)obj callback:(Callback)cb {
    ...
}

-(void)addWeakRef:(id)obj callback:(Callback)block
{
  [self.savedObjects addObject:[[CacheSlot alloc] initWithRef:obj callback:block]];
}