Re: Another memory question
Re: Another memory question
- Subject: Re: Another memory question
- From: Jörn Salewski <email@hidden>
- Date: Tue, 26 Aug 2003 21:49:48 +0200
"option" 2 is even worse. You will have a memory leak _and_ the app will
crash.
>
NSString *demoFile;
>
>
NSString *temp = [[NSString alloc] init];
You're allocating memory for an immutable NSString and initializes it with
nothing.
>
temp = [NSString stringWithString: @"~/Application User Data/dsf.plist"];
To the same memory region you assign an autoreleased NSString - the return
value of your convenience method. The previously allocated and initialized
String will not be accessible anymore --> a LEAK
>
demoFile = [temp stringByExpandingTildeInPath];
demoFile will hold the reference to an autoreleased NSString
>
[demoFile release];
You explicitly release the autoreleased string
>
// more code
How much more code? It will crash.
I can't say exactly what will happen in your first code snippet, but why not
simply write:
NSString *demoFile;
NSString *tmp;
tmp = [NSString stringWithString: @"~/Application User Data/dsf.plist"];
demoFile = [tmp stringByExpandingTildeInPath];
(Or even shorter ?
NSString *demoFile = [[NSString stringWithString: @"~/Application User
Data/dsf.plist"] stringByExpandingTildeInPath];
)
Chears,
JS
_______________________________________________
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.