Re: alloc/release confusion
Re: alloc/release confusion
- Subject: Re: alloc/release confusion
- From: Boon Chew <email@hidden>
- Date: Fri, 13 Feb 2009 16:55:28 -0800 (PST)
Thanks Bryan.
How do I go about knowing whether a method like show would do the retain? The reference doc doesn't say it. Also, what if it doesn't? Would a premature release dealloc an object that's still alive (such as a dialog), causing the system to crash when the memory is overwritten by another object later?
Is there a way in Cocoa to find out the reference count of an object (for debugging purposes)?
- boon
--- On Fri, 2/13/09, Bryan Henry <email@hidden> wrote:
> From: Bryan Henry <email@hidden>
> Subject: Re: alloc/release confusion
> To: "Boon Chew" <email@hidden>
> Cc: "email@hidden" <email@hidden>
> Date: Friday, February 13, 2009, 4:02 PM
> When you call -show, the UIAlertView is retained elsewhere
> (somewhere in SpringBoard's internals).
>
> It does look a bit odd, and understandably so, but the
> -release is correct there because you still want to
> relinquish your ownership of the object...the ownership you
> took when you sent the -alloc message. That release
> doesn't actually deallocate the object because it was
> retained by some part of -show's implementation.
>
> Bryan
>
> Sent from my iPhone
>
> On Feb 13, 2009, at 6:02 PM, Boon Chew
> <email@hidden> wrote:
>
> > Hi all,
> >
> > I am very new to Cocoa programming (but have
> programmed in C/C++ before) and there is one thing I
> don't understand with alloc and release. Sometimes I
> see code that alloc an object and release it within the same
> method, even though it's clear that the object is still
> live and well.
> >
> > For example:
> >
> > -(IBAction)onButtonPressed
> > {
> > UIAlertView *alert = [[UIAlertView alloc]
> initWithTitle...];
> > [alert show];
> > [alert release];
> > }
> >
> > Why is the code decrementing the ref count even though
> the alert window is still up?
> >
> > Also, how do you know when you are decrementing ref
> count too soon? How do you know which object method you call
> might increase the ref count of the object in question?
> >
> > Thanks!
> >
> > - boon
> >
> >
> >
> >
> >
> >
> >
> > _______________________________________________
> >
> > Cocoa-dev mailing list (email@hidden)
> >
> > Please do not post admin requests or moderator
> comments to the list.
> > Contact the moderators at
> cocoa-dev-admins(at)lists.apple.com
> >
> > Help/Unsubscribe/Update your Subscription:
> >
> >
> > This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden