Re: is there a memory leak?
Re: is there a memory leak?
- Subject: Re: is there a memory leak?
- From: Leo <email@hidden>
- Date: Mon, 20 Nov 2006 16:09:22 +0800
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