I'm assuming it's in Car. And that Car has an instance variable of type Engine* called engine. Which is NOT the same Engine* pointer as in the second code fragment (which should be a controller class with it's own instance variables).
I'm assuming it's in Car. And that Car has an instance variable of type Engine* called engine. Which is NOT the same Engine* pointer as in the second code fragment (which should be a controller class with it's own instance variables).
First, engine IS initialized. Engine new is the same (for all practical purposes) as Engine alloc init.So you've got two Car objects, and 1 Engine object. You set the engine instance variable in car1 to point to the engine object.
Then release the original pointer.Fine. Then you assign it to car2 as well. If car2 had a different engine, it gets released and the new one assigned.
If it didn't have an engine assigned, the release message gets sent to nil (which is fine), and it gets assigned as well. It's all fine. PS - while Objective-C has the new construct, it's generally a bad idea to use it because it obscures the two-stage creation pattern that is the norm for Apple frameworks.
Use alloc & init.
Thanks, this is exactly what I thought. So the book is wrong in saying the counter goes to 0 on line car2 setEngine: car1 engine; – monzonj Sep 10 '11 at 20:44 Ah, and Sorry. You were right in all assumptions about the Car object.
– monzonj Sep 10 '11 at 20:44 Yes, I think the book is probably wrong. The only way the release can cause the retain count on the single Engine object to go to zero is if that instance of Car (car2) was already pointing to the Engine object. And in that case, the retain count couldn't be 1, because both car1 and car2 would have the same pointer.
– Flyingdiver Sep 10 '11 at 20:51 Yep, I reported an errata. Let's see what they say. The book would be accurate if car2 is set to: Car *car2 = car1 instead to a new object.
– monzonj Sep 10 '11 at 20:58 True, but then you only have one Car object, which is not equivalent. – Flyingdiver Sep 10 '11 at 23:27.
The setter isn't right. It might be: - (void) setEngine: (Engine *) newEngine { if(newEngine! = engine) { engine release; engine = newEngine retain; } }.
As I said, im not interested in discussing the right way to implement a setter. I already know that. What I want is to find out why the author of the book says that about the retain count.
– monzonj Sep 10 '11 at 20:38 There's nothing wrong with the setter. Checking to see if it's already the same object is an nice optimization, but not necessary. – Flyingdiver Sep 10 '11 at 20:39 Or just retain newEngine prior to releasing engine.
– zneak Sep 10 '11 at 20:39 @Flyingdiver - which setter is there nothing wrong with? – hooleyhoop Sep 10 '11 at 21:06 The one in the original post. The one in the second answer works as well, but it's just a slight optimization.
– Flyingdiver Sep 10 '11 at 23:26.
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.