本文介绍了UInt8 EXC_BAD_ACCESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法,将添加一个过滤器的图像。这工作得很好,直到几个月前,现在当我尝试使用这种方法的应用程序将崩溃的图像缓冲区。我创建缓冲区并将其设置为图像的数据,稍后访问特定索引会导致访问崩溃。我已经找了一两个小时,现在我确信有一些可以忽略的东西。我觉得有些东西正在被释放,不应该是。我正在使用xcode的ios DP 4预览版,我认为这个问题是从测试版的更新开始的,但我真的不确定。



这是行它位于第一个 for循环的中间附近

  m_PixelBuf [index + 2] = m_PixelBuf [index + 2] / * aRed * /; 

通常情况下,它被设置为一个红色,我已经检查过了,它不应该离开缓冲区边界。(void)contrastWithContrast:(float)contrast colorWithColor:(float)color {
drawImage.image =原版的;
UIImage * unfilteredImage2 = [[[UIImage alloc] initWithCGImage:drawImage.image.CGImage] autorelease];
CGImageRef inImage = unfilteredImage2.CGImage;
CGContextRef ctx;
CFDataRef m_DataRef;
m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
UInt8 * m_PixelBuf =(UInt8 *)CFDataGetBytePtr(m_DataRef);
int length = CFDataGetLength(m_DataRef);
NSLog(@Photo Length:%i,length);
//////对比度///////////
// NSLog(@Contrast:%f,contrast);
int aRed;
int aGreen;
int aBlue;
for(int index = 0; index< length; index + = 4){
aRed = m_PixelBuf [index + 2];
aGreen = m_PixelBuf [index + 1];
aBlue = m_PixelBuf [index]; ((aRed-128)*(对比度+100))/ 100)+128;

aRed =如果(aRed ;如果(aRed> 255)aRed = 255;
m_PixelBuf [index + 2] = m_PixelBuf [index + 2] / * aRed * /; //总是在这里崩溃

aGreen =(((aGreen-128)*(contrast + 100 ))/ 100)+ 128; (aGreen ;如果(aGreen> 255)aGreen = 255;
m_PixelBuf [index + 1] = aGreen; (((aBlue-128)*(对比度+100))/ 100)+128; b
$ b aBlue =
if(aBlue m_PixelBuf [index] = aBlue; (inImage),

$ b ctx = CGBitmapContextCreate(m_PixelBuf,
)CGImageGetHeight(inImage),
CGImageGetBitPerComponent ,
CGImageGetColorSpace(inImage),
CGImageGetBitmapInfo(inImage));
CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
UIImage * rawImage = [[UIImage alloc] initWithCGImage:imageRef];
drawImage.image = rawImage;

[rawImage发行];
CGContextRelease(ctx);
CFRelease(imageRef);
CFRelease(m_DataRef);

unfilteredImage2 = [[[UIImage alloc] initWithCGImage:drawImage.image.CGImage] autorelease];
inImage = unfilteredImage2.CGImage;
m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
m_PixelBuf =(UInt8 *)CFDataGetBytePtr(m_DataRef);
length = CFDataGetLength(m_DataRef);
$ b $ ///////彩色////////////////
(int index = 0; index< length; index + = 4)
{
//蓝
如果((m_PixelBuf [指数] +((int)的颜色* 2))> 255){
m_PixelBuf [索引] = 255; ((m_PixelBuf [index] +((int)color * 2))< 0){
m_PixelBuf [index] = 0;
}
else {
m_PixelBuf [index] = m_PixelBuf [index] +((int)color * 2);
}

//绿色
如果((m_PixelBuf [指数+ 1] +((int)的颜色* 2))> 255){
m_PixelBuf [ index + 1] = 255; ((m_PixelBuf [index + 1] +((int)color * 2))< 0){
m_PixelBuf [index + 1] = 0;
}
else {
m_PixelBuf [index + 1] = m_PixelBuf [index + 1] +((int)color * 2);
}

//红色
如果((m_PixelBuf [指数+ 2] +((int)的颜色* 2))> 255){
m_PixelBuf [ index + 2] = 255; ((m_PixelBuf [index + 2] +((int)color * 2))< 0){
m_PixelBuf [index + 2] = 0;
}
else {
m_PixelBuf [index + 2] = m_PixelBuf [index + 2] +((int)color * 2);
}

// m_PixelBuf [index + 3] = 255; // Alpha
}

ctx = CGBitmapContextCreate(m_PixelBuf,
CGImageGetHeight(inImage),
CGImageGetBitsPerComponent(inImage),
CGImageGetBytesPerRow(inImage),
CGImageGetColorSpace(inImage),
CGImageGetBitmapInfo(inImage)) ;
imageRef = CGBitmapContextCreateImage(ctx);
rawImage = [[UIImage alloc] initWithCGImage:imageRef];
drawImage.image = rawImage;
[rawImage发行];
CGContextRelease(ctx);
CFRelease(imageRef);
CFRelease(m_DataRef);
//drawImage.image = unfilteredImage2;
willUpdate = YES;





$ b

对于任何额外的评论/信息抱歉我只是复制了整个方法。 / p>

谢谢,

Storealutes

解决方案

我有同样的问题。
您应该使用下面的代码来获取像素缓冲区的指针,而不是CFDataGetBytePtr()。

$ p codeGImageRef cgImage = originalImage。 CGImage;
size_t width = CGImageGetWidth(cgImage);
size_t height = CGImageGetHeight(cgImage);
$ b $ char * buffer =(char *)malloc(sizeof(char)* width * height * 4);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef cgContext = CGBitmapContextCreate(buffer,width,height,8,width * 4,colorSpace,kCGImageAlphaPremultipliedLast);
CGContextSetBlendMode(cgContext,kCGBlendModeCopy);
CGContextDrawImage(cgContext,CGRectMake(0.0f,0.0f,width,height),cgImage);

free(buffer);
CGContextRelease(cgContext);
CGColorSpaceRelease(colorSpace);


I have a method that will add a filter to an image. This worked fine until a couple of months ago, now when I try to use this method the application will crash on the images buffer. I create the buffer and set it to the image's data, accessing the specific index later causes a bad access crash. I have looked for the past hour or two, and now I am convinced there is something im overlooking. I think something is being released that should not be. I am using the ios DP 4 preview of xcode, and I think this problem started with the update to the beta, but I am really not sure.

This is the line it crashes on located near the middle of the first for loop

    m_PixelBuf[index+2] = m_PixelBuf[index+2]/*aRed*/;

Normally it is set to aRed Which I have checked, and it should not go out of the buffers boundaries.

    -(void)contrastWithContrast:(float )contrast colorWithColor:(float )color{
    drawImage.image = original;
    UIImage * unfilteredImage2 = [[[UIImage alloc]initWithCGImage:drawImage.image.CGImage]    autorelease];
    CGImageRef inImage = unfilteredImage2.CGImage;
    CGContextRef ctx;
    CFDataRef m_DataRef;
    m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
    UInt8 * m_PixelBuf  = (UInt8 *) CFDataGetBytePtr(m_DataRef);
    int length = CFDataGetLength(m_DataRef);
    NSLog(@"Photo Length: %i",length);
    //////Contrast/////////////
    //NSLog(@"Contrast:%f",contrast);
    int aRed;
    int aGreen;
    int aBlue;
    for (int index = 0; index < length; index += 4){
    aRed = m_PixelBuf[index+2];
    aGreen = m_PixelBuf[index+1];
    aBlue = m_PixelBuf[index];

    aRed = (((aRed-128)*(contrast+100) )/100) + 128;
    if (aRed < 0) aRed = 0; if (aRed>255) aRed=255;
    m_PixelBuf[index+2] = m_PixelBuf[index+2]/*aRed*/;//Always crashes here

    aGreen = (((aGreen-128)*(contrast+100) )/100) + 128;
    if (aGreen < 0) aGreen = 0; if (aGreen>255) aGreen=255;
    m_PixelBuf[index+1] = aGreen;

    aBlue = (((aBlue-128)*(contrast+100) )/100) + 128;
    if (aBlue < 0) aBlue = 0; if (aBlue>255) aBlue=255;
    m_PixelBuf[index] = aBlue;
}
ctx = CGBitmapContextCreate(m_PixelBuf,
                            CGImageGetWidth( inImage ),
                            CGImageGetHeight( inImage ),
                            CGImageGetBitsPerComponent(inImage),
                            CGImageGetBytesPerRow(inImage ),
                            CGImageGetColorSpace(inImage ),
                            CGImageGetBitmapInfo(inImage) );
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [[UIImage alloc]initWithCGImage:imageRef];
drawImage.image = rawImage;

[rawImage release];
CGContextRelease(ctx);
CFRelease(imageRef);
CFRelease(m_DataRef);

unfilteredImage2 = [[[UIImage alloc]initWithCGImage:drawImage.image.CGImage] autorelease];
inImage = unfilteredImage2.CGImage;
m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
length = CFDataGetLength(m_DataRef);

///////Color////////////////
for (int index = 0; index < length; index += 4)
{
    //Blue
    if((m_PixelBuf[index] + ((int)color * 2))>255){
        m_PixelBuf[index] = 255;
    }else if((m_PixelBuf[index] + ((int)color * 2))<0){
        m_PixelBuf[index] = 0;
    }
    else{
        m_PixelBuf[index]=m_PixelBuf[index] + ((int)color * 2);
    }

    //Green
    if((m_PixelBuf[index+1] + ((int)color * 2))>255){
        m_PixelBuf[index+1] = 255;
    }else if((m_PixelBuf[index+1] + ((int)color * 2))<0){
        m_PixelBuf[index+1] = 0;
    }
    else{
        m_PixelBuf[index+1]=m_PixelBuf[index+1] + ((int)color * 2);
    }

    //Red
    if((m_PixelBuf[index+2] + ((int)color * 2))>255){
        m_PixelBuf[index+2] = 255;
    }else if((m_PixelBuf[index+2] + ((int)color * 2))<0){
        m_PixelBuf[index+2] = 0;
    }
    else{
        m_PixelBuf[index+2]=m_PixelBuf[index+2] + ((int)color * 2);
    }

    //m_PixelBuf[index+3]=255;//Alpha
}

ctx = CGBitmapContextCreate(m_PixelBuf,
                            CGImageGetWidth( inImage ),
                            CGImageGetHeight( inImage ),
                            CGImageGetBitsPerComponent(inImage),
                            CGImageGetBytesPerRow(inImage ),
                            CGImageGetColorSpace(inImage ),
                            CGImageGetBitmapInfo(inImage) );
imageRef = CGBitmapContextCreateImage (ctx);
rawImage = [[UIImage alloc]initWithCGImage:imageRef];
drawImage.image = rawImage;
[rawImage release];
CGContextRelease(ctx);
CFRelease(imageRef);
CFRelease(m_DataRef);
//drawImage.image = unfilteredImage2;
willUpdate = YES;
}

sorry for any extra comments/info I just copied the whole method in.

Thanks,

Storealutes

解决方案

I had same problem.You should use below code to get pointer to pixel buffer instead of CFDataGetBytePtr().

CGImageRef cgImage = originalImage.CGImage;
size_t width = CGImageGetWidth(cgImage);
size_t height = CGImageGetHeight(cgImage);

char *buffer = (char*)malloc(sizeof(char) * width * height * 4);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef cgContext = CGBitmapContextCreate(buffer, width, height, 8, width * 4, colorSpace, kCGImageAlphaPremultipliedLast);
CGContextSetBlendMode(cgContext, kCGBlendModeCopy);
CGContextDrawImage(cgContext, CGRectMake(0.0f, 0.0f, width, height), cgImage);

free(buffer);
CGContextRelease(cgContext);
CGColorSpaceRelease(colorSpace);

这篇关于UInt8 EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 18:06