سؤال

I am setting the line dash style on an NSBezierPath instance:

NSBezierPath *path = [NSBezierPath path];
// Get the path information...

NSInteger count = 0;
// Get the array count...

CGFloat *dashLengths = (CGFloat *)malloc(sizeof(CGFloat) * count);
// Populate the array...

CGFloat phase = 0.0f;
// Get the phase...

[path setLineDash:dashLengths count:count phase:phase];

Should I call free() on dashLengths to prevent a memory leak? Or would releasing the array cause a error later on?

هل كانت مفيدة؟

المحلول

Why are you using malloc in the first place? Just declare

CGFloat dashLengths[count];

Now dashLengths is an automatic variable. No memory management required.

نصائح أخرى

No, you must free() that malloc()'d array yourself.

You should call free() on dashLengths right after you free path. NSBezierPath has no way of retaining it, since it's just a regular ol pointer, so you should not take that memory away until NSBezierPath goes away.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top