【问题标题】:Potential leak on a retain保留上的潜在泄漏
【发布时间】:2011-12-02 02:35:06
【问题描述】:

我正在开发一个 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];
}

我收到了:potential leak on an object allocated on line 52 and stored into 'connection'

这个类的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 吗?

【问题讨论】:

    标签: iphone objective-c memory-leaks


    【解决方案1】:
     NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    

    [connection release]; 在哪里? 例如,将此行放在[authURL release]; 之后。

    【讨论】:

      【解决方案2】:

      您的代码启动了一个异步请求,但您尝试访问返回的数据,然后打算立即释放连接。您需要实现您可能拥有的委托方法,以便能够读取返回的数据,并在已完成和失败的委托方法中释放连接。您拥有的代码将适用于快速网络连接(可能),但在现实世界中会失败。

      【讨论】:

      • 感谢您的回答。你能和我分享一个关于这样做的例子吗?我是 Objective-C 编程的新手(如您所见),我不知道该怎么做。
      • 参见 NSURLConnection 文档的委托部分,以及文档中链接的 SimpleURLConnections 或 LazyTableImages 示例代码。
      猜你喜欢
      • 1970-01-01
      • 2011-02-07
      • 2013-02-09
      • 2011-11-14
      • 2015-07-25
      • 2013-01-18
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      相关资源
      最近更新 更多