【发布时间】:2011-10-31 06:08:56
【问题描述】:
我在发现目标 c 中的内存泄漏以及如何修复它们方面有点新奇。我知道如何使用 alloc/init/copy 和 release/retain 但是(至少我是这么认为的 :-))但是我的 IOS 应用程序中有一些奇怪的内存泄漏。
-(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=%@×tamp=%@", 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);
}
这是根据 Xcode 仪器的内存泄漏 => 泄漏:
Leaked Object # Address Size 责任库责任框架 NSCFString, 0x16cb40 144 Bytes Foundation -[NSPlaceholderString initWithFormat:locale:arguments:]
这是我的代码中带有“data.postData = ...”的行。有人可以帮帮我吗?
这就是我在 httpRequest 类中使用 postData 的方式:
- (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];
}
当然:
NSString *postData; @property (nonatomic, 保留) NSString *postData;
和
@synthesize postData;
【问题讨论】:
-
你是如何声明
postData的? -
postData 是 httpRequest 类中的一个 NSString,带有 @property(nonatomic, retain) NSString *postData 并合成 postData。
-
@Melvin 你会在
dealloc中发布postData吗? -
@albertamg Thnx,现在看起来已经修复了。但我真的不明白 dealloc 是如何工作的。我从未对 postData 进行过分配/初始化。这是否意味着我的 .h 文件中的每个对象(如 postData)都需要在 .m 文件的 dealloc 中释放?有人知道关于这个主题的好教程吗?
-
@Melvin 我提供了一个答案。看看这个。我希望它有所帮助。
标签: objective-c ios memory-management memory-leaks nsstring