Message *m = [msg retain];
dispatch_queue_t queue = _handle_queue;
__block ProtocolDriver* blockSelf = self;
dispatch_async(queue, ^{
if (blockSelf.protocol_delegate && [blockSelf.protocol_delegate respondsToSelector:@selector(onReceive:)]) {
[blockSelf.protocol_delegate onReceive:m];
}
});
I have a MRC objective-c file, which need run some code in block asynchronously.
But I thought the blockself
is not safe since it is not weak pointer, Should I change it to true weak pointer.
@interface WeakPtr : NSObject
@property (nonatomic, weak) id realObj;
@property (nonatomic, assign) bool released;
@end
@implementation WeakPtr
@end
set the released when the MRC-object is dealloced, and test it in block
@property (nonatomic, retain) WeakPtr* selfPtr;
- (id)init{
...
self.selfPtr = [[[WeakPtr alloc] init] autorelease];
self.selfPtr.realObj = self;
}
- (void)dealloc
{
...
self.selfPtr.released = true;
}
-(void)pump: (Message *)msg
{
Message *m = [msg retain];
dispatch_queue_t queue = _handle_queue;
__block WeakPtr* weakSelf = self.selfPtr;
dispatch_async(queue, ^{
if (!weakSelf.released && [weakSelf.protocol_delegate respondsToSelector:@selector(onReceive:)]) {
[weakSelf.protocol_delegate onReceive:m];
}
});
}