Memory management in Cocoa?

Firstly, you want to make sure your setter is checking to see if the objects are different.

Firstly, you want to make sure your setter is checking to see if the objects are different: - (void)setEngine:(Engine *)newEngine { if ( engine! = newEngine ) { engine release; newEngine retain; engine = newEngine; } } Here's what happens inside this method: Check to see that the engine that has been passed in is different to the one we already have. If it is the same, nothing needs to be done.

Release the old engine. If we were the last object to own it, its retain count will be zero and it will be deallocated. If the old engine was nil, then the release message will simply be ignored.

Retain the new engine. If it is released somewhere else, it will not be deallocated because we have taken ownership of it. If the new engine is nil, then the retain message will simply be ignored.

Set the engine instance variable to newEngine. Secondly, the reason that the retain count is two after you call setEngine: is because it started as 1 when it was created. Then you assigned it to car1, which retained it, bringing the retain count to 2.

The retain count is two because your outer code owns it and car1 owns it. You should pass it to car1 and car2 in the same way (car2 setEngine:car) which would bring the retain count to 3. Once you have passed it along to car1 and car2, you can release it it your outer code - this tells Obcjective-C that your outer code no longer owns it, the ownership lies only with car1 and car2.

Here's a full example: // create an engine, we will own it for now Engine *engine = Engine alloc init; /* engine retain count: 1, owned by this context */ Car *car1 = Car alloc init; Car *car2 = Car alloc init; // pass engine along to cars car1 setEngine: engine; car2 setEngine: engine; /* engine retain count: 3, owned by car1, car2 and this context */ // release engine, we no longer need it engine release; /* engine retain count: 2, owned by car1 and car2.

Engine = newEngine retain; } – Sahitya Tarumani Jan 18 '11 at 5:30 I added a description of what is happening inside setEngine:. – Simon Cave Jan 18 '11 at 5:40 ...and I just edited it again so it's a bit clearer. – Simon Cave Jan 18 '11 at 5:42 Does that mean that the setter method also causes increase in the retain count of an object?

I ask so because when engine is passed as a parameter to the setter method, it is retained by car1. – Sahitya Tarumani Jan 18 '11 at 5:45 Ah I think I understand what is confusing you. Initially the engine property is nil because it has not been set yet.

This means that when you call engine release you are actually calling nil release. This may seem strange, but there is actually nothing wrong with it. You can call any method on nil and it will simply ignore it.So the first time you call release inside the setter, it is being ignored.

Likewise, if you were to call setEngine:nil then it would call retain on nil, also ignored. – Simon Cave Jan 18 '11 at 5:47.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions