tags:

views:

83

answers:

3

I am not familiar with C. I have a method created by someone else that has a CFDicionary that was created like this

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

point = (CGPoint *)malloc(sizeof(CGPoint));
CFDictionarySetValue(touchBeginPoints, touch, point);

I would like to dealloc the dictionary now. As far as I understand, I have to go item by item on whatever number of entries the dictionary has and free each one.

something like

        free((void *)CFDictionaryGetValue(touchBeginPoints, ...));
        CFDictionaryRemoveValue(touchBeginPoints, ...);

So, how do I iterate thru this CFDictionary freeing each point stored there and removing each dictionary entry?

thanks for any help.

A: 

You can use Objective-C's toll-free bridging between CFDictionary and NSDictionary, if you'd like:

[(id)touchBeginPoints release]

Mike Krieger
A: 

No need to iterate through it, just release it with the CFRelease function. For further details and information refer to Apples Memory Management Programming Guide for Core Foundation on the iPhone Dev Center.

In your case:

CFRelease(touchBeginPoints);
rastersize
+1  A: 

You don't.

You read this page: http://developer.apple.com/mac/library/documentation/CoreFoundation/Reference/CFDictionaryRef/Reference/reference.html#//apple_ref/doc/c_ref/CFDictionaryValueCallBacks

You recognise that by default, the standard dictionary will call CFRetain() to hold on to the value you pass it, and CFRelease() to let go of it. You think "...but CFRetain() is not appropriate to be called against CGPoint". You realise that all those people who told you to just release the dictionary will be leaking at best and crashing at worst.

You then create a structure like this:

CFDictionaryValueCallBacks myValueCallbacks = {
  0,
  NULL,
  myRelease,
  NULL,
  NULL
};

and pass that as the valueCallbacks argument when creating the dictionary (the last argument). The myRelease function should look like this:

void myRelease ( CFAllocatorRef allocator, const void *value )
{
    free(value);
}

Now, whenever the dictionary releases the values, it will call free() on them for you.

Having said all that, if you're a newcomer to C, this is not the place to start.

Jeff
I understand you are right. I tried to implement what you said and I am receiving errors from the compiler. For example on the free(value) line I see ("warning: passing argument 1 of 'free' discards qualifiers from pointer target type"). So, can you please tell me how do I add and remove objects from this dictionary and how do I release the dictionary after implementing your method? Please code specifically for my case cause I have no means in C to extrapolate pseudo code and apply to my case... I am totally noob in C. THANKS.
Digital Robot
To eliminate the warning, just change free(value) to free((void*)value).Once you're using this valueCallback, you can just use CFRelease(touchBeginPoints) to release the dictionary, as @rastersze suggested.You do not need to iterate through the contents of the dictionary. Removing an object from the dictionary will now free it. Note, it is impossible to tell whether this is a good thing or not for the rest of your application. At a guess, you're copying Erica Sadun here, and thats not a good thing for a noob to try.
Jeff
I know, but Erica's example will just sit there on a class that does what I want and that I will never need to modify. She does much stuff in C and C melts my brain. I always try to convert everything from C/C++ to Objective-C, but first I have to understand what is done. I have accomplished this with success in the past but this dictionary stuff was killing me. Thanks!
Digital Robot