[Q] Cocoa Object : Behavior when "retain" and "copy"?
[Q] Cocoa Object : Behavior when "retain" and "copy"?
- Subject: [Q] Cocoa Object : Behavior when "retain" and "copy"?
- From: JongAm Park <email@hidden>
- Date: Sun, 11 Feb 2007 08:18:29 -0800
Hello, all..
I started blogging at jongampark.egloos.com on Cocoa programming.
Actually the purpose of my blog is to clarify things which can
mislead or explain things which are not covered in the Apples
document. I'm sorry that it is written in Korean, instead of English.
In Japan, the Apple computer Inc. Japan -- oh.. it is now Apple inc.
-- maintains document written in Japanese.
However, in Korea, people don't enjoy that luxury. :)
So, I write in Korean to communicate with people there.
By the way, one of my friend told me about the issue I posted before.
He pointed out that there is an explanation in the Life-cycle of
Cocoa Objects :
"Generally you copy an object instead of retaining it when it is a
value object—that is, an object encapsulating some primitive value.
This is especially true when that object is mutable, such as an
NSMutableString. For immutable objects, copy and retain can be
equivalent and might be implemented similarly."
It doesn't mention how cocoa object behave in default. Programmers
will usually shallow copy generally, or deep copy when it is really
necessary, ah.. I mean when the object need to stand alone really.
Also, it says that Cocoa object can retain in some situation or copy
in other situation.
It doesn't clearly state about it.
I guess it would be better to draw table, and classify Cocoa objects,
and write default behavior there.
Also, wouldn't it be nice if the default behavior or the copy method
is shallow copy, instead of "retain"?
Probably how things are done the way as it is now is for practical
issue like implementation issue.
However, if it is "retained", although the method you call is "copy",
you should be aware that.. "Oh.. it can be retained, although I call
copy". Wouldn't programmers know when they want to retain or shallow
copy, or even deep copy? In my opinion, it would be better if retain
is retain, and copy is shallow copy in default.
If a programmer wants a deep copy, then he can extend the default
behavior and make it deep copy.
Is my opinion reasonable? :)
Thanks.
On Feb 3, 2007, at 1:43 AM, JongAm Park wrote:
- (NSString *)detectEncodingWithContentOfFile:(NSString *)path
{
NSString *fileContent, *anotherFileContent, *copiedFileContent;
NSError *error;
NSStringEncoding enc;
// First try Unicodes with BOM
fileContent = [NSString stringWithContentsOfFile:path
usedEncoding:&enc error:&error];
if( fileContent == nil ) {
// Try with UTF-8 without BOM
enc = NSUTF8StringEncoding;
fileContent = [NSString stringWithContentsOfFile:path
encoding:enc error:&error];
}
if( fileContent != nil ){
NSLog(@"In %s (%d) : fileContent(0x%X:0x%X) %d\n",
__FILE__, __LINE__, &fileContent, fileContent, [fileContent
retainCount] ); <== (1)
usedEncoding = enc;
anotherFileContent = [fileContent retain];
NSLog(@"In %s (%d) : fileContent(0x%X:0x%X) %d\n",
__FILE__, __LINE__, &fileContent, fileContent, [fileContent
retainCount] ); <== (2)
NSLog(@"In %s (%d) : anotherFileContent(0x%X:0x%X) %d\n",
__FILE__, __LINE__, &anotherFileContent, anotherFileContent,
[anotherFileContent retainCount] );
copiedFileContent = [fileContent copy];
NSLog(@"In %s (%d) : fileContent(0x%X:0x%X) %d\n",
__FILE__, __LINE__, &fileContent, fileContent, [fileContent
retainCount] );<== (3)
NSLog(@"In %s (%d) : copiedFileContent(0x%X:0x%X) %d\n",
__FILE__, __LINE__, &copiedFileContent, copiedFileContent,
[copiedFileContent retainCount] );
return [fileContent retain];
...
What it prints is :
(1) : fileContent(0xBFFFE798:0x4F17A90) 1
(2) : fileContent(0xBFFFE798:0x4F17A90) 2
anotherFileContent(0xBFFFE79C:0x4F17A90) 2
(3) : fileContent(0xBFFFE798:0x4F17A90) 3
copiedFileContent(0xBFFFE7A0:0x4F17A90) 3
The case (2) is when it is retained, while the case (3) is when it
is copied.
For the case (2), where anotherFileContent points is same to that
of fileContent. It is reasonable.
The retain count is 2. OK. It is logical.
However, in the case (3), which is "copy" case, strangely where the
copiedFileContent points is
same to that of fileContent. Also its retain counter is 3, which is
same to that of fileContent.
I expected that the location where the copiedfileContent is
different from that of the fileContent, and
also expected that the reference counter are not increased. However
I'm wrong!!!!
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden