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 7928447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:44:05+00:00 2026-06-03T19:44:05+00:00

In a book, the following is said: So how do you know when an

  • 0

In a book, the following is said:

So how do you know when an object is owned, and by whom? Consider the
following example:

NSString *str = [[NSString alloc] initWithString:@”Hello”];  
NSString *str2 = str;

In this example, you use the alloc keyword for str, so you own str.
Therefore, you need to release it when it’s no longer needed. However,
str2 is simply pointing to str, so you do not own str2, meaning you
need not release str2 when you are done using it.

I thought ownership is by object, not variable or pointer… so we can’t say we “own str” or “own str2“… we own an object, which is pointed to by either str or str2, and if we use [str release] or [str2 release], it is all the same.

The other description is:

For example, consider the example used in the previous section:

NSString *str = [[NSString alloc] initWithString:@”Hello”]; 
NSString *str2 = str;
[str release];
[str2 release]; //---this is not OK as you do not own str2---

Attempting to release str2 will result in a runtime error because you
cannot release an object not owned by you.

We can actually use [str2 release] if that is called before [str release]. If we do that, then the line [str release] will cause an error because now str as well as str2 are both dangling pointers, and supposedly when release was sent to the object the first time, the reference count became 0, and dealloc was called immediately, and the memory was freed by the C function free().

Is the above correct, or maybe there is something else to be corrected?

  • 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-06-03T19:44:06+00:00Added an answer on June 3, 2026 at 7:44 pm

    Don’t think of it in terms of managing memory, but in terms of object ownership. You obtain ownership of an object when you allocate it, retain it, or copy it. You are responsible for releasing exactly the objects you own, not others.

    In your example the assignment to str2 does not take ownership of the object, but if you really need a second “owning” reference to it, then you ought to do [str2 retain], after which it is not an error to do [str release]; [str2 release];. This is also what would happen automatically using ARC, unless you annotated str2 as a weak reference. (Of course in this simple case the unnecessary retain/release could be optimized away internally by the compiler.)

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

Sidebar

Related Questions

Consider the following nested type: public class Book { private class Chapter { }
assume the following: @interface BOOK : NSObject { @private NSString *title; NSString *author; NSString
A book as the following example and explains that you won't see the line
I have an Hello world applet,taken from book example. HelloWorldApplet.java import java.awt.*; import javax.swing.*;
I'm following a book on c, and I come to some code that reads
i'm trying to learn codeigniter (following a book) but don't understand why the web
Info: I'm currently trying to learn template metaprogramming (by following this book ). One
I am learning Python, following a book (Python Programming for the Absolute Beginner, 3rd
I am here stuck with a question in my C++ book with the following:
following the text book, I do measure performance whenever I try optimizing my code.

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.