Search code examples
iosobjective-cxcodeuint8t

Passing uint8_t to another viewController EXC_BAD_ACCESS


Im using AVFoundation to take a still image from the camera in the kCVPixelFormatType_420YpCbCr8BiPlanarFullRange format. I get all the data from the buffer and i want to pass it to another view controller.

I have a uint8_t *yBuffer; and i pass this pointer to the delegate method (didCaptureImage:withYBuffer:) of the current controller.

The other controller sets its variable uint8_t *YBuffer to the one received from the delegate.

The problem is that after copying the YBuffer in the new controller, and i try to modify a value in the array, it gives me the error EXC_BAD_ACCESS (code=1, address=0x104e40010)

I tried debugging and it seems to me the YBuffer pointer in the new controller stores the YBuffer value from the delegate controller until the delegate is released from memory.

Value of YBuffer in new controller before delegate released: "/87/34/45/76..and so on"

Value of YBuffer in new controller after delegate released: "" its just empty quotes.

Maybe the problem is the way i copy the uint8_t array? Please someone guide me in the right direction. Thanks


Solution

  • Figured it out:

    otherController.yBuffer = malloc(inWidth * inHeight  * sizeof(uint8_t) ); //size you want to allocate
    
    memcpy(otherController.yBuffer, yBuffer, inWidth * inHeight  * sizeof(uint8_t)); //copy to target buffer from current buffer, with the size you allocated
    

    After you are done with the buffer and don't need it:

    free(self.cameraViewController.yBuffer);