Search code examples
c++objective-cobjective-c++

EXC_BAD_ACCESS uint32_t


I am attempting to implement an extension method on NSData in Obj C. The extension method calls a cpp lib method.

Here is the method signature of the cpp method:

void process(char *result) { }

Here is the extension method calling the cpp method:

- (NSData *)PROCESS
{
    uint32_t result;

    process((char*)&result);

    // At this point result shows 2949384954 as the value

    NSData *d = [NSData dataWithBytes:&result length:sizeof(result)];

    return d;
}

When I use PROCESS and assign the returned (NSData*), I get an EXC_BAD_ACCESS error. I'm sure this has to do with improper use of pointers or references, but I just can't figure it out. Thanks in advance.


Solution

  • The code following code works fine:

    uint32_t result = 2949384954;
    NSData *d = [NSData dataWithBytes:&result length:sizeof(result)];
    NSLog(@"d: %@", d);
    

    NSLog output:

    d: <fa0accaf>  
    

    There is probably an issue with the way the method is called.

    Back to "extension method ". What do you mean by that? Just make this a function to add it as a method to a class.