• 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: Memory Management
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory Management


  • Subject: Re: Memory Management
  • From: Shaun Wexler <email@hidden>
  • Date: Tue, 29 Jul 2003 09:15:22 -0700

On Tuesday, July 29, 2003, at 01:23 AM, p3consulting wrote:

Le lundi, 28 juil 2003, ` 21:58 Europe/Brussels,
email@hidden a icrit :

- (void)setName:(NSString *)newNamemet {
[newName retain];
[_name release];
_name = newName;
}

This is fine.

Thread safe ?

I'd prefer to use this semi-thread-safe method, ensuring that the object pointed to by _myObject will not already be released if accessed when thread context changes between the retain/release of the above method. Ex:

- (void) setObject:(id)newObject
{
if (newObject != _myObject) {
id _oldObject = _myObject;
_myObject = [newObject retain];
[_oldObject release];
}
}

- (void) myObject
{
return [[_myObject retain] autorelease];
}

Making a copy of an object might be appropriate in some cases, but is wasteful of memory and generally not necessary, other than where you might pass a mutable object using an immutable pointer:

NSString *objectA = @"I'm a string";
NSString *objectB = [NSNumber numberWithInt:12345];
NSMutableArray *mutableArray = [NSMutableArray arrayWithObject:objectA];

[self setObject:mutableArray];
[mutableObject replaceObject:objectA withObject:objectB];
NSLog(@"%@", NSClassToString([[_myObject objectAtIndex:0] class]);

This fails to return "NSString" with the -setObject method above, returning "NSNumber", but if the accessor+copy method was used...

- (void) setObject:(id)newObject
{
if (newObject != _myObject) {
id _oldObject = _myObject;
_myObject = [newObject copy];
[_oldObject release];
}
}

- (void) myObject
{
return [[_myObject retain] autorelease];
}

NSString *objectA = @"I'm a string";
NSString *objectB = [NSNumber numberWithInt:12345];
NSMutableArray *mutableArray = [NSMutableArray arrayWithObject:objectA];

[self setObject:mutableArray];
[mutableObject replaceObject:objectA withObject:objectB];
NSLog(@"%@", NSClassToString([[_myObject objectAtIndex:0] class]);

...then it would return "NSString", as expected. I would rename the -setObject: method -setObjectWithCopy: instead, for clarity.

The thread-safe benefit of using the retain/autorelease accessor method is that a 2nd thread might release the object directly or from within its NSAutoreleasePool, while its being used in another thread, but the accessor method extended the object's lifetime by placing into its OWN NSAutoreleasePool.

I typically just use simple accessors which directly set and return the object, and never have any problems since the object is usually used within my own code, but when creating the AppKit/Foundation and iApp's, Apple's overly-paranoid use of retain/autorelease and copy help to ensure stability for all.

Whatever floats your boat... ;)
--
Shaun Wexler
MacFOH
http://www.macfoh.com
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Memory Management
      • From: "M. Uli Kusterer" <email@hidden>
References: 
 >Re: Memory Management (From: p3consulting <email@hidden>)

  • Prev by Date: Re: Memory Management
  • Next by Date: Re: Talking to the Dock (Nooo - stop it!)
  • Previous by thread: Re: Memory Management
  • Next by thread: Re: Memory Management
  • Index(es):
    • Date
    • Thread