Malloc pointer being freed was not allocated error when calling initWithBitmapData
<p>When I create a CIImage by calling this routine I get the malloc error in the title.  When I call initWithBitmapData directly (not within the createCIUimageFromData routine) then it works fine.  </p>

<p>I have seen references to possible bugs in iOS that might be related, but I can't tell for sure and I certainly suspect my code more than Apple's!  </p>

<p>My guess is somehow my additional redirection is screwing things up, but it's cleaner to have the separate routine than to embed the code wherever I need it.</p>

<p>Thank you.</p>

<p>Fails:</p>

<pre><code>- (CIImage *) createCIimageFromData : (unsigned char *)pData   width : (int32_t)width height : (int32_t)height
{

    /*
     Once we have the raw data, we convert it into a CIImage.
     The following code does the required work.
     */
    NSLog(@"entering createciimage\n");
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();

    NSData *_pixelsData = [NSData dataWithBytesNoCopy:pData length:(sizeof(unsigned char)*width*height) freeWhenDone:YES ];
    CIImage *_dataCIImage = [[CIImage alloc] initWithBitmapData:_pixelsData bytesPerRow:(width*sizeof(unsigned char)) size:CGSizeMake(width,height) format:kCIFormatR8 colorSpace:colorSpaceRef];

    CGColorSpaceRelease(colorSpaceRef);

    /*
     newImage is the final image
     Do remember to release the allocated parts.
     */
    NSLog(@"done ciimage\n");
    return _dataCIImage;
}
</code></pre>

<p>Works:</p>

<pre><code>void prepData(unsigned char *pData,  // source-destination
                           int strideSrc, // stride
                           int width,
                           int height,
                           double amount,
                           int deltaLimit,
                           id owner)
{

   //[owner createCIimageFromData:pData width:width height:height];  // &lt;-- commented out
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();

    NSData *_pixelsData = [NSData dataWithBytesNoCopy:pData length:(sizeof(unsigned char)*width*height) freeWhenDone:YES ];
    CIImage *_dataCIImage = [[CIImage alloc] initWithBitmapData:_pixelsData bytesPerRow:(width*sizeof(unsigned char)) size:CGSizeMake(width,height) format:kCIFormatR8 colorSpace:colorSpaceRef];

    CGColorSpaceRelease(colorSpaceRef);

// . . .
}
</code></pre>