NSString 内存泄漏
我在寻找 Objective 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 Instruments => 这是内存泄漏leaks:
泄漏对象 # 地址大小 负责库 负责框架 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(非原子,保留)NSString *postData;
和
@synthesize postData;
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=%@×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);
}
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在
dealloc
中释放postData
。说到属性,您需要在
dealloc
中释放所有未标记为分配
的属性(以及您拥有的任何非属性实例变量)。非分配
属性拥有由支持实例变量持有的对象,因此您需要通过在dealloc中向其发送release
消息来放弃它的所有权。来自 “Objective-C 编程语言”:
我还建议您阅读 “内存管理编程指南”。
You need to release
postData
indealloc
.Speaking of properties, you need to release in
dealloc
all your properties not markedassign
(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 arelease
message in dealloc.From "The Objective-C Programming Language":
I would also recommend you to read the "Memory Management Programming Guide".