I have a costume class called Region and I am using this bit of code to initialize the region variable how is that possible that at the end of this bit of code myAppDelegate.provisoryRegion is not nil and region is nil??? isn’t myAppDelegate.provisoryRegion passed by reference to region???
I’m 100% sure that region is nil at the end of this piece of code.
Region *region=myAppDelegate.provisoryRegion;
if (!myAppDelegate.provisoryRegion) {
myAppDelegate.provisoryRegion=[[Region alloc]init];
}
if (!region){ NSLog(@"region is nil");
}
Yes. It must be nil.
Because you change the object!
When doing
myAppDelegate.provisoryRegion=[[Region alloc]init];myAppDelegate.provisoryRegion points to a new object (a new address on the heap).*regionwill NOT follow to the new address!So
*regionstill point to theNULLaddress.But why you copy your pointer at line 1?
Why not always use
myAppDelegate.provisoryRegion.*regionis a unlinked alias ofmyAppDelegate.provisoryRegionJust do at the end in your sample-dummy code and delete line 1:
And by the way…
looks like a memory leak (if your @property is set to retain).
Fix with: