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 6246531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:42:30+00:00 2026-05-24T12:42:30+00:00

I try to pass CGRect to NSInvocation (setArgument:atIndex:). I wrap it by NSValue, push

  • 0

I try to pass CGRect to NSInvocation (setArgument:atIndex:). I wrap it by NSValue, push to the NSArry,then get from NSArray and use NSValue (getValue:). Calling of (getValue:) method causes the changing of before declared index NSInteger i. Can anybody say why does this happen?

 NSString className = @"UIButton";
    Class cls = NSClassFromString(className);
    cls pushButton5 = [[cls alloc] init];
    CGRect rect =CGRectMake(20,220,280,30);
    NSMethodSignature *msignature1;
    NSInvocation *anInvocation1;

    msignature1 = [pushButton5 methodSignatureForSelector:@selector(setFrame:)];
    anInvocation1 = [NSInvocation invocationWithMethodSignature:msignature1];

[anInvocation1 setTarget:pushButton5];
[anInvocation1 setSelector:@selector(setFrame:)];
NSValue* rectValue = [NSValue valueWithCGRect:rect];
NSArray *params1;
params1= [NSArray arrayWithObjects:rectValue,nil];
id currentVal = [params1 objectAtIndex:0];
NSInteger i=2;
if ([currentVal isKindOfClass:[NSValue class]]) {
    void *bufferForValue;
    [currentVal getValue:&bufferForValue];
    [anInvocation1 setArgument:&bufferForValue atIndex:i];
}else {
    [anInvocation1 setArgument:&currentVal atIndex:i];
}
[anInvocation1 invoke];

When this(getValue:) method implements value of ‘i’ changes from 2 to something like : 1130102784, and in (setArgument:atIndex:) I have an SIGABRT because index i is out of bounds.

So why does [NSValue getValue:(*void) buffer] change other variables?

(P.S. I do it in function so I simplified an example and initialize array directly.
And if I set directly atIndex:2, It works perfect.But as I’ve sad I simplified a little, and I need to pass i to atIndex:)


Thanks to Tom Dalling(especially) and sergio problem solved. I delete this:

void *bufferForValue;
[currentVal getValue:&bufferForValue];
[anInvocation1 setArgument:&bufferForValue atIndex:i];

and paste this

NSUInteger bufferSize = 0;
NSGetSizeAndAlignment([currentVal objCType], &bufferSize, NULL);
void* buffer = malloc(bufferSize);
[currentVal getValue:buffer];
[anInvocation1 setArgument:buffer atIndex:i];

Thank you. Stackoverflow.com really helpful site with smart people.

  • 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-24T12:42:31+00:00Added an answer on May 24, 2026 at 12:42 pm

    You can’t fit an NSRect into a void*. The correct code would be:

    NSRect buffer;
    [currentVal getValue:&buffer];
    

    Or alternatively, if you don’t know the contents of the NSValue object:

    NSUInteger bufferSize = 0;
    NSGetSizeAndAlignment([currentVal objCType], &bufferSize, NULL);
    void* buffer = malloc(bufferSize);
    [currentVal getValue:buffer]; //notice the lack of '&'
    //do something here
    free(buffer);
    

    The value for i is changing because your code causes an overflow into other stack-allocated variables.

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

Sidebar

Related Questions

If you try to pass the username/password to OpenVPN from a file via the
I have a get-parameter with name controller . When I try to pass it
I try to pass some value from one page to another but at the
I try to pass a NSString from one view to an other view, but
When I try and pass data to my view from a controller using ViewData
It works if I use loadMovie(http://graph.facebook.com/100000108805716/picture, imageLoader2); but as I try to pass that
If I try to pass data from one activity to another, but before going
What is the difference between ',' and 'as' in except statements, eg: try: pass
My compiler behaves oddly when I try to pass a fixed-size array to a
I'm working on a program where I try to pass parameters by reference. I'm

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.