gpt4 book ai didi

objective-c - NSString 泄漏内存

转载 作者:行者123 更新时间:2023-11-29 13:46:37 26 4
gpt4 key购买 nike

我在寻找 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=%@&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);

}

根据 Xcode instruments => 泄漏,这是内存泄漏:

泄漏对象# 地址大小 负责库 负责框架NSCFString,0x16cb40 144 字节基础 -[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, retain) NSString *postData;

@synthesize postData;

最佳答案

你需要在dealloc中释放postData

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?

说到属性,您需要在dealloc 中释放所有标记为assign 的属性(连同任何非属性实例变量自己的)。非assign 属性拥有支持实例变量持有的对象,因此您需要通过在 dealloc 中向它发送 release 消息来放弃它的所有权.

来自 "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.

我还建议您阅读 "Memory Management Programming Guide" .

关于objective-c - NSString 泄漏内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7132731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com