Malloc for CGPoint pointer throwing EXC_BAD_ACCESS on access

I try to use the code snippet from the Apple programming guide and I get EXC_BAD_ACCESS when I try to pass a pointer to a function right after malloc is executed.

(for reference: iPhone Application Programming Guide: event handling - Listing 3-6 )

This code 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 a statement if, it assigns 'output' from mallocto a variable / pointer point.

Then, when it tries to pass pointto a function CFDictionarySetValue, it resets the application with:Program received signal: "EXC_BAD_ACCESS".

- malloc point var/pointer : &point, EXC_BAD_ACCESS.

( Apple) ???

.

+3
3

. CFDictionarySetValue, retain CFMutableDictionaryRef. , (, CFDictionaryCreateMutable()), .

EDIT:

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
+4

, , CGPoint, , , CGPoint , C.

+1

Source: https://habr.com/ru/post/1750544/


All Articles