Sunday, August 1, 2010

Instruments say this code from Erica Sadun is leaking

Programmer Question

I have this snippet of code from Erica Sadun (the only woman I ever heard that kicks butts in programming) from a class she created, that Instruments say is leaking.



- (void)cacheBeginPointForTouches:(NSSet *)touches
{
if ([touches count] > 0) {
for (UITouch *touch in touches) {
CGPoint *point = (CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch);
if (point == NULL) {
point = (CGPoint *)malloc(sizeof(CGPoint));
CFDictionarySetValue(touchBeginPoints, touch, point);
}
*point = [touch locationInView:self.superview];
}
}
}


Instruments is pointing to



 point = (CGPoint *)malloc(sizeof(CGPoint));


as the leaking line.



As this malloc stuff is not familiar to me. I know that it allocates memory, but as I never worked with C, C++ and other flavors of C, malloc and I are not acquaintances.



Another question I don't understand is why she put an asterisk before "point" on



*point = [touch locationInView:self.superview];



So, do you see something wrong with the code and why instruments are saying it is leaking there? An explanation about the asterisk is a bonus! :)



thanks.



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails