1

In below function I am trying to return UIImage pointer which is created from NSData

  1. When I give option freeWhenDone = YES the UIImage displayed returned from here is white image.
  2. When I give option freeWhenDone = NO

    • UIImage returned form here shows it as a black image.
    • When I pass rgb_buffer (char array) instead of [rgbData bytes] function1 and function2 work properly. Everything is fine.

Is it something to do with ARC?

Myfunction
{
    char *pu1_out_buffer = malloc(length);
    int width, height, stride;
    char *rgb_buffer = malloc(BUFFER_LENGTH);
pu1_out_buffer = datafromfile(FILE_PATH)  // initialized with some data , not important

/* rgb NSdata created from malloced rub buffer */
    NSMutableData *rgbData = [NSMutableData dataWithBytesNoCopy:rgb_buffer
                                                     length:(u4_stride * u4_height * 3)
                                               freeWhenDone:YES];
[self function1:pu1_out_buffer
            rgb_buffer:(UWORD16 *)[rgbData bytes]
                        …]

    free(pu1_out_buffer); 
    UIImage *outUIImage  = [self function2:rgbData          
                                        width:u4_width
                                       height:u4_height
                                       stride:u4_stride];

    return outUIImage;
}
Amar
  • 13,202
  • 7
  • 53
  • 71
thar_bun
  • 820
  • 1
  • 5
  • 20
  • Out of curiosity: why do you need to use dynamic memory allocation (malloc's)? – Arek Holko Oct 15 '13 at 13:31
  • 1
    It is impossible to answer why your image is white or black without knowing `function1`, `function2`. – Martin R Oct 15 '13 at 13:46
  • Unrelated but `pu1_out_buffer = datafromfile(FILE_PATH) ` = invalid syntax and a memory leak. `MyFunction {` is also invalid unless a macro, please post a real example. – Joe Oct 15 '13 at 13:49
  • Also why would you `malloc` the size of `BUFFER_LENGTH` but lie to `NSMutableData` and tell it `(u4_stride * u4_height * 3)`? – Joe Oct 15 '13 at 13:50
  • 1
    `rgb_buffer` is freed, which has been explained in your previous question http://stackoverflow.com/questions/19380363/problems-with-nsmutabledata-byteswithnocopy or its possible duplicate http://stackoverflow.com/questions/18770265/nsmutabledata-datawithbytesnocopylengthfreewhendone-seems-to-make-a-copy-of-t. So it is unlikely that using `rgb_buffer` instead of `[rgbData bytes]` works "better". – Martin R Oct 15 '13 at 13:56

1 Answers1

1

There are a handful of issues with this code.

char *pu1_out_buffer = malloc(length);
pu1_out_buffer = datafromfile(FILE_PATH)  // initialized with some data , not important

That leaks the original malloc.

[self function1:pu1_out_buffer
        rgb_buffer:(UWORD16 *)[rgbData bytes]
                    …]

That method should be something like: function1:rgbBuffer:...

As for the crash, it is most likely for the reason Martin cites. If you have ARC enabled, you are grabbing an interior pointer from the NSMutableData instance. ARC can't associate the return value of bytes with the original data, assumes the data object is no longer being used, and releases it.

To fix, add:

[rgbData bytes] right before return outUIImage;. That will let ARC know that the object is in use through the duration of the function2:width:height:stride: call.

bbum
  • 162,346
  • 23
  • 271
  • 359