开发者

How do I free() after malloc() when the result of malloc() is returned by the function?

开发者 https://www.devze.com 2022-12-18 18:02 出处:网络
I have the following instance method (adapted from Listing 3-6 of the Event Handling section in the iPhone Application Programming Guide):

I have the following instance method (adapted from Listing 3-6 of the Event Handling section in the iPhone Application Programming Guide):

- (CGPoint)originOfTouch开发者_开发问答:(UITouch *)touch
{
    CGPoint *touchOriginPoint = (CGPoint *)CFDictionaryGetValue(touchOriginPoints, touch);
    if (touchOriginPoint == NULL)
    {
        touchOriginPoint = (CGPoint *)malloc(sizeof(CGPoint)); // leaks
        CFDictionarySetValue(touchOriginPoints, touch, touchOriginPoint);
        *touchOriginPoint = [touch locationInView:touch.view];
    }
    return *touchOriginPoint;
}

Every once in a while my app leaks 16 Bytes as a result of the call to malloc(). I'm not sure how to return touchOriginPoint while free()ing it as well.


If you do not care a minor performance loss, use an NSMutableDictionary and store the point as an NSValue:

NSValue* touchOriginPointValue = [touchOriginPoints objectForKey:touch];
if (touchOriginPointValue == nil) {
   touchOriginPointValue = [NSValue valueWithCGPoint:[touch locationInView:touch.view]];
   [touchOriginPoints setObject:touchOriginPointValue forKey:touch];
}
return [touchOriginPointValue CGPointValue];

If you must use the CFDictionary approach, you have to find a place to free those malloc-ed memory when the values are not needed. Therefore, you have to pass the values callbacks when creating the dictionary

static void free_malloced_memory (CFAllocatorRef allocator, const void *value) {
   free((void*)value);
}
static const CFDictionaryValueCallBacks values_callbacks = {0, NULL, free_malloced_memory, NULL, NULL};
...
touchOriginPoints = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, & values_callbacks);
...


If you must return the malloc'd value from the function, then you have passed the responsibility for freeing the memory to the calling function, or one of its callers.

Since we can't see the calling functions, we can't diagnose any more.


If you are going to be returning an object that is allocated, then either you need to have the caller free() it, or else you need to be using some kind of garbage collection (so it gets freed automatically).


you don't actually return a pointer, the value is copied to a temp value when it is returned, so you aren't really returning the allocation at all, the problem is that you just aren't freeing it either, you add the allocation to the dictionary and just leave it there?

is there like an EndOfTouch function? where you remove the touch from the dictionary? if there is, call free on your allocation there and you should be fine

0

精彩评论

暂无评论...
验证码 换一张
取 消