Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3623944
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:26:46+00:00 2026-05-18T23:26:46+00:00

How do I (or can I even) pass a nil argument to an NSInvocation

  • 0

How do I (or can I even) pass a nil argument to an NSInvocation object?

I tried to do this:

NSMethodSignature* signature = [AClass instanceMethodSignatureForSelector:@selector(aMethod:theOtherArg:)];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature: signature];

[invocation setTarget: aTargetObj];
[invocation setSelector: @selector(aMethod:theOtherArg:)];

/* I've tried both this way */
AnObj* arg1 = nil;
AnotherObj* arg2 = nil;
[invocation setArgument: &arg1 atIndex:2];
[invocation setArgument: &arg2 atIndex:3];

/* and this way */
//[invocation setArgument: nil atIndex:2];
//[invocation setArgument: nil atIndex:3];

NSInvocationOperation* operation = [[NSInvocationOperation alloc] initWithInvocation:invocation];
//opQueue is an NSOperationQueue object
[opQueue addOperation:operation];

The first approach will crash with this message:

Thread 0 Crashed:
0   libSystem.B.dylib              0x927c1f10 strlen + 16
1   com.apple.CoreFoundation       0x92dd1654 __NSI3 + 500
2   com.apple.CoreFoundation       0x92dd1994 -[NSInvocation retainArguments] + 132
3   com.apple.Foundation           0x96a50c5e -[NSInvocationOperation initWithInvocation:] + 78

The second approach will crash with this message:

Error: Exiting due to caught object *** -[NSInvocation setArgument:atIndex:]: NULL address argument

Thanks in advance for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:26:46+00:00Added an answer on May 18, 2026 at 11:26 pm

    Yes, you can pass nil arguments. More precisely, you can pass a valid pointer whose contents are nil.

    I haven’t been able to reproduce your particular problem. Here’s the code I’ve used to test it. There’s probably something else in your program that’s causing the error.

    #import <Foundation/Foundation.h>
    
    @interface Person : NSObject {
        NSString *name;
        NSUInteger age;
    }
    - (void)setName:(NSString *)personName age:(NSNumber *)personAge;
    @end
    
    @implementation Person
    - (void)setName:(NSString *)personName age:(NSNumber *)personAge {
        NSLog(@"setName:%@ age:%@", personName, personAge);
        name = [personName copy];
        age = [personAge unsignedIntegerValue];
    }
    @end
    
    int main() {
        NSAutoreleasePool *pool = [NSAutoreleasePool new];            
        Person *person = [Person new];
    
        SEL sel = @selector(setName:age:);
    
        NSMethodSignature *sig = [Person instanceMethodSignatureForSelector:sel];
        NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
    
        [inv setTarget:person];
        [inv setSelector:sel];
    
        NSString *name = nil;
        NSNumber *age = nil;
    
        [inv setArgument:&name atIndex:2];
        [inv setArgument:&age atIndex:3];
    
        // [inv retainArguments];
        // [inv invoke];
    
        NSInvocationOperation *op = [[[NSInvocationOperation alloc] initWithInvocation:inv] autorelease];
        NSOperationQueue *queue = [NSOperationQueue new];
        [queue addOperation:op];
    
        [pool release];
        return 0;
    }
    

    I’ve also tested it without using NSInvocationOperation and sending -retainArguments directly, since that’s what seems to be triggering your error.

    For the record, your second approach won’t work since -[NSInvocation setArgument:atIndex:] expects a valid memory address pointing to a buffer which will be copied. In the case of object arguments, the buffer must contain the address of the object. The address of the object can be zero but the address of the buffer must be valid.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Surprisingly as you get good at vim, you can code even faster than standard
Thinking about my other problem , i decided I can't even create a regular
I want to write Html format, but I can not even get a simple
Is it a measure of anything that a developer or even manager can look
How can I check if a given number is even or odd in C?
How can I configure an application, or even an entire machine, to use either
I can't find documentation for it, even on MSDN...
I can't seem to apply a tax value on the cart total value even
I can't seem to get it to work. Perhaps I'm not even testing it
We know that a single application instance can use multiple cores and even multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.