Re: autorelease and distributed objects
Re: autorelease and distributed objects
- Subject: Re: autorelease and distributed objects
- From: Wade Tregaskis <email@hidden>
- Date: Wed, 2 Jun 2004 19:38:44 +1000
Well, if no-one else'll answer it, I guess I'll just have to. :)
NSEnumerator *allDistantObjects= [ ... ];
id theDistantObject;
[theDictionary autorelease];
while(theDistantObject= [allDistantObjects nextObject])
{
[theDistantObject updateWithDictionary: theDictionary];
}
In other word, is it safe to autorelease the dictionary before I send
it out, it should be right since its a copy anyways. Would it also be
safe to instead of autorelease it, release it after all the messages
are sent? I would assume so but I wanted to make sure.
This should be safe, since your autoreleased object is [in practice, at
least] guaranteed to be around until the end of the current method,
even if you do tricksy runloop stuff in it.
[myObject beginEditing];
[myObject setObject: someObject forKey: @"hello"];
[myObject setObject: someObject2 forKey: @"bye"];
[myObject endEditing];
[distantObject updateWithDictionary: [myObject editedProperties]];
My idea is that internall, myObject would have a editedProperties
dictionary, and would set someObject to @"hello", etc. Then, when you
call endEditing, I plan on sending that dictionary the autorelease
message:
- (void)beginEditing
{
editedProperties= [[NSMutableDictionary alloc] init];
}
- (void)endEditing
{
[editedProperties autorelease];
}
Is this "safe"? Or is there a chance of the editedProperties being
deallocated before I send it out.
This one's trickier... I think you'll find in practice that your code
would work, since it's no different from returning an autoreleased
object. However, it is conceptually different, and since it's not
really explicit what's going on inside your black-box "myObject", some
other developer unaware of this trick may come afoul of it.
Better to return the autoreleased dictionary (or whatever it is) in the
endEditing method, so you can pass it directly to your distant object
and handle it explicitly as you would any other method. If you can't
do that, you'll need to delay your [auto]release until deallocation.
You could also perhaps reuse the same dictionary for each edit, which
might save you a bit of overhead in malloc and other such things.
Wade Tregaskis (aim: wadetregaskis)
-- Sed quis custodiet ipsos custodes?
_______________________________________________
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.