对我从事的一个项目的所有 api 调用使用 LRResty。我注意到该项目经常泄漏内存,因此我得到了修复所有泄漏并提高当前应用程序稳定性的工作。
下面是场景: 整个项目在ARC下,但是LRResty是非arc的。视图控制器初始化将 self 作为委托传递的“CustomService”对象。CustomService 对象调用处理请求并返回响应的 LRResty 客户端。CustomService 具有属性“弱”的私有属性请求。
问题是: 当我运行仪器(在 iphone 或模拟器上)时,在属性请求属性为“弱”和“强”的两种情况下都会检测到泄漏。请求 (LRRestyRequest) 很弱(在这种情况下 customService dealloc 被调用)否则 customService 会陷入保留周期并且不会调用 dealloc。使用“弱”属性,泄漏是“malloc”,我无法识别来源,但是如果我将属性更改为“强”,“仪器”会在 initWithDelegate:self 和 [customService serviceScheduleRequest] 处显示泄漏,以及 customService 的 dealloc永远不会被调用。
如何使此代码无泄漏?我花了几个小时,我不知道。LRResty 框架是否有可能负责该内存问题?有人有使用这个框架的经验吗?
我的视图控制器
CustomService *customService = [[CustomService alloc] initWithDelegate:self];
[customService serviceScheduleRequest];
定制服务.h
@protocol CustomServiceDelegate;
@interface CustomService : Service <LRRestyClientResponseDelegate>
@property (nonatomic, weak) id<CustomServiceDelegate> delegate;
- (id) initWithDelegate:(id)aDelegate;
- (void)serviceScheduleRequest;
@end
@protocol CustomServiceDelegate <NSObject>
@optional
- (void) serviceScheduleRequestDidStart;
- (void) serviceScheduleRequestDidEndWithSuccessJSON:(id) JSON;
- (void) serviceScheduleRequestDidEndWithSuccess:(BOOL) status;
- (void) serviceScheduleRequestDidEndWithError:(NSError *)error;
@end
定制服务.m
@interface CustomService ()
@property (nonatomic, weak) LRRestyRequest *request;
@end
@implementation CustomService
@synthesize delegate=_delegate;
- (id) initWithDelegate:(id)aDelegate
{
self = [super init];
if (self)
{
_delegate = aDelegate;
}
return self;
}
- (void)serviceScheduleRequest
{
NSMutableDictionary* parameters = [self getParams];
self.private_request = [[LRResty client] post:@"http://mylink.com/apicall"
payload:parameters
delegate:self];
}
- (NSMutableDictionary*) getParams
{
NSMutableDictionary* parameters = [super getParams];
return parameters;
}
- (void)restClient:(LRRestyClient *)client receivedResponse:(LRRestyResponse *)response
{
[self.delegate callSomeMethod];
// do something...
}
- (void) dealloc
{
NSLog(@"\n\n\n\n dealloc gets called \n\n\n\n");
}
@end
堆栈跟踪以防万一它给任何人线索
0 libsystem_c.dylib realloc
1 Foundation _NSMutableDataGrowBytes
2 Foundation -[NSConcreteMutableData appendBytes:length:]
3 Foundation -[NSConcreteMutableData appendData:]
4 MyApp -[LRURLRequestOperation connection:didReceiveData:] /Users/admin/.jenkins/jobs/LRResty Nightly Builds/workspace/Classes/LRURLRequestOperation.m:107
5 MyApp -[LRRestyRequest connection:didReceiveData:] /Users/admin/.jenkins/jobs/LRResty Nightly Builds/workspace/Classes/LRRestyRequest.m:177
6 Foundation ___NSURLConnectionDidReceiveData_block_invoke_0
7 Foundation __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke_0
8 Foundation -[NSURLConnectionInternalConnection invokeForDelegate:]
9 Foundation -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]
10 Foundation -[NSURLConnectionInternal _withActiveConnectionAndDelegate:]
11 Foundation _NSURLConnectionDidReceiveData
12 CFNetwork ___delegate_cacheTrifecta_block_invoke_0
13 CFNetwork ___withDelegateAsync_block_invoke_0
14 CFNetwork ___performAsync_block_invoke_068
15 CoreFoundation CFArrayApplyFunction
16 CFNetwork RunloopBlockContext::perform()
17 CFNetwork non-virtual thunk to RunloopBlockContext::multiplexerClientPerform()
18 CFNetwork MultiplexerSource::perform()
19 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
20 CoreFoundation __CFRunLoopDoSources0
21 CoreFoundation __CFRunLoopRun
22 CoreFoundation CFRunLoopRunSpecific
23 CoreFoundation CFRunLoopRunInMode
24 Foundation -[NSRunLoop(NSRunLoop) runMode:beforeDate:]
25 Foundation -[NSRunLoop(NSRunLoop) run]
26 MyApp -[LRURLRequestOperation start] /Users/admin/.jenkins/jobs/LRResty Nightly Builds/workspace/Classes/LRURLRequestOperation.m:63
27 MyApp -[LRRestyRequest start] /Users/admin/.jenkins/jobs/LRResty Nightly Builds/workspace/Classes/LRRestyRequest.m:136
28 Foundation __block_global_6
29 libdispatch.dylib _dispatch_call_block_and_release
30 libdispatch.dylib _dispatch_client_callout
31 libdispatch.dylib _dispatch_root_queue_drain
32 libdispatch.dylib _dispatch_worker_thread2
33 libsystem_c.dylib _pthread_wqthread
34 libsystem_c.dylib start_wqthread