Crash CGDataProviderCreateWithCopyOfData: vm_copy failed: status 1

和自甴很熟 提交于 2020-01-03 08:46:10

问题


I am facing a crash with following error:

"CGDataProviderCreateWithCopyOfData: vm_copy failed: status 1."

I have multiple questions and you can help.

  1. What does status 1 stand for in vm_copy failed?

  2. This crash happens only when I set a break point in the inner for loop of data copy. And then resume and remove the breakpoint. If there is no breakpoint, function executes but I get a blank image. How do I ensure that even if I don't set a break point, such crashes are caught and application stops execution?

  3. This error comes when I execute CGBitmapContextCreateImage. Does anyone know how to resolve this?

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

}

回答1:


The kern_return.h header file gives:

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

This corresponds to the error code related to vm_copy failed: status 1.

I suspect this is a problem related to memory alignment, since the vm_copy documentation states that address[es] must be on a page boundary.

To make sure you work with properly aligned buffers, you should allocate your piData buffer with the same stride than your inImage input image:

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

Then use this bytesPerRow value instead of width*4 within your for loop, i.e:

iStep = i*bytesPerRow;

This should solve your problem (Note: I assume that CGImageGetWidth(inImage) and width are the same).



来源:https://stackoverflow.com/questions/13100078/crash-cgdataprovidercreatewithcopyofdata-vm-copy-failed-status-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!