如何在所附的C阵列中正确释放内存? [英] How does one release memory correctly in the attached C array?

查看:89
本文介绍了如何在所附的C阵列中正确释放内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想弄清楚为什么以下代码会泄漏内存,并且我有一种奇怪的感觉,就是我没有正确释放阵列内存。这是更广泛的Objective-C应用程序中的C函数,我不是C的本地人。我试过在数组上仅使用free(),但感觉这不是全部。

I'm just trying to work out why the following code is leaking memory and I have a funny feeling that i'm not releasing the array memory correctly. This is a C function in a wider objective-c app and I'm not native to C... i've tried just using free() on the array, but have a feeling this isn't the whole story...

有人可以看看我在这里想念的是什么吗?谢谢!

Could someone have a look and see what I'm missing here. Thanks!

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);


推荐答案

您不会释放未分配给内存的内存堆。

You don't free memory that's not allocated on the heap.

这篇关于如何在所附的C阵列中正确释放内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆