崩溃CGDataProviderCreateWithCopyOfData:vm_copy失败:状态1

问题描述 投票:7回答:1

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

“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;

}
ios objective-c image crash cgbitmapcontextcreate
1个回答
5
投票

kern_return.h头文件给出:

#define KERN_INVALID_ADDRESS        1
  /* Specified address is not currently valid.
   */

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

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

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

size_t bytesPerRow = CGImageGetBytesPerRow(inImage);
UInt8*piData = calloc(bytesPerRow*height,sizeof(UInt8));

然后在你的bytesPerRow循环中使用这个width*4值而不是for,即:

iStep = i*bytesPerRow;

这应该可以解决你的问题(注意:我假设CGImageGetWidth(inImage)width是相同的)。

© www.soinside.com 2019 - 2024. All rights reserved.