Re: Re: is there a memory leak?
Re: Re: is there a memory leak?
- Subject: Re: Re: is there a memory leak?
- From: "Julien Jalon" <email@hidden>
- Date: Mon, 20 Nov 2006 09:51:27 +0100
I really think you should read memory management topic in documentation:
http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/MemoryManagementRules.html
In short:
Autorelease pools are releasing objects marked as autoreleased.
You mark an object as autoreleased by calling its autorelease method.
You have to release alloc'ed or copied objects either by releasing
them directly or marking them as autoreleased.
If you do:
stringFromFileAtPath=nil;
[stringFromFileAtPath release];
since you assigned nil to stringFromFileAtPath, [stringFromFileAtPath
release] won't do anything.
Here is your (mail compiled) code fixed:
- (NSString *)getContent:(NSString *)filename{
NSString *fileContents = nil;
int cnt = 0;
static const int plainTextFileStringEncodingsSupported[] = {4,
-2147481296, -2147481280, -2147481087, -2147481085, -1};
while(plainTextFileStringEncodingsSupported[cnt] != -1) {
NSError *error;
NSStringEncoding encoding = plainTextFileStringEncodingsSupported[cnt];
fileContents = [[NSString alloc] initWithContentsOfFile:filename
encoding:encoding error:&error];
if(fileContents != nil){
break;
}
// fileContents is nil, so nothing is to be released, just loop
cnt++;
}
// fileContents needs to be released
// autorelease will defer the release to the next autorelease pool discard
// (if fileContents == nil, this won't do anything, so it's OK)
return [fileContents autorelease];
}
Last thing, you should replace the line:
static const int plainTextFileStringEncodingsSupported[] = {4,
-2147481296, -2147481280, -2147481087, -2147481085, -1};
by using NSStringEncoding instead of int and NS*StringEncoding
constants instead of explicit values.
--
Julien
On 11/20/06, Leo <email@hidden> wrote:
how should i release stringFromFileAtPath?
i think NSAutoreleasePool has already release it, when i release it like
this:
if(stringFromFileAtPath!=nil){
fileContent=stringFromFileAtPath;
stringFromFileAtPath=nil;
[stringFromFileAtPath release];
cnt=5;
}
the leak also exist , if i delete stringFromFileAtPath=nil; the method
crash.
email@hidden ??:
> i didn't take a careful look but it appears as though you aren't
> releasing stringFromFileAtPath after you alloc and init it on each
> iteration in the while loop. the autorelease pool doesn't pick up
> stringFromFileAtPath since you don't mark the variable for
> autoreleasing when you alloc+init it - just release it before it loops
> back and give mallocdebug a go and see if that resolved it. is the
> autorelease pool needed for something else?
>
> cheers,
> jean-pierre
>
>
> On 11/19/06, Leo <email@hidden> wrote:
>> Hi all:
>>
>> here is my code:
>>
>> - (NSString *)getContent:(NSString *)filename{
>> NSString *fileContent;
>> int cnt = 0;
>>
>> static const int plainTextFileStringEncodingsSupported[] = {4,
>> -2147481296, -2147481280, -2147481087, -2147481085, -1};
>>
>> while(plainTextFileStringEncodingsSupported[cnt] != -1) {
>> NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
>> NSError *error;
>> NSStringEncoding encoding=plainTextFileStringEncodingsSupported[cnt];
>> NSString *stringFromFileAtPath=[[NSString alloc]
>> initWithContentsOfFile:filename encoding:encoding error:&error];
>>
>> if(stringFromFileAtPath!=nil){
>>
>> fileContent=stringFromFileAtPath;
>> stringFromFileAtPath=nil;
>>
>> cnt=5;
>>
>> }
>> else{
>> cnt++;
>>
>> }
>> [pool release];
>>
>> }
>>
>> return fileContent;
>> }
>>
>> when using mallocDebug to test this method and it displayed there is a
>> leak in "[[NSString alloc] initWithContentsOfFile:filename
>> encoding:encoding error:&error] ", does anybody can tell me why ? i
>> think there is no leak at all.
>>
>> thank you!
>>
>>
>> Leo
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> Cocoa-dev mailing list (email@hidden)
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>>
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden