So, this is pretty standard memory management, from what I understand:
ClassName *temp=[[ClassName alloc] init];
self.ivar=temp;
[temp release];
This is to avoid the memory leak created by just doing this:
self.ivar=[[ClassName alloc] init];
Cool. But, suppose I have several ivars that are based ClassName? Is this okay:
ClassName *temp=[[ClassName alloc] init];
self.ivar=temp;
self.othervar=temp;
self.anothervar=temp;
[temp release];
Would they all be manipulating the same object, ultimately, even though I want them to have different instances of ClassName? I assume that the outcome of this might depend on if the ivars were created as retain vs copy? Suppose they are set to retain, would this be okay?
the default expectation is “yes, they would reference the same object because this is a reference counted system.”
not might, but entirely. if the property is declared
copyand the type adoptsNSCopying, that should be all that is needed. if you implement the setters (what are often synthesized properties) yourself, you mustcopyin your implementations.then they would all refer to the same instance. whether that is the correct semantic for your program depends on how you need it to behave. in this example, you stated “I want them to have different instances of ClassName” so you would either need to create an unique instance for every destination (either directly or via
copy, ifClassNameadoptsNSCopying).