Re: Memory management and returned values from methods...
Re: Memory management and returned values from methods...
- Subject: Re: Memory management and returned values from methods...
- From: "Hank Heijink (Mailinglists)" <email@hidden>
- Date: Fri, 15 Jul 2011 15:59:56 -0400
On Jul 15, 2011, at 3:38 PM, Kevin Muldoon wrote:
> Hey guys,
>
> I know of at least one way to fix this memory leak but I'm hoping to find a few more ways.
>
> I'm chewing through a text file of 205,960 lines in a C while loop. All is good until MyObject returns a value. Of course the return value set to autorelease (Well, I suppose it would autorelease anyway if I just didn't copy, alloc or init), but the loop is going so rapidly it never actually releases. The program freezes before it's finished.
Do you have a memory leak, or does your application freeze? Or both? You only talk about the leak in the rest of the message.
> I've avoiding garbage collection since I've started Obj-C but I know (or reasonably suspect) that I could simply
>
> - (NSString *)stopTheMemoryLeakAndKeepOnTruckin {
> NSAutoreleasePool *pool;
> pool = [[NSAutoreleasePool alloc] init];
> //do interesting things...
> [pool drain];
> return [result autorelease];
> }
This wouldn't stop the leak that you have. This code is fine, but it doesn't add anything. Of course, it also doesn't show the relevant code, which is how result is declared and initialized.
> @implementation AppController
>
> NSString *filePath @"/Users/username/output.txt"
> NSMutableArray *objectArray = [[NSMutableArray alloc] init];
> FILE *filePointer;
> char buffer[BUFSIZ] = "Garbage";
> filePointer = fopen([filePath UTF8String], "r");
>
> while (fgets(buffer, sizeof(buffer), filePointer) != NULL) {
>
> NSString *line = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%s", buffer]];
>
> MyObject *myObject = [[MyObject alloc] initWithString:line];
>
> //Do some very interesting things with the line in myObject...
>
> [myObject doAnInterestingMethod];
> NSString * iNeedThisString = [myObject makeAMemoryLeakAndDriveMeCrazy];
>
> [myObject release];
> [line release];
>
> }
>
> @end
This code is fine, although your line could be initialized as follows:
NSString *line = [[NSString alloc] initWithFormat:@"%s", buffer];
or even
NSString *line = [[NSString alloc] initWithUTF8String:buffer];
if you know it's UTF-8. If not, you can use one of the other methods that take a char * (see the NSString docs).
Your problem is most likely in one of these places:
1. //Do some very interesting things with the line in myObject...
2. - (void)doAnInterestingMethod {/**/};
3. //do interesting things...
Posting code is helpful, but posting complete code is even more helpful, especially if you're not sure where the problem is.
Best,
Hank
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden