Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6562899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:45:53+00:00 2026-05-25T13:45:53+00:00

In the book Learn Objective-C on the Mac’ you can find the following code

  • 0

In the book “Learn Objective-C on the Mac’ you can find the following code which I think is an errata:


“Here’s another attempt at writing setEngine:

- (void) setEngine: (Engine *) newEngine  
{  
      [engine release];  
      engine = [newEngine retain];  
}

… ponder this case:

Engine *engine = [Engine new]; // count: 1  
Car *car1 = [Car new];  
Car *car2 = [Car new]; 

[car1 setEngine: engine]; // count: 2 
[engine release]; // count 1 

[car2 setEngine: [car1 engine]]; //Ops...

Why is this a problem? Here’s what’s happening. [car engine] returns a pointer to engine which has a retain count of 1. The first line of setEngine is [engine release], which makes the retain count 0, an the object gets deallocated….”


However, after reading this paragraph on page 166. I wonder what the internal engine reference in car2 has to do with engine in car1. They are two different references and by te time “[car2 setEngine: [car1 engine]]” is invoke, the engine in car2 hasn’t been initialized and keeps no relation to the retained engine the author talks about. So “[engine release]” won’t decrease te counter to 0 of car1 engine.
See my point? Is it an errata or am I terrible wrong?

((I would like to discuss this particular code, not the right way of implementing a setter))

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:45:54+00:00Added an answer on May 25, 2026 at 1:45 pm

    What class is setEngine: in? 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: preg_match_all('/(.*) \((\d+)\) - ([\d\.\d]+)[,?]/U', E-Book What I Didn't Learn
I've been reading the book Learn Objective C on the Mac, and I was
Anyone can read the GoF book to learn what design patterns are and how
i'm trying to learn codeigniter (following a book) but don't understand why the web
Can you recommend some great book to learn how to make app with web
I was trying learn lex and yacc using the oreilly book. I tried following
I am trying to learn cocos2d for iphone, following the following book http://www.apress.com/9781430233039 .
I am following a book to learn C++ (come from a python background). I've
I am following a book to learn Android and am getting an error, this
I'm reading a book called Java and C: Key Differences, Learn Objective-C for Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.