崩溃CGDataProviderCreateWithCopyOfData:vm_copy失败:状态1

Jai*_*ken 7 crash image objective-c cgbitmapcontextcreate ios

我正面临崩溃并出现以下错误:

"CGDataProviderCreateWithCopyOfData:vm_copy失败:状态1."

我有很多问题,你可以提供帮助.

  1. vm_copy中状态1代表什么失败?

  2. 仅当我在数据副本的内部for循环中设置断点时才会发生此崩溃.然后恢复并删除断点.如果没有断点,则执行函数但是我得到一个空白图像.即使我没有设置断点,如何捕获此类崩溃并且应用程序停止执行,我如何确保?

  3. 执行CGBitmapContextCreateImage时会出现此错误.有谁知道如何解决这个问题?

-(UIImage *) convertBitmapRGBA8ToUIImage:(UInt8**)img 
                                    :(int) width
                                    :(int) height 
{

CGImageRef inImage = m_inFace.img.CGImage;

UInt8*piData = calloc( width*height*4,sizeof(UInt8));

int iStep,jStep;
for (int i = 0; i < height; i++) 
{
    iStep = i*width*4;
    for (int j = 0; j < width; j++) 
    {
        jStep = j*4;
        piData[iStep+jStep] =img[i][j];
        piData[iStep+jStep+1] =img[i][j];
        piData[iStep+jStep+2] = img[i][j];

    }
}

CGContextRef ctx = CGBitmapContextCreate(piData,
                                         CGImageGetWidth(inImage),  
                                         CGImageGetHeight(inImage),  
                                         CGImageGetBitsPerComponent(inImage),
                                         CGImageGetBytesPerRow(inImage),  
                                         CGImageGetColorSpace(inImage),  
                                         CGImageGetBitmapInfo(inImage) 
                                         ); 

CGImageRef imageRef = CGBitmapContextCreateImage(ctx);  
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
CGImageRelease(imageRef);
free(piData);
return finalImage;

}
Run Code Online (Sandbox Code Playgroud)

del*_*eil 5

kern_return.h头文件给出了:

#define KERN_INVALID_ADDRESS        1
  /* Specified address is not currently valid.
   */
Run Code Online (Sandbox Code Playgroud)

这对应于与之相关的错误代码vm_copy failed: status 1.

我怀疑这是一个与内存对齐有关的问题,因为vm_copy文档声明地址[es]必须位于页面边界上.

要确保使用正确对齐的缓冲区,应该piData使用与inImage输入图像相同的步幅分配缓冲区:

size_t bytesPerRow = CGImageGetBytesPerRow(inImage);
UInt8*piData = calloc(bytesPerRow*height,sizeof(UInt8));
Run Code Online (Sandbox Code Playgroud)

然后使用此bytesPerRow值而不是width*4for循环中,即:

iStep = i*bytesPerRow;
Run Code Online (Sandbox Code Playgroud)

这应该解决您的问题(注:我假设CGImageGetWidth(inImage)width相同).