retain or not retain on init?

retain or not retain on init?

Post by Marc » Fri, 08 Aug 2003 18:26:56



In an application I am debugging I found that in the init of some classes I
would find:
phonenumbers = [NSArray array];
and in other classes I found this:
connections= [[NSArray array] retain];

This surprised me, shouldn't it always be retained?

BTW, does [[NSArray alloc] init] automatically retain the object? Any idea
where I can get more info on this?

Thanks,

Marco

 
 
 

retain or not retain on init?

Post by John C. Randolp » Fri, 08 Aug 2003 19:52:14



> In an application I am debugging I found that in the init of some classes I
> would find:
> phonenumbers = [NSArray array];
> and in other classes I found this:
> connections= [[NSArray array] retain];

> This surprised me, shouldn't it always be retained?

It depends.  Does the app need the array to persist beyond the scope of
the init method in question?

Quote:>> BTW, does [[NSArray alloc] init] automatically retain the object? Any idea
> where I can get more info on this?

In a nutshell:  If you +alloc, -copy, or -retain, you get back an object
that needs to be released.  If you use any other means to obtain an
object (such as sending +array, +stringWithFormat:, etc.) releasing it
is not your responsibility.

There are two excellent articles about this at stepwise.com:

http://www.stepwise.com/Articles/Technical/HoldMe.html

http://www.stepwise.com/Articles/Technical/2001-03-11.01.html

-jcr

 
 
 

retain or not retain on init?

Post by Marc » Fri, 08 Aug 2003 20:44:56





> > In an application I am debugging I found that in the init of some
classes I
> > would find:
> > phonenumbers = [NSArray array];
> > and in other classes I found this:
> > connections= [[NSArray array] retain];

> > This surprised me, shouldn't it always be retained?

> It depends.  Does the app need the array to persist beyond the scope of
> the init method in question?

Oops, I should have mentioned that. Both arrays are instance variables of
the class the init belongs to.

Thanks for the link. I will look into them.

Regards,

Marco

 
 
 

1. Why private DC does not retain attributes across threads ?

Hello,

GetDC manual page:

" Remarks:
"
"   For class and private DCs, GetDC leaves the previously assigned
"   attributes unchanged.

This is not true (at least on W2k SP2 English) when the DC is
retrieved/set from different threads.

For example, one thread retrieves DC ('GetDC()') from window with
'CS_OWNDC' class style set and then set a font for this DC
('SelectObject()'). Then, different thread retrieves the DC ('GetDC()')
from the same window and outputs text ('TextOut()'). The text is
output using default font, not the font set by the first thread.
The values of DC handles retrieved by these two threads are different.
These two DC handle values are same if they are retrieved from the same
thread. The difference is probably due to the following info
from 'Remarks' section of 'GetDC' manual page:

" Note that the handle to the DC can only be used by a single thread
" at any one time.

but why should two different private DC handle values, that refer to the
same window, have different set of attributes (cited remark above) ?

Am I missing something or is this just an incorrect documentation or bug ?

Thanks for your thoughts.

--
Radek Tomis

Help stop world hunger -- visit <http://www.thehungersite.org>
Is The Hunger Site real? <http://www.umich.edu/~virus-busters/hunger.html>

2. bootloader with a PC : TI C54x: Help!!!

3. Toolbar Buttons do not retain their color.

4. HighImpact must sell in 5 days!

5. Installing Office XP on a PC with Office 97 and retain use of both

6. HELP!!! Hp-Ux ... apollo 715/50

7. SDI with Multiple Views (and retaining CSplitterWnd)

8. no room at the inn??

9. Need variable in callback to retain its value, cannot be static

10. How to retain a screen saver in the screen saver list?

11. How to stop button from retaining focus?

12. How to Stop Button Retaining Focus?

13. retain in Access