CFDictionary存储CGPoint和结构体等数据

ask:

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.

(For Reference: iPhone Application Programming Guide: Event Handling - Listing 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 pointvariable/pointer.

Then when it tries to pass point into the CFDictionarySetValue function it crashes the application with: 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.

What I am (and it looks like Apple) doing wrong???

Thanks in advance.

 

 

answer:

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. I'm guessing that when you create the mutable dictionary (presumably usingCFDictionaryCreateMutable()), you're not providing custom callbacks for how to handle setting and removing values.

EDIT:

Another option to providing custom callbacks is to provide NULL for the value callbacks:

 

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

 

I didn't do custom call backs... I used the following... touchBeginPoints = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); – kdbdallas Jun 17 '10 at 17:24


@kdbdallas and the documentation for kCFTypeDictionaryValueCallBacks says that the default retain callback is CFRetain, which is where your problem lies. You have to specify custom retain and release callbacks (and it wouldn't hurt to specify copy and equals callbacks either), or box the points in NSValues, like Dave Dribin suggests. – Dave DeLong Jun 17 '10 at 17:25


Defining it like this solved it. touchBeginPoints = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, NULL); THANKS! – kdbdallas Jun 17 '10 at 17:39


Doesn't this leak the malloc'ed CGPoint? When does it get freed? – Dave Dribin Jun 17 '10 at 18:02


@Dave yeah, this will leak both the point and the dictionary. I was just writing the code to show that it's possible. – Dave DeLong Jun 17 '10 at 18:22

 

 

 

注:Any CF type can be released/retained/autoreleased.

Also you should definitely not be freeing the result of CFDataGetBytePtr. Stop doing that, and don't modify it in place.

 

 

 

touchBeginPoints = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);

CGPoint *point = (CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch);
        if (point == NULL) {
            point = (CGPoint *)malloc(sizeof(CGPoint));
            CFDictionarySetValue(touchBeginPoints, touch, point);
        }
        *point = [touch locationInView:self.superview];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值