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

  • Home
  • SEARCH
  • 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 714745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:06:34+00:00 2026-05-14T05:06:34+00:00

I have several objects in my app that can become nil at some point

  • 0

I have several objects in my app that can become nil at some point and I have methods that in theory are used to put these objects to nil.

But, if I try to put to nil an object that does not exist, the app will crash.

for example…

[object1 release];
object1 = nil;

//… and after that

[object1 removeFromSuperview]; // this will crash

Then I thought, why not testing to see if the object exists before removing…

if (object1 != nil)
 [object1 removeFromSuperview]; 
// this will crash too, because object1 cannot be tested for nil because it does not exist

How can I check if the object exists before testing if it is nil?
something as

if (object1 exists( {
  if(object1 != nil))
    [object1 removeFromSuperview)
}

is this possible?

I ADDED THIS TO CLARIFY…

what I mean is this: imagine I have object1 declared on the header and initialized on the code. So, it exists and points to a valid reference. At some point, the object may be released, so the reference still points to an object but the object was freed. Even if I put the object to nil after releasing, I cannot do anything with it.

The problem is this: I have some methods that are asynchronous. One of them scans for some objects and remove them if they are found. I must check if the object exists and the reference points to a valid object before releasing it again. This is the point: how do I test if the object exists and its reference points to a valid existent object before releasing it again, to void releasing again an object that is already released and crash the app.

  • 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-14T05:06:34+00:00Added an answer on May 14, 2026 at 5:06 am

    In short, your question of determining if the pointer is still valid is going down the wrong path.

    After you release an object, you must immediately set its value to null so that you no longer control it. If two methods are running asynchronously accessing the same object, they must be synchronized at that point so that releasing and setting to null happens at the same time in one thread before the other has a chance to interrupt.

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

Sidebar

Related Questions

Assume you have some objects which have several fields they can be compared by:
I have a class that creates several IDisposable objects, all of these objects are
I have a mutable array that is retained and storing several objects. At some
I have an iPhone app that used to use an array of several thousand
I have an application that has several objects (about 50 so far, but growing).
I have a tree that consists of several objects, where each object has a
I have an ASP.NET web app that places several string data into an object
I have several objects in the database. Url to edit an object using the
During coding I frequently encounter this situation: I have several objects ( ConcreteType1 ,
I have an array of FileReference objects which have several listeners attached to each

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.