Quick and basic retain/release/pointers question
Quick and basic retain/release/pointers question
- Subject: Quick and basic retain/release/pointers question
- From: Keith Blount <email@hidden>
- Date: Sat, 4 Dec 2004 06:56:52 -0800 (PST)
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
Hello,
I've only been developing in Cocoa for a few months,
and although I thought I had grasped the whole
retain/release thing, I think I may have made a
mistaken assumption and would be very grateful if
somebody could clear this up for me, as I've been
having some very-hard-to-track-down crashes in an app
with 1000s of lines of code, and I'm wondering if this
may be my problem.
This is one of the techniques I use in my programming
with retain and release:
SomeCusomView *aView = [[SomeCustomView alloc]
initWithFrame:aRect];
[anotherView addSubview:aView];
[aView release];
//...
[aView doThisOrThat];
ie. I may create a view, assign it as a subview,
release it, but continue using the pointer the pointer
to that view. This is because I read that addSubview
causes the receiver to retain the subview. I therefore
thought I could release this subview, relying on the
parent view to retain it, and blithely continue using
the pointer - I assumed the pointer still pointed to
the correct object.
>From what I have read, I thought that retain did not
actually write the object to another part of memory,
but just added to the retain count. So I thought that
release didn't release the memory unless the retain
count was 0 - so the pointer should still be valid.
Am I wrong? Now, thinking about it, I am wondering if
this is a big mistake, and that the pointer is now
pointing towards a section of memory that may or may
not contain the object I expect. But why would it only
sometimes crash? After I send a release message, would
it continue to work *until* something else is placed
in that section of memory?
Would this, then, be the correct way of doing it:
SomeCusomView *aView = [[SomeCustomView alloc]
initWithFrame:aRect];
[anotherView addSubview:aView];
[aView release];
aView = [[anotherView subviews] objectAtIndex:0]; //
assuming anotherView only has one subview
//...
[aView doThisOrThat];
Or was I right in the first place?
The reason I am wondering about this is because I get
a lot of crashes like this: [[_NSCornerView]
setIntValue:]: selector not recognized, as though my
object has been replaced in memory with another
object.
Is this my problem, or do my errors lie elsewhere?
Many thanks for any clarification,
Keith
__________________________________
Do you Yahoo!?
The all-new My Yahoo! - What will yours do?
http://my.yahoo.com
_______________________________________________
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