• 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: retain, release, autorelease (was Checkboxes and NSBox titles)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: retain, release, autorelease (was Checkboxes and NSBox titles)


  • Subject: Re: retain, release, autorelease (was Checkboxes and NSBox titles)
  • From: "David P. Henderson" <email@hidden>
  • Date: Thu, 21 Jun 2001 09:44:50 -0400

On Wednesday, June 20, 2001, at 03:39 , jgo wrote:

> Could someone provide a snippet that shows or words that explain
> how it works with containers/collections. It always bothered me
> in C++ because it seemed like I was either eating at least twice
> as much memory as was strictly required (get the info, use it to
> initialize objects, add the objects to the collection...) or
> futzing over exactly what got destructed when (what constituted
> scope), and I'd rather avoid that in Objective-C/Cocoa.
>
The same rules for retain/release cycles apply to collections as to
non-collections in Cocoa, with the addendum that when you insert an
object into a collection, the collection retains it, and when you remove
an object from a collection, the collection releases it. Further when
you release a collection, all the objects in the collection are also
released.

I don't see the difficulty with scope. The scope of a object's data
member is the lifetime of the object. The scope of a method's variables
is the end of the method block. So how long an object persists depends
on where it is declared and how it is assigned.

Contrived example:

@interface MyObject :NSObject
{
NSArray *myArray;
NSData *myData;
}
- (void)doSomething;
- (void)doSomethingElse;

// Accessor methods
...
@end

@implementation MyObject

- (id)init
{
[super init];
return self;
}

- (void)dealloc
{
[myArray release]; // release any objects when MyObject instances
are released
[myData release]; // otherwise we will leak
[super dealloc];
}

- (void)doSomething
{
NSMutableArray *tempArray = [NSMutableArray array]; // returns an
autoreleased array
id anObject;

while (some test)
{
anObject = [[ADifferentObject alloc] init]; // returns a
retained object
[tempArray addObject:anObject]; // adds anObject and retains it
[anObject release]; // We are done with anObject; get rid of it
or we leak
}
[self setMyArray:tempArray];
// tempArray goes out of scope here, and it will be dealt with at
some point by the autorelease pool
}

- (void)doSomethingElse
{
NSMutableArray *tempArray = [[NSMutableArray alloc]
initWithArray:[self myArray]]; // returns a retained array

while (some test)
{
// process tempArray
}
[tempArray release]; // tempArray goes out of scope here if we
don't release it explicitly we have a memory leak.
}

...
// Assume definition of accessor methods
@end

Hopefully this clarifies the issue.

Dave
--
Chaos Assembly Werks
"Beautiful bodies and beautiful personalities rarely go together."
- Carl Jung


References: 
 >Re: retain, release, autorelease (was Checkboxes and NSBox titles) (From: jgo <email@hidden>)

  • Prev by Date: Re: WWDC Dev Tools / Software update
  • Next by Date: Re: Adding a preferences panel to the system preferences panel folder
  • Previous by thread: Re: retain, release, autorelease (was Checkboxes and NSBox titles)
  • Next by thread: Re: Checkboxes and NSBox titles
  • Index(es):
    • Date
    • Thread