FROM : Ryan Stevens
DATE : Tue Dec 31 03:29:16 2002
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_removed>
> 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_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
DATE : Tue Dec 31 03:29:16 2002
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_removed>
> 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_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
| Related mails | Author | Date |
|---|---|---|
| mw | Dec 30, 22:26 | |
| Michael Latta | Dec 30, 22:52 | |
| mw | Dec 30, 23:35 | |
| mw | Dec 30, 23:44 | |
| Marcel Weiher | Dec 30, 23:51 | |
| Michael Latta | Dec 31, 00:02 | |
| Kevin Callahan | Dec 31, 00:12 | |
| Ryan Stevens | Dec 31, 03:29 | |
| mw | Dec 31, 21:46 |






Cocoa mail archive

