Problem with CFDataGetBytePtr

Apprentice
Posts: 9
Joined: 2008.07
Post: #1
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
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
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.
Quote this message in a reply
⌘-R in Chief
Posts: 1,237
Joined: 2002.05
Post: #3
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.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #4
FreakSoftware Wrote:Any CF type can be released/retained/autoreleased.

Neat! Good to know.
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2008.07
Post: #5
Well I'm using a buffer now instead of modifying the pointer, and it's working fine.
Thanks !
Quote this message in a reply
Post Reply