开发者

NSString leaking memory

I'm kinda new in finding memory leaks in objective c and how to fix them. I'm know how to use alloc/init/copy and release/retain but ( a least i think so :-) ) but i have some strange memory leaks in my IOS app.

-(void) sendStats {

// read the app settings
plistHandler *readData  = [[plistHandler alloc] init];
[readData setPlistName:@"Settings"];
NSDictionary *settingsArray  = [readData readPlist];
[readData release];

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:[NSString stringWithFormat:@"%@", [settingsArray objectForKey:@"range"]]];
[f release];

int rangeForUrl;

if(myNumber != nil) {
    rangeForUrl = [myNumber intValue];
} else {
    rangeForUrl = 10;
}

// get uniqe device ID
UIDevice *getdev = [UIDevice currentDevice];
NSString *uniqueIdentifier = [getdev uniqueIdentifier];

NSString *deviceId  = [NSString stringWithFormat:@"IOS-%@", uniqueIdentifier];

// get the unix timestamp
NSDate * past = [NSDate date];
NSTimeInterval oldTime = [past timeIntervalSince1970]; 
NSString * unixTime = [[[NSString alloc] initWithFormat:@"%0.0f", oldTime] autorelease];


// send the data with a post request to the API
HttpRequest *data = [[HttpRequest alloc] init];
data.postData   = [NSString stringWithFormat:@"&device=%@&age=%@&gender=%@&latitude=%@&longitude=%@&timestamp=%@", deviceId, [settingsArray objectForKey:@"age"], [settingsArray objectForKey:@"gender"], @"00", @"00", unixTime];

data.controller = @"sendDevice";

NSString *url   = [NSString stringWithFormat:@"http://www..eu/api/send_device"];

[data loadHostName:url];

[data release];

//NSLog(@"string s: %@", data.postData);

}
开发者_StackOverflow中文版

This is the memory leak according to Xcode instruments => leaks:

Leaked Object # Address Size Responsible Library Responsible Frame NSCFString, 0x16cb40 144 Bytes Foundation -[NSPlaceholderString initWithFormat:locale:arguments:]

This is the line with "data.postData = ..." in my code. Can someone help me out?

this is how I use postData in the class httpRequest:

- (void)loadHostName:(NSString *)hostName {



responseData = [[NSMutableData alloc] init];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", hostName]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

if(authString) {
    [request setValue:authString forHTTPHeaderField:@"Authorization"];
}

if([postData isKindOfClass:[NSString class]] && postData != @"") {

    NSData *dataToPost = [postData dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d",[dataToPost length]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
    [request setHTTPBody:dataToPost];

}

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection release];

}

with of course:

NSString *postData; @property (nonatomic, retain) NSString *postData;

and

@synthesize postData;


You need to release postData in dealloc.

looks like its fixed now. But i dont really understand how the dealloc works. I never did a alloc/init on postData. Does this mean every object in my .h file like postData need to be released in the dealloc in the .m file?

Speaking of properties, you need to release in dealloc all your properties not marked assign (along with any non-property instance variable you own). Non-assign properties own the object held by the backing instance variable so you need to relinquish ownership of it by sending it a release message in dealloc.

From "The Objective-C Programming Language":

Declared properties, along with the @synthesize directive, take the place of accessor method declarations; when you synthesize a property, the compiler creates accessor methods as needed. However, there is no direct interaction between property declaration and the dealloc method—properties are not automatically released for you. Declared properties do, however, provide a useful way to cross-check the implementation of your dealloc method: you can look for all the property declarations in your header file and make sure that object properties not marked assign are released, and those marked assign are not released.

I would also recommend you to read the "Memory Management Programming Guide".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜