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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:39:09+00:00 2026-06-13T08:39:09+00:00

I’m developing an app in Objective-C using ARC. My simplified code looks like this:

  • 0

I’m developing an app in Objective-C using ARC.

My simplified code looks like this:

ClassA (.m)

MyCustomClass *obj = [[MyCustomClass alloc] initWithValue1:@"abc" value2:1000];
MyViewController *vc = [[MyViewController alloc] initWithObject:obj];
// "vc" will become the first item of a UITabBarController

MyViewController (.h)

- (id)initWithObject:(MyCustomClass *)obj {
    ...
    localReferenceToOjbect = obj;
    ...
}

- (void)viewWillAppear:(BOOL)animated {
    // do something with "localRefernceToObject" <---
}

launching the app will result in a call to a zombie: when the ViewController is shown, the “obj” will be already deallocated and so i can’t use it anymore.

my workaround is:

ClassA (.h)

@interface ClassA : UIViewController {
    MyCustomClass *obj;
}

ClassA (.m)

obj = [[MyCustomClass alloc] initWithValue1:@"abc" value2:1000];
MyViewController *vc = [[MyViewController alloc] initWithObject:obj];
// "vc" will become the first item of a UITabBarController

is this the right way?! i don’t think so: why i’ve to store an istance of an object that is useless for ClassA?
i can’t get an explanation on what’s actually happening. could you help me?

  • 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-13T08:39:10+00:00Added an answer on June 13, 2026 at 8:39 am

    You’re right in the fact that it is not logical to keep around a reference to obj in ClassA.

    But if you need to keep around the reference to obj for MyViewController to use it, retain it in MyViewController, not in ClassA, because that’s MyViewController that will use it.

    The easiest way to do this is to transform your localReferenceToObject you use in MyViewController into a @property(retain) propertyToObject; (or @property(strong) propertyToObject if you use ARC) and access it in your MyViewController.m with self.propertyToObject (instead of localReferenceToObject, to be sure to call the property’s setter and thus really retain the object).

    This way, the object will be retained and kept around while your MyViewController instance is still alive.


    [EDIT] If you want this property to be private, you can declare it in the class extension so that it is not accessible from other classes, as in the below example. See here in Apple’s documentation for more details.

    In your MyViewController.h header file

    @interface MyViewController : UIViewController
    // Here you write the public API in the .h / public header
    // If you don't want your property to be visible, don't declare it there
    @end
    

    In your MyViewController.m file

    @interface MyViewController ()
    // This is the private API, only visible inside the MyViewController.m file and not from other classes
    // Note the "()" to declare the class extension, as explained in Apple doc
    @property(nonatomic, retain) MyCustomClass* referenceToObject; // Note: use strong (which is a synonym of retain) if you use ARC
    @end
    
    @implementation MyViewController
    @synthesize referenceToObject = _referenceToObject; // not even needed with modern ObjC and latest LLVM compiler
    
    - (id)initWithObject:(MyCustomClass *)obj
    {
        self = [super init];
        if (self) {
            ...
            self.referenceToOjbect = obj;
            ...
        }
        return self;
    }
    
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        // do something with "self.refernceToObject"
    }
    
    // This memory management code is only needed if you don't use ARC
    -(void)dealloc
    {
        self.referenceToObject = nil; // release memory
        [super dealloc];
    }
    

    Personally, as suggested by Apple in some WWDC sessions, I now really rarely use instance variables and prefer the use of properties instead, either public in the .h or private in the .m.


    If you use ARC, you can still use an instance variable instead of a property as ARC will retain it for you, but as long as you make sure your instance variable is declared as strong and not weak.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.