内存泄漏“弱”和“强”属性

时间:2013-05-02 11:35:51

标签: ios objective-c memory-leaks instruments

在我工作的项目之一上使用LRResty进行所有api调用。我注意到该项目泄漏了很多内存,所以我已经得到了解决所有泄漏并改善当前应用程序稳定性的工作。

以下是该方案: 整个项目都在ARC下,但LRResty是非弧形的。 视图控制器初始化将自身作为委托传递的“CustomService”对象。 CustomService对象调用处理请求并返回响应的LRResty客户端。 CustomService具有属性为“weak”的私有属性请求。

问题是: 当我运行仪器(在iphone或模拟器上)时,在属性请求属性为“弱”和“强”的两种情况下都会检测到泄漏。请求(LRRestyRequest)很弱(在这种情况下会调用customService dealloc),否则customService会在保留周期中被捕获,并且不会调用dealloc。对于属性“weak”,泄漏是“malloc”而我无法识别源,但是如果我将属性更改为“strong”,则“Instruments”会在initWithDelegate上显示泄漏:self和[customService serviceScheduleRequest],以及customService的dealloc永远不会被称为。

如何使此代码无泄漏?我花了几个小时,我不知道。 LRResty框架是否可能负责该内存问题?任何人都有使用此框架的经验?

MyViewController

CustomService *customService = [[CustomService alloc] initWithDelegate:self]; 
[customService serviceScheduleRequest];

CustomService.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

CustomService.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

0 个答案:

没有答案
相关问题