objective c - objc: strong property with NSString -
i declare strong property:
@property (strong) nsstring *message;
and set message like:
self.message = [nsstring stringwithformat:@"xxxx %@",sth]; but crashed message:
*** -[cfstring retain]: message sent deallocated instance 0x1015ea790
even change property: strong-> copy, still crashes. fixed using:
self.message = [[nsstring stringwithformat:@"xxxx %@",sth] copy]; but still can't understand write in same way in ios.
by way: code cocoa, no-arc
update1:
1 have added @synthesize message;
2 sth example, real code
self.message = [nsstring stringwithformat:@"xxxx %@", [[nsdate date] description]]; 3 remember default implementation strong/copy property can like:
- (void)setmessage:(nsstring*)newmsg { if (message != newmsg) { [newmsg retain]; [message release]; message = newmsg; } } so think property-synthesis add copy/retain me. that's makes me confused!
the negative reference count imbalance lies in code has not been posted.
this variant wrong:
self.message = [[nsstring stringwithformat:@"xxxx %@",sth] copy]; -- introduces positive reference count imbalance (hallmark side effect: leaked objects).
first, fix analyzer warnings. if not fix it, run instruments. can record reference count operations each object: debugging exec_bad_access on iphone using performance tool, allocations
note not solve problem: should use copy nsstring property rather strong.
Comments
Post a Comment