iDevGames Forums
Problem with CFDataGetBytePtr - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: Problem with CFDataGetBytePtr (/thread-1852.html)



Problem with CFDataGetBytePtr - Xavier - Jan 26, 2009 08:44 AM

Hi,

I'm actually developing a camera-based application.
Once I have taken a picture, I want to change its pixel data.

In my "TransformPicture" method:

Code:
CGImageRef oldImage = m_ImageRef; // m_ImageRef is the picture taken with the camera
    CFDataRef oldData = CGDataProviderCopyData(CGImageGetDataProvider(oldImage));    
    UInt8 * pixelData = (UInt8 *) CFDataGetBytePtr(oldData);    
    int length = CFDataGetLength(oldData);

    // Modify pixelData...

    [(id)oldImage autorelease];
    free(pixelData);
    CFRelease(oldData);

First time I'm passing through this method, everything is fine. I can turn my picture into grayscale.
But the second I'm passing through it, "CFDataGetBytePtr" returns a BAD_ACCESS error.

I'm not sure of what I'm doing wrong, maybe because it returns a const UInt8 * ?

Thanks


Problem with CFDataGetBytePtr - ThemsAllTook - Jan 26, 2009 08:55 AM

Xavier Wrote:I'm not sure of what I'm doing wrong, maybe because it returns a const UInt8 * ?
Quite possibly. Modifying data marked by the API as const is never safe. It's the library's way of saying to you "this data is intended to be immutable, so any changes to it are likely to break something".

[(id)oldImage autorelease] is also highly suspect. I wouldn't expect that to necessarily do what you intended it to; as far as I can tell, CGImageRef isn't toll-free bridged with anything in Cocoa. I could definitely see this appearing to work and then causing things to break later, as you're observing.


Problem with CFDataGetBytePtr - SethWillits - Jan 26, 2009 01:30 PM

Any CF type can be released/retained/autoreleased.

Also you should definitely not be freeing the result of CFDataGetBytePtr. Stop doing that, and don't modify it in place.


Problem with CFDataGetBytePtr - ThemsAllTook - Jan 26, 2009 01:36 PM

FreakSoftware Wrote:Any CF type can be released/retained/autoreleased.

Neat! Good to know.


Problem with CFDataGetBytePtr - Xavier - Jan 27, 2009 01:44 AM

Well I'm using a buffer now instead of modifying the pointer, and it's working fine.
Thanks !