objective c - properties - non public weak setter appears to work, but getter only returns the same value as first call -


using properties want publicly visible getter , privately visible setter object weak reference. thought had worked using class extension. until called getter, both, before , after setting object nil. getter worked if called before or after object set nil. here have:


bar.h

#import <foundation/foundation.h>  @interface bar : nsobject @property (nonatomic, readonly, weak) nsobject *object;  // note readonly - (id) initwithobject:(nsobject *)object; @end 

bar.m

#import "bar.h"  @interface bar ()  // class extension @property (nonatomic, readwrite, weak) nsobject *object;  // note readwrite @end  @implementation bar  - (id) initwithobject:(nsobject *)object {     self = [super init];     if (self)     {         self.object = object;     }     return self; }  @end 

main.c

#import "bar.h"  int main(int argc, char *argv[]) {     @autoreleasepool {          // call getter once, before setting object nil.         // appears work.         nsobject *object1 = [[nsobject alloc] init];         bar *bar1 = [[bar alloc] initwithobject:object1];         nslog(@"before      - bar1.object[%p] object1[%p]",bar1.object, object1);          // call getter once, after setting object nil.         // appears work.         nsobject *object2 = [[nsobject alloc] init];         bar *bar2 = [[bar alloc] initwithobject:object2];         object2 = nil;         nslog(@"after       - bar2.object[%p] object2[%p]",bar2.object, object2);          // call getter twice, before , after setting object nil.         // appears work work first call getter.         nsobject *object3 = [[nsobject alloc] init];         bar *bar3 = [[bar alloc] initwithobject:object3];         nslog(@"both before - bar3.object[%p] object3[%p]",bar3.object, object3);         object3 = nil;         nslog(@"both after  - bar3.object[%p] object3[%p]",bar3.object, object3);          return 0;     } } 

results

before      - bar1.object[0x9623030] object1[0x9623030] after       - bar2.object[0x0] object2[0x0] both before - bar3.object[0x7523d90] object3[0x7523d90] both after  - bar3.object[0x7523d90] object3[0x0] 

i expected both after be: both after - bar3.object[0x0] object3[0x0] .

it appears weak reference not being set nil when getter called prior setting object nil , again calling again after.

when call getter before, weak property being loaded , converted strong reference, being autoreleased. when call again after, object3 nil expected, original object referenced still alive, sitting in autorelease pool. therefore bar3.object still has valid object return you.

try following instead:

nsobject *object3 = [[nsobject alloc] init]; bar *bar3 = [[bar alloc] initwithobject:object3]; @autoreleasepool {     nslog(@"both before - bar3.object[%p] object3[%p]",bar3.object, object3); } object3 = nil; nslog(@"both after  - bar3.object[%p] object3[%p]",bar3.object, object3); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -