[英]Malloc to a CGPoint Pointer throwing EXC_BAD_ACCESS when accessing
I am trying to use a snippet of code from a Apple programming guide, and I am getting a EXC_BAD_ACCESS when trying to pass a pointer to a function, right after doing a malloc. 我正在尝试使用Apple编程指南中的一段代码,并且在尝试将一个指针传递给函数时,我正在获取EXC_BAD_ACCESS,就在执行malloc之后。
(For Reference: iPhone Application Programming Guide: Event Handling - Listing 3-6 ) (仅供参考: iPhone应用程序编程指南:事件处理 - 清单3-6 )
The code in question is really simple: 有问题的代码非常简单:
CFMutableDictionaryRef touchBeginPoints;
UITouch *touch;
....
CGPoint *point = (CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch);
if (point == NULL)
{
point = (CGPoint *)malloc(sizeof(CGPoint));
CFDictionarySetValue(touchBeginPoints, touch, point);
}
Now when the program goes into the if
statement it assigns the 'output' of malloc
into the point
variable/pointer. 现在当程序进入if
语句时,它将malloc
的'output'分配给point
变量/指针。
Then when it tries to pass point
into the CFDictionarySetValue
function it crashes the application with: Program received signal: “EXC_BAD_ACCESS”.
然后,当它试图将point
传递给CFDictionarySetValue
函数时,它会崩溃应用程序: Program received signal: “EXC_BAD_ACCESS”.
Someone suggested not doing the malloc
and pass the point
var/pointer as: &point
, however that still gave me a EXC_BAD_ACCESS
. 有人建议不要使用malloc
并将point
var /指针传递为: &point
,但是仍然给了我一个EXC_BAD_ACCESS
。
What I am (and it looks like Apple) doing wrong??? 我是什么(看起来像Apple)做错了???
Thanks in advance. 提前致谢。
Sean's answer is mostly correct. 肖恩的回答大多是正确的。 According to the documentation for CFDictionarySetValue
, it's going to try and retain
the value according to how your CFMutableDictionaryRef
is set up. 根据CFDictionarySetValue
的文档 ,它将根据CFMutableDictionaryRef
的设置方式尝试retain
该值。 I'm guessing that when you create the mutable dictionary (presumably using CFDictionaryCreateMutable()
), you're not providing custom callbacks for how to handle setting and removing values . 我猜测当你创建可变字典(大概使用CFDictionaryCreateMutable()
)时,你不提供如何处理设置和删除值的自定义回调。
EDIT: 编辑:
Another option to providing custom callbacks is to provide NULL
for the value callbacks: 提供自定义回调的另一个选择是为值回调提供NULL
:
CFMutableDictionaryRef dict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, NULL);
CGPoint * point = (CGPoint *)malloc(sizeof(CGPoint));
point->x = 42;
point->y = 42;
CFDictionarySetValue(dict, @"foo", point);
CGPoint * newPoint = CFDictionaryGetValue(dict, @"foo");
NSLog(@"%f, %f", newPoint->x, newPoint->y);
Logs: 日志:
2010-06-17 11:32:47.942 EmptyFoundation[45294:a0f] 42.000000, 42.000000
CGPoint is a struct, not an Objective-C/CF object, so you need to wrap it in an NSValue
: CGPoint是一个结构,而不是Objective-C / CF对象,因此您需要将其包装在NSValue
:
+ (NSValue *)valueWithPoint:(NSPoint)aPoint
http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSValue_Class/Reference/Reference.html#//apple_ref/occ/clm/NSValue/valueWithPoint : http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSValue_Class/Reference/Reference.html#//apple_ref/occ/clm/NSValue/valueWithPoint :
它可能会崩溃,因为当你把它放在字典中时它试图保留你的CGPoint,除了CGPoint不是真正的对象而是C结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.