如何在附加的C数组中正确释放内存?

Tri*_*cky 4 c iphone cocoa objective-c

我只是想弄清楚为什么以下代码泄漏内存,我有一种有趣的感觉,我没有正确释放阵列内存.这是一个更广泛的Objective-c应用程序中的C函数,我不是C的原生...我曾尝试在数组上使用free(),但感觉这不是整个故事......

有人可以看一看,看看我在这里缺少什么.谢谢!

CFIndex theNumberOfSettings = 3;
CTParagraphStyleSetting theSettings[3] =
{
    {kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &alignment},
    {kCTParagraphStyleSpecifierLineSpacing, sizeof(lineSpacing), &lineSpacing},
    {kCTParagraphStyleSpecifierHeadIndent, sizeof(headIndent), &headIndent}
};

CTParagraphStyleRef theParagraphRef = CTParagraphStyleCreate(theSettings, theNumberOfSettings);

CFAttributedStringSetAttribute(attrString, CFRangeMake(0, CFAttributedStringGetLength(attrString)-1), kCTParagraphStyleAttributeName, theParagraphRef);

CFRelease(theParagraphRef);
free(theSettings);
Run Code Online (Sandbox Code Playgroud)

Bri*_*ndy 12

您没有释放未在堆上分配的内存.

  • 更强:你*不得*释放上面的数组.只有malloc()ed的东西应该是free()d.试图释放malloc尚未返回的指针是一个错误.当函数或方法返回时,将自动回收该数组的内存. (7认同)