I recently discovered a strange memory leak in a 3rd-party component I use. A call to CFArraySortValues() sometimes produced a leak, sometimes it didn't.

This was the invocation:

int context = kCFCompareCaseInsensitive;
CFArraySortValues(keyArray,
                  CFRangeMake(0, count),
                  (CFComparatorFunction)CFStringCompare, &context);

After reading through CFArraySortValues and CFStringCompare documentation it's clear (to me anyway) that the context is used incorrectly. The documentation doesn't imply it will dereference the context pointer at any point. That means that CFStringCompare will be called with the pointer value instead of the contents of that address.

Changing it to

CFArraySortValues(keyArray,
                  CFRangeMake(0, count),
                  (CFComparatorFunction)CFStringCompare,
                  (void*)kCFCompareCaseInsensitive);

made the leak go away. I can only assume that the author never realized the array wasn't going to be sorted the way he intended.

Can anyone tell me if the latter use of CFArraySortValues() is correct? It doesn't feel right to cast an int option to (void *) but the documentation simply doesn't say it will pass on a dereferenced value. The fact that the leak went away tells me I'm on the right track.

Regards
Markus
--
__________________________________________
Markus Spoettl
_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to