保留处可能存在泄漏
我正在开发一个 iPhone 应用程序。我有以下方法:
- (void)generateTokenWithUserId: (NSString *)uniqueId {
NSLog(@"TokenGenerator - generateTokenWithUserId: '%@'", uniqueId);
NSString *myMD5String = [Utilities returnMD5Hash:uniqueId];
// Create the request.
NSMutableString *authURL = [[NSMutableString alloc] initWithString:CLOUDMADE_AUTH];
[authURL appendString: localApiKey];
[authURL appendString: USER_ID];
[authURL appendString: myMD5String];
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:authURL]
cachePolicy: NSURLRequestUseProtocolCachePolicy
timeoutInterval: 60.0];
[theRequest setHTTPMethod: @"POST"];
NSLog(@"TokenGenerator URL - '%@'", authURL);
// create the connection with the request
// and start loading the data
//NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (connection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
responseData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
[authURL release];
}
我收到:第 52 行分配并存储到“连接”中的对象存在潜在泄漏
。
该类的 Dealloc 方法是:
- (void) dealloc {
NSLog(@"TokenGenerator - dealloc");
[responseData release];
[localApiKey release];
[super dealloc];
}
接口声明是:
@class TokenGenerator;
@protocol TokenGeneratorDelegate <NSObject>
- (void)tokenGeneratorDidFinishGeneration:(TokenGenerator *)tokenGenerator token:(NSString *)tokenGenerated;
@end
@interface TokenGenerator : NSObject {
NSString *localApiKey;
id<TokenGeneratorDelegate> delegate;
NSMutableData *responseData;
}
@property (nonatomic, assign) id<TokenGeneratorDelegate>delegate;
- (id) initWithApikey:(NSString*) apiKey delegate:(id)anObject;
- (void)generateTokenWithUserId: (NSString *)uniqueId;
@end
如何解决这个问题?我可以创建该对象的副本,然后将其分配给responseData吗?
I'm developing an iPhone application. I have the following method:
- (void)generateTokenWithUserId: (NSString *)uniqueId {
NSLog(@"TokenGenerator - generateTokenWithUserId: '%@'", uniqueId);
NSString *myMD5String = [Utilities returnMD5Hash:uniqueId];
// Create the request.
NSMutableString *authURL = [[NSMutableString alloc] initWithString:CLOUDMADE_AUTH];
[authURL appendString: localApiKey];
[authURL appendString: USER_ID];
[authURL appendString: myMD5String];
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:authURL]
cachePolicy: NSURLRequestUseProtocolCachePolicy
timeoutInterval: 60.0];
[theRequest setHTTPMethod: @"POST"];
NSLog(@"TokenGenerator URL - '%@'", authURL);
// create the connection with the request
// and start loading the data
//NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (connection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
responseData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
[authURL release];
}
I'm getting a: potential leak on an object allocated on line 52 and stored into 'connection'
.
Dealloc method for this class is:
- (void) dealloc {
NSLog(@"TokenGenerator - dealloc");
[responseData release];
[localApiKey release];
[super dealloc];
}
And interface declaration is:
@class TokenGenerator;
@protocol TokenGeneratorDelegate <NSObject>
- (void)tokenGeneratorDidFinishGeneration:(TokenGenerator *)tokenGenerator token:(NSString *)tokenGenerated;
@end
@interface TokenGenerator : NSObject {
NSString *localApiKey;
id<TokenGeneratorDelegate> delegate;
NSMutableData *responseData;
}
@property (nonatomic, assign) id<TokenGeneratorDelegate>delegate;
- (id) initWithApikey:(NSString*) apiKey delegate:(id)anObject;
- (void)generateTokenWithUserId: (NSString *)uniqueId;
@end
How can I fix this problem? May I create a copy to that object, and then assign it to responseData?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
[连接释放];
在哪里?例如,将此行放在
[authURL release];
之后。Where is
[connection release];
?Put this line after
[authURL release];
for example.您的代码启动异步请求,但您尝试访问返回的数据,然后打算立即释放连接。您需要实现委托方法,您可能拥有这些方法,以便能够读取返回的数据,并在已完成和失败的委托方法中释放连接。您拥有的代码(可能)可以在快速网络连接下运行,但在现实世界中会失败。
Your code starts an asynchronous request, but you try to access the returned data and then intend to release the connection immediately. You need to implement the delegate methods, which you presumably have in order to be able to read the returned data, and release the connection in the finished and failed delegate methods. The code you have will work with fast network connections (possibly), but will fail in the real world.