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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:53:34+00:00 2026-05-29T16:53:34+00:00

I’m using NSInvocation to get some method returns, and unfortunately I seem to have

  • 0

I’m using NSInvocation to get some method returns, and unfortunately I seem to have a leak, but can’t figure out how to free the void* I’m allocating, after I’ve returned it from NSInvocation.

In the following implementation I tried to free it with a block that gets performed on the next run loop, but I get a crash due to returnBuffer not being allocated.

Why can’t I free returnBuffer in the block, and if it hasn’t been allocated why is it getting through returnBuffer!=NULL?

This is a special method that has to do with IMP swizzling so I DON’T know the method return type. Putting it in NSData or something will not work.

NSUInteger length = [[invocation methodSignature] methodReturnLength];
if(length!=0){
    void* returnBuffer = (void *)malloc(length);
    [invocation getReturnValue:&returnBuffer];
    if(returnBuffer!=NULL){
        void(^delayedFree)(void) = ^{ free(returnBuffer); };
        [[NSOperationQueue mainQueue] addOperationWithBlock:delayedFree];
    }
    return returnBuffer;
}
return nil;

ANSWER
Got it to work the following way thanks to Josh’s -[NSMutableData mutableBytes] trick

 NSUInteger length = [[invocation methodSignature] methodReturnLength];
if(length!=0){
    NSMutableData * dat = [[NSMutableData alloc] initWithLength:length];
    void* returnBuffer =  [dat mutableBytes];
    [invocation getReturnValue:&returnBuffer];
    void(^delayedFree)(void) = ^{ [dat release]; };
    [[NSOperationQueue mainQueue] addOperationWithBlock:delayedFree];
    return returnBuffer;
}
return nil;
  • 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-29T16:53:36+00:00Added an answer on May 29, 2026 at 4:53 pm

    You can get a void * from NSMutableData, just like you can from malloc(), and essentially turn it into an instance variable, so that the lifetime of the allocation is tied to the lifetime of the instance. I got this trick from Mike Ash. I’ve been doing some NSInvocation monkeying myself lately; this is in a category on NSInvocation (you should use your own prefix for the method, of course):

    static char allocations_key;
    - (void *) Wool_allocate: (size_t)size {
        NSMutableArray * allocations = objc_getAssociatedObject(self, 
                                                                &allocations_key);
        if( !allocations ){
            allocations = [NSMutableArray array];
            objc_setAssociatedObject(self, &allocations_key, 
                                     allocations, OBJC_ASSOCIATION_RETAIN);
        }
    
        NSMutableData * dat = [NSMutableData dataWithLength: size];
        [allocations addObject:dat];
    
        return [dat mutableBytes];
    }
    

    I’m not sure where the code you’ve posted is located; if you’re in your own custom class, you don’t need to deal with the associated objects bits — just make allocations an ivar.

    Tying this into your code:

    NSUInteger length = [[invocation methodSignature] methodReturnLength];
    if(length!=0){
        void* returnBuffer = [self Wool_allocate:length];
        [invocation getReturnValue:&returnBuffer];
        return returnBuffer;
    }
    return nil;
    

    If you use the associated object route, the allocations array will be deallocated when this instance is. Otherwise, just put [allocations release] into your dealloc.

    Also, to answer your question as posed, rather than just solving the problem: free() won’t operate on any pointer that you didn’t get from malloc(). When the pointer gets used in the Block, I’m pretty sure it gets copied, so you end up with another pointer — still pointing to the same memory, but one that free() doesn’t think it owns. Thus, you get the error about not having allocated it.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.