• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSInvocation driving me nuts
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSInvocation driving me nuts


  • Subject: Re: NSInvocation driving me nuts
  • From: Ondra Cada <email@hidden>
  • Date: Sat, 8 Apr 2006 14:59:53 +0200

Pierre,

On 8.4.2006, at 13:24, Pierre Bernard wrote:

Can you spot the difference between:

[delegate _managedObjectContextEditor:self didCommit:YES contextInfo:contextInfo];

and this:

SEL selector = @selector (_managedObjectContextEditor:didCommit:contextInfo:);
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[delegate methodSignatureForSelector:selector]];
BOOL success = YES;

[invocation setTarget:delegate]; // 0
[invocation setSelector:selector]; // 1
[invocation setArgument:&self atIndex:2];
[invocation setArgument:&success atIndex:3];
[invocation setArgument:&contextInfo atIndex:4];
[invocation retainArguments];

In this case retaining arguments seems superfluous, since the invocation self-evidently is never stored.


[invocation invoke];

This should work all right.

[invocation release];

This should crash as soon as the current autorelease pool (which contains the already released invocation) gets released.


Check this:

23 /tmp> <q.m
#import <Cocoa/Cocoa.h>

@interface Delegate:NSObject @end
@implementation Delegate
-(void)_managedObjectContextEditor:editor didCommit:(BOOL)dc contextInfo:(void*)ci {
NSLog(@"delegate method called all right with %@/%d/% x",editor,dc,ci);
}
@end
int main() {
SEL sel=@selector (_managedObjectContextEditor:didCommit:contextInfo:);
id p=[NSAutoreleasePool new],del=[Delegate new],inv=[NSInvocation invocationWithMethodSignature:[del methodSignatureForSelector:sel]];
BOOL succ=YES;
[inv setTarget:del];
[inv setSelector:sel];
[inv setArgument:&del atIndex:2]; // have no self here, of course
[inv setArgument:&succ atIndex:3];
[inv setArgument:&sel atIndex:4]; // why not, any address does
[inv retainArguments]; // superfluous, harmless
NSLog(@"delegate method about to be called now");
[inv invoke];
[inv release]; // overreleasing here
NSLog(@"overrelease crash about to happen now");
[p release];
NSLog(@"never seen");
return 0;
}
24 /tmp> cc -Wall q.m -framework Cocoa && ./a.out
2006-04-08 14:58:59.551 a.out[1126] delegate method about to be called now
2006-04-08 14:58:59.552 a.out[1126] delegate method called all right with <Delegate: 0x504240>/1/940a5170
2006-04-08 14:58:59.552 a.out[1126] overrelease crash about to happen now
zsh: bus error ./a.out
25 /tmp>


---
Ondra Čada
OCSoftware:     email@hidden               http://www.ocs.cz
private         email@hidden             http://www.ocs.cz/oc


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: NSInvocation driving me nuts
      • From: Pierre Bernard <email@hidden>
References: 
 >NSInvocation driving me nuts (From: Pierre Bernard <email@hidden>)

  • Prev by Date: Re: NSInvocation driving me nuts
  • Next by Date: Running an application owned by a different user
  • Previous by thread: Re: NSInvocation driving me nuts
  • Next by thread: Re: NSInvocation driving me nuts
  • Index(es):
    • Date
    • Thread