• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: checking if an object's instance exists.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: checking if an object's instance exists.


  • Subject: Re: checking if an object's instance exists.
  • From: Kevin Ballard <email@hidden>
  • Date: Sat, 5 Feb 2005 01:11:25 -0500

When you release object1, set it to nil afterwards. Just releasing it won't set it to nil, so if it's the last reference you'll end up with a pointer to random memory, which could crash or it could raise an exception (depending on if any of the memory has been re-used for anything else). If you set object1 to nil after releasing it, you can now check the pointer to see if it's nil or not (which is what you're doing), and if it's nil then you know you've already released it.

You should also set object1 to nil in your init method to be safe (AFAIK there's no docs saying ivars are zeroed when allocated, so I wouldn't assume it)

On Feb 5, 2005, at 12:45 AM, Aaron Boothello wrote:

I've subclasses NSObject for  a custom class by the name of 'myObject'.
An instance of 'myObject' is called 'object1'.

I allocate memory and initialize the instance like this:
object1 = [[myObject alloc]init];

And release it like this:
[object1 release];

At one point in my code, i need to access the instance, but it could happen that the object hasnt been allocated or initialized. how do i check ?
my block of code looks something liek this:


-(void)method1
{
   [release object1];
   [self method2];
}

-(void)method2
{
int x;

if(object1)
     x=[object1 length];
}

but it dosent seem to work.
basically i need to check if the instance object1 exists or not.
Any suggestions ?

-- Kevin Ballard email@hidden http://www.tildesoft.com http://kevin.sb.org

Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: checking if an object's instance exists.
      • From: Chris Ridd <email@hidden>
    • Re: checking if an object's instance exists.
      • From: Sherm Pendley <email@hidden>
References: 
 >checking if an object's instance exists. (From: Aaron Boothello <email@hidden>)

  • Prev by Date: Re: NSCoding question regarding BOOL
  • Next by Date: Re: checking if an object's instance exists.
  • Previous by thread: checking if an object's instance exists.
  • Next by thread: Re: checking if an object's instance exists.
  • Index(es):
    • Date
    • Thread