简体   繁体   English

程序接收信号EXC_BAD_ACCESS访问阵列

[英]Program received signal EXC_BAD_ACCESS accessing array

I am using the routine for getting pixel colour (this one: http://www.markj.net/iphone-uiimage-pixel-color/ ) and am faced with frequent app crashes when using it. 我正在使用例程获取像素颜色(这一个: http//www.markj.net/iphone-uiimage-pixel-color/ )并且在使用它时遇到频繁的应用程序崩溃。 The relevant portion of the code: 代码的相关部分:

unsigned char* data = CGBitmapContextGetData (cgctx);
if (data != NULL) {
    int offset = (some calculations here);
    int alpha = data[offset]; // <<<< crashes here
}

This code is linked to be ran on touchesBegan, touchesEnded and touchesMoved. 此代码链接在touchesBegan,touchesEnded和touchesMoved上运行。 It appears that the crashes occur during touchesEnded and touchesMoved events only, particularly when I start the touch on the target image, but move it off the boundaries of the image in the process. 看起来崩溃只发生在touchesEnded和touchesMoved事件中,特别是当我开始触摸目标图像时,但是在此过程中将其移出图像的边界。

Is there any way to check what is the size of the data in the array pointed to by data object? 有没有办法检查data对象指向的数组中的数据大小是多少? What could be going wrong there? 那里可能出现什么问题?

Edit : The calculation of offset: 编辑 :偏移量的计算:

int offset = 4*((w*round(point.y)*x)+round(point.x)*x);

Where point is the point where touch occurs, w is the width if the image, x is the scale of the image (for hi-res images on retina displays). 其中point是发生触摸的点, w是图像的宽度, x是图像的比例(对于视网膜显示器上的高分辨率图像)。

I don't see anything wrong with the cgctx either. 我也没有看到cgctx有什么问题。 Basically, I am running the code from the link above almost unmodified, the particular code snippet I have problems with is in the function (UIColor*) getPixelColorAtLocation:(CGPoint)point so if you want the details of what the code does, just read the source there. 基本上,我从上面的链接运行代码几乎未修改,我遇到问题的特定代码片段是在函数(UIColor*) getPixelColorAtLocation:(CGPoint)point所以如果你想要代码的详细信息,只需阅读来源那里。

Edit: another thing is that this never happens in the simulator, but often happens when testing on a device. 编辑:另一件事是,这在模拟器中永远不会发生,但经常在设备上进行测试时发生。

Edit: Ideally I'd want to do nothing if the finger is currently not over the image, but have trouble figuring out when that happens. 编辑:理想情况下,如果手指当前没有覆盖图像,我不想做任何事情 ,但是无法确定何时发生这种情况。 It looks like the relevant methods in SDK only show what view the touch originated in, not where it is now. 看起来SDK中的相关方法只显示触摸源自哪个视图,而不是现在的位置。 How can I figure that out? 我怎么能弄明白呢?

You didn't show all your work. 你没有展示你的所有作品。 Your offset calculation is likely returning either a negative number or a number well beyond the end of the buffer. 您的offset计算可能会返回负数或远远超出缓冲区末尾的数字。 Since CG* APIs often allocate rather large chunks of memory, often memory mapped, it is quite likely that the addresses before and after the allocation are unallocated/unmapped and, thus, access outside of the buffer leads to an immediate crash (as opposed to returning garbage). 由于CG* API经常分配相当大的内存块(通常是内存映射),因此分配前后的地址很可能是未分配/未映射的,因此,在缓冲区外部的访问会导致立即崩溃(与返回垃圾)。

Which is good. 这很好。 Easier to debug. 更容易调试。

You did provide a clue: 你确实提供了一个线索:

move it off the boundaries of the image in the process 将其移出过程中图像的边界

I'd guess you modified the offset calculation to take the location of the touch. 我猜你会修改offset计算以获取触摸的位置。 And that location has moved beyond the bounds of the image and, thus, leads to a nonsense offset and a subsequent crash. 并且该位置已经超出了图像的范围,因此导致无意义的offset和随后的崩溃。

Fix that, and your app will stop crashing here. 修复此问题,您的应用将停止崩溃。


Does your image exactly occupy the entire bounds of the item being touched? 您的图像是否完全占据了被触摸项目的整个范围? Ie does the thing handling the touches*: events have a bounds whose width and height are exactly the same as the image? 即处理touches*:的事情touches*:事件有一个宽度和高度与图像完全相同的边界?

If not, you need to make sure you are correctly translating the coordinates from whatever is handling the touches to coordinates within the image. 如果没有,您需要确保正确地将坐标从处理触摸的任何内容转换为图像中的坐标。 Also note that the layout of bytes in an image is heavily dependent on exactly how the image was created and what internal color model it is using. 另请注意,图像中字节的布局很大程度上取决于图像的确切创建方式以及图像的内部颜色模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM