I am just curious if the order expressed for the release of objects should reflect the reverse of their order in the hierarchy:
// Psuedo code
alloc OBJ_001;
alloc OBJ_001 > OBJ_002;
alloc OBJ_001 > OBJ_002 > NSSting;
release NSString;
release OBJ_002;
release开发者_开发技巧 OBJ_001;
I am pretty sure it should (makes sense to me), but have not seen any mention of this in my learning so far.
Gary
It doesn't matter. I assume you mean that OBJ_001
owns OBJ_002
owns the NSString instance, and you own all three objects (co-owning the NSString with OBJ_002
and co-owning OBJ_002
with OBJ_001
).
Doing it in reverse order:
- Release NSString instance: Nothing happens, because
OBJ_002
still owns it. - Release
OBJ_002
: Nothing happens, becauseOBJ_001
still owns it. - Release
OBJ_001
: It releasesOBJ_002
, which releases the NSString. All three dealloc.
Doing it in the same order:
- Release
OBJ_001
: It releasesOBJ_002
, then deallocks.OBJ_002
still exists because you still own it. - Release
OBJ_002
: It releases the NSString, then deallocks. The NSString still exists because you still own it. - Release NSString instance: It deallocks.
Either way, all three instances dealloc. There is no difference.
I prefer the reverse order, but that's purely a style choice.
I wouldn't say so, as long as you're done using it you can release it whenever you want.
精彩评论