• 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: Attack of the Nils!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Attack of the Nils!


  • Subject: Re: Attack of the Nils!
  • From: Ryan Stevens <email@hidden>
  • Date: Mon, 30 Dec 2002 18:29:16 -0800

On Monday, December 30, 2002, at 01:26 PM, mw wrote:

Hello again,

I am having a serious problem in my program with nil's. No matter how many
times I retain an object, it always gets lost!! For example, take this piece
of code:

- (void)setController:(id <NPRolloverProtocol>)newController
{
controller = [newController retain];
[self finalInit];
}

Not sure if this is the correct way to do it or not but I would write this as:

- (void)setController:(id <NPRolloverProtocol>)newController
{
[controller autorelease];
controller = newController;
[controller retain];
[self finalInit];
}

I haven't had any problems using this style of setXXX:. Though, it looks like what you've got should do similar...hmm..

As you can see, newController is retained and assigned to the member
variable called controller in my class. In the debugger, running this
command:

print (int)[controller retainCount]

always yields 2. So there are 2 retains on the object, right? I never, in
any of my code, release this object because its life needs to span the life
of the program. However, in the next function it is used in (which just
happens to be a event handler that is called when the mouse is over the
object, which is a custom control), it is ALWAYS NIL!!!! Here is the code to
this handler, if that makes any difference:

- (void)mouseEntered:(NSEvent *)e
{
NSBundle *mainBundle = [NSBundle mainBundle];
overState = [[NSImage alloc] initByReferencingFile: [mainBundle
pathForResource:[controller getOverStatePath:self] ofType: [controller
getOverStateType:self]]];

[self setImage: overState];
[self setNeedsDisplay:YES]; // force a refresh
[overState release];
}

This is part of the same class as the setController: function is. I was
having a problem with the program where the image that is supposed to be
returned by 'controller' wasn't being displayed properly. So I ran the
debugger, only to find that, to my dismay, 'controller' was now equal to
<nil> and (of course) had a retain count of 0. This is so frustrating! This
whole retaining and releasing business has got me stumped. Why can't Cocoa's
method of handling pointers be the same as C++'s?? To create a new pointer,
you use the new() function. When you are finished with it, you delete() it!!
I have never been able to completely figure out how to use retain/release,
and I have completely given up on ever using autorelease because it always
seemed to make my programs crash. Ach!!!

Does anybody have any advice for me? I have already checked out
Stepwise.com's article on using retain and release, but it really didn't
help any at all. I can't see any feasible reason why controller keeps
getting released from the memory stack. Can anyone else?

TIA,
mw
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Attack of the Nils! (From: mw <email@hidden>)

  • Prev by Date: Re: Localization problem in PB
  • Next by Date: Symbol conflict?
  • Previous by thread: Re: Attack of the Nils!
  • Next by thread: Re: Attack of the Nils!
  • Index(es):
    • Date
    • Thread