Re: Memory Leak with NSMutable String
Re: Memory Leak with NSMutable String
- Subject: Re: Memory Leak with NSMutable String
- From: Fabrizio La Rosa <email@hidden>
- Date: Mon, 26 Jan 2004 14:24:19 +0100
Il giorno 26/gen/04, alle 12:14, Alexander F. Hartner ha scritto:
The following code uses as diagnosed with ObjectAlloc and MallocDebug
more and more memory. It seems that NSMutableString don't get properly
released. I am still quite new to Objective C and most likely am doing
something incredible stupid. Any suggestions are welcome.
Thanks
Alex
#import <Foundation/Foundation.h>
#include <stdio.h>
void doOtherStuff()
{
NSMutableString *data = [NSMutableString stringWithCapacity:1000];
[data appendString:@"DATA"];
[data release];
You don't need to release a such created object because all those
objects created using a convenience method (those beginning with
stringWith... for NSString) are automatically autoreleased.
Try to remove the [data release] line and check if the memory leak
still occurs.
I am sure that someone has a better explanation, but probably the fact
that you release twice and recreate the same object n times could cause
some sort of "confusion" to the Objective-C runtime system, because in
such cases the object is not effectively created and released at every
iteration but the same memory address (and probably the same object)
could be re-used instead.
printf("Interation \n");
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSLog(@"Hello, World!");
int counter=0;
for (counter=0; counter<1000000;counter++)
{
doOtherStuff();
}
[pool release];
return 0;
}
_______________________________________________
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.
_______________________________________________
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.