Memory management and returned values from methods...
Memory management and returned values from methods...
- Subject: Memory management and returned values from methods...
- From: Kevin Muldoon <email@hidden>
- Date: Fri, 15 Jul 2011 15:38:50 -0400
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.
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];
}
However, there must be a better way than giving up control of
releasing my objects to NSAutoreleasePool. I appreciate ya'll weighing
in on the subject. A small example program below...
//
// AppController.m
// Kvorkian
//
@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
//
// MyObject.m
// Kvorkian
//
- (id)init {/**/};
- (id)initWithString {/**/};
- (void)doAnInterestingMethod {/**/};
- (NSString *)makeAMemoryLeakAndDriveMeCrazy {
//do interesting things...
return [result autorelease];
}
@end
Kevin Muldoon
e: email@hidden
_______________________________________________
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