2009-10-26 5 views
1
-(void)invokeMethod 
{ 
    NSMethodSignature * sig = [[source class] instanceMethodSignatureForSelector:@selector(mySelector:)]; 
    NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:sig]; 

    [invocation setTarget:myTarget]; 
    [invocation setSelector:@selector(mySelector:)]; 

    MySubClassOfNSInvocationOperation * command = [[[MySubClassOfNSInvocationOperation alloc] initWithInvocation:invocation] autorelease]; 

    //setArgument retains command 
    [invocation setArgument:&command atIndex:2]; 

    //addOperation retains command until the selector is finished executing 
    [operationQueue addOperation:command]; 
} 


-(void)mySelector:(MySubClassOfNSInvocation*)command 
{ 
    //Do stuff 
} 

정확히 무슨 일이 일어나고 있는지 모르겠지만 NSInvocation & MySubClassOfNSInvocationOperation 누수가 있습니다.이 메모리 누출을 어떻게 해결할 수 있습니까? NSInvocation

누수가 없으므로 통과와 관련된 문제점이 있습니다. 명령을 인수로 사용하십시오.

답변

2

당신은 아마 레퍼런스 카운트 루프 ... commandinvocation을 유지하지 않고 invocationcommand을 유지하고 어느 쪽도 자신의 dealloc 방법까지 해제를 원하는 상황이있다.

두 계층 중 어느 계층이 다른 상위 계층보다 우선 순위가 높고 하위 개체가 상위를 유지하지 않는지 확인해야합니다. 덧붙여서 NSInvocationretainArguments을 호출하지 않는 한 인수를 유지하지 않습니다. 또는 close 메서드를 구현하여 수동으로 다른 하나를 해제하라는 메시지를 표시하고주기를 위반할 수 있습니다.

내 자신의 프로젝트 중 하나에서 NSInvocation와 (과) 정확한 문제를 발견 한 후 게시물을 작성했습니다 (Rules to avoid retain cycles).

+0

완벽. 감사 –

-1

setArgument 메서드는 버퍼 (이 경우에는 명령 개체)를 유지합니다. 설정 후 명령을 해제 할 수 있습니다. 그러나 당신은주의해야한다). 내 친구는 새로운 아이폰 OS에서 실행되지 않을 때 혼란스러워했다. 왜냐하면 그는 한 줄에 추가 릴리스 메시지를 추가하여 애플의 누수를 바로 잡았 기 때문이다.

[invocation setArgument:&command atIndex:2]; 

당신은 포인터에-A-전달되고 있습니다 애플 새 OS에서 보정을했을 때 그리고,이 라인이 라인에 추가 앰퍼샌드 무엇 응용 프로그램)

+0

응답 해 주셔서 감사합니다.하지만 시도해 보니 dealloc'd가되고 충돌이 발생합니다. –

-2

충돌의 이유가 있었다 명령의 포인터. 그건 나에게 잘못된 것처럼 보인다. 그들은 결코 해제되지받을 상황에 선도 -

+1

아니요 - 맞습니다. http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSInvocation_Class/Reference/Reference.html#//apple_ref/occ/instm/NSInvocation/setArgument:atIndex : –

+0

구체적으로 "인수 값이 객체 일 때 객체를 복사해야하는 변수 (또는 메모리)에 대한 포인터를 전달하십시오." –

+0

나쁘다 - 교정되었습니다. – JBRWilkinson

관련 문제