[cfe-users] Is it correct to use __weak arguments in Obj-C?

Andrew Pleshkov andrew.pleshkov at gmail.com
Tue Aug 20 01:01:23 PDT 2013


Here's a little example of using weak arguments:

@interface MYTestObject : NSObject
@end
@implementation MYTestObject {
   void(^_block)(void);}
- (void)dealloc {
   NSLog(@"DEALLOC!");}
- (id)init {
   if (self = [super init]) {
      [self doSomethingWithObject:self];
   }
   return self;}
- (void)doSomethingWithObject:(id __weak /* <- weak argument! */)obj {
   _block = ^{
      NSLog(@"%p", obj);
   };}
@end

And it works: -dealloc is called! Also, if you remove __weak you'll get a
retain-cycle and it's absolutely correct.

Wonder, if that's just a side-effect and it's completely unsafe to use weak
arguments? Or is it a specified behavior and I'm just a bad google-user?


P.S.

Here's my question on SO:
http://stackoverflow.com/questions/18316803/objective-c-arc-is-it-correct-to-use-weak-arguments
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20130820/e7a34ab7/attachment.html>


More information about the cfe-users mailing list