Retaining objects....

Retaining objects....

Post by Tom Duckeri » Sat, 12 Jul 2003 17:53:28



Hi,

I'm a relative newbie to obj-C and coming from Java, the thing that I
understand least is the whole notion of the memory management.

My question is:

If I have an designed an object that contains other objects, does my
object have to send a retain message to the contained objects? (I hope
that makes sense)

thanks in advance

Tom

 
 
 

Retaining objects....

Post by Paul Mitch » Sat, 12 Jul 2003 18:44:46



> Hi,

> I'm a relative newbie to obj-C and coming from Java, the thing that I
> understand least is the whole notion of the memory management.

> My question is:

> If I have an designed an object that contains other objects, does my
> object have to send a retain message to the contained objects? (I hope
> that makes sense)

The question makes sense. So much sense, in fact, that it's a FAQ.

The answer is: That depends on where and how you're getting the
contained objects. If you're using alloc or new to create the object and
store a reference to it, its retain count is 1, and you don't need to
send it another retain.

Read up: <http://makeashorterlink.com/?H21231B35>

 
 
 

Retaining objects....

Post by Christian Brunsch » Sat, 12 Jul 2003 19:05:58




>Hi,

>I'm a relative newbie to obj-C and coming from Java, the thing that I
>understand least is the whole notion of the memory management.

>My question is:

>If I have an designed an object that contains other objects, does my
>object have to send a retain message to the contained objects? (I hope
>that makes sense)

Objective-C differs from Java in the important aspect that there is
nothing specified in the langiage itself about memory management, which
means that memory management is the respondibility of each class - which
in turn leads to memory management differing between different class
libraries or frameworks.

Considering that you are asing about retaining, though, it would make
sense to assume that you are working under Apple's Cocoa or under GNUstep
- please correct me if I'm wrong. So I am going to proceed on that
assumption, and answer the question in the context of Cocoa/GNUstep.

Essentially, if you have a reference to an object, and you want that
object to 'stick around' until you are done with it, you retain the
object; and then when you _are_ done with the object, you release it.

By 'an object that contains another object', I believe you mean that your
your class has instance variables that refer to other objects (again,
correct me if I am assuming incorrectly). These instance variables are
reference to those other objects, and are essentially the same as any
other references to those other objects. As discussed above, if you want
to make sure the referred-to objects don't go away, you retain them, and
later when you are done with them you release them. So, the short answer
to your question is 'yes, you retain the object'.

However, a good practice when writing object-oriented code in general, is
to not use instance variables directly, but to access them through
accessor methods - also known as 'setter and getter' methods. Only within
the accessors would you actually interact with the instance variables per
se; and even within the rest of the code for the class you would use the
accessor methods. Using this approach, you would only retain or release
objects within the accessor methods, and the whole process is thus
entirely transparent. Something like:

        Bar *bar;

Quote:}

- (Bar *) bar;
- (void) setBar:(Bar *)bar;
- (void) doSomething;


/* simply return the value of the instance variable */
- (Bar *) bar { return bar; }

/* retain the new value first, then release the old value
   this handles the case where you call [self setBar:[self bar]]; */
- (void) setBar:(Bar *)newBar {
        [newBar retain];
        [bar release];
        bar = newBar;

Quote:}

/* in any other code, even within your own class, use the accessor,
   rather than the instance variable */
- (void) doSomething {

Quote:}

/* unless you need very special functionality on ibject initialization,
   you should probably use the accessors even in -init and related
   methods */
- init {
        if ((self = [super init]) != nil) {
                [self setBar:nil];
        }
        return self;

}


All of this is just a rough overview, of course. You might want to have an
accessor method that copies rather than retains the new value, for
instance; and there are different ways that different people like to
structure their accessor methods. But that is a rather different subject.

There is one other point by the way: Because this kind of question really
is rather specific to the class library you are using, it's not really a
language issue per se; which means that this is not actually the best
forum for this kind of question. You may want to investigate the
comp.sys.mac.* newsgroup hierarchy, Apple's Cocoa-Dev mailing list (see
<http://lists.apple.com/mailman/listinfo/cocoa-dev>), or the OmniGroup Mac
OS X development mailing list (see
<http://www.omnigroup.com/developer/mailinglists/macosx-dev/>).

Quote:>thanks in advance

Best wishes and good luck! :)

Quote:>Tom

// Christian Brunschen
 
 
 

Retaining objects....

Post by Tom Duckeri » Sat, 12 Jul 2003 22:09:19


[snippety snip]

Quote:> The question makes sense. So much sense, in fact, that it's a FAQ.

sorry, will check it out :o)
 
 
 

Retaining objects....

Post by David Ste » Mon, 14 Jul 2003 05:30:22



> nothing specified in the langiage itself about memory management, which
> means that memory management is the respondibility of each class - which
> in turn leads to memory management differing between different class
> libraries or frameworks.

Memory management is however not "subclass responsibility".

It's inherited from the root class.

Therefore the root class is important, since its job is to assist in a coherent
memory management, shared between the various libraries and frameworks.

The root class itself doesn't implement the memory management, but it specifies
the Objective-C API towards the C runtime (the low level routines that do the
allocation of C structures to which Objective-C instances and classes map).

So basically it's the Objective-C "virtual machine" (the runtime) that does the
memory management, and the root class is the interface towards that for the
Objective-C layer.

 
 
 

1. retain or not retain on init?

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

2. Where is DCGPOFix

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

4. how do i talk to my serial ports

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

6. US- Platteville, WI - Software Engineer

7. SDI with Multiple Views (and retaining CSplitterWnd)

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

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

10. How to stop button from retaining focus?

11. How to Stop Button Retaining Focus?

12. retain in Access