Re: leek in componentsSeparatedByString
Re: leek in componentsSeparatedByString
- Subject: Re: leek in componentsSeparatedByString
- From: Ondra Cada <email@hidden>
- Date: Mon, 26 Aug 2002 17:51:05 +0200
On Monday, August 26, 2002, at 04:35 , David Burris wrote:
Any suggestions would be greatly appreciated.
Use a nested autorelease pool. The way you go releases all objects at once
just before exit. Also, a few general remarks:
tester * c1 = [tester alloc];
Do use capitalized names for classes. Also do use init.
- (void)infile:(NSString *)infile outfile:(NSString *)outfile {
...
//NSMutableArray* words;// = [[[NSMutableArray alloc] init]
autorelease];
[NSMutableArray array] does the same, more nicely, less typing needed...
char* infilename = [infile cString];
char* outfilename = [outfile cString];
char temp[1001];
FILE* inRIB;
FILE* outRIB;
It would be better to use NSFileHandle. If you insist, do at least use
fileSystemRepresentationWithPath:, lest national characters in filenames
are interpreted incorrectly.
NSString* currentline = [[NSString
stringWithCString:temp]autorelease];
Will crash sooner or later. stringWithCString returns a string already
autoreleased -- just like all other standard Cocoa getters do.
NSMutableArray* words = [[currentline
componentsSeparatedByString:@" "]mutableCopy];
Contrariwise, this is leak. +alloc, +new, -copy, and -mutableCopy make new
object which is not autoreleased -- that's your responsibility. Besides,
there seems to be no need to make a copy at all?
[currentline release];
Makes the crash mentioned above twice more inevitable.
//free(temp);
This is plain C memory management, and just as wrong: automatic variables
are "released" automatically as soon as you leave the scope.
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
private email@hidden
http://www.ocs.cz/oc
_______________________________________________
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.