本文介绍了固定件上的潜在泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发iPhone应用程序.我有以下方法:
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];
}
我得到一个:potential leak on an object allocated on line 52 and stored into 'connection'
.
此类的Dealloc方法是:
Dealloc method for this class is:
- (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吗?
How can I fix this problem? May I create a copy to that object, and then assign it to responseData?
推荐答案
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[connection release];
在哪里?例如,将此行放在[authURL release];
之后.
Where is [connection release];
?Put this line after [authURL release];
for example.
这篇关于固定件上的潜在泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!