无法获取函数中内存泄漏的位置
我在识别内存泄漏时遇到问题。我尝试了 Instruments,它说每次我调用下面描述的函数时都会出现内存泄漏。
CFStringRef getStringFromLocalizedNIB(int cmdId)
{
IBNibRef nibRef;
WindowRef wind=NULL;
CFStringRef alertString;
CreateNibReference(CFSTR("main"), &nibRef);
CreateWindowFromNib(nibRef, CFSTR("Localized Strings"), &wind);
DisposeNibReference(nibRef);
ControlID alertID = {'strn',cmdId};
ControlRef alertRef;
GetControlByID(wind, &alertID,&alertRef);
GetControlData(alertRef, kControlNoPart, kControlStaticTextCFStringTag, sizeof(CFStringRef), &alertString, NULL);
return alertString;
}
每次调用该函数时,我都会释放返回的对象。
CFStringRef lstr;
lstr = getStringFromLocalizedNIB(20);
//Use lstr;
CFRelease(lstr);
那么有人可以解释一下泄漏在哪里吗?
I am having a problem in identifying a memory leak. I tried Instruments and it says that there is memory leak every time I call the function described below.
CFStringRef getStringFromLocalizedNIB(int cmdId)
{
IBNibRef nibRef;
WindowRef wind=NULL;
CFStringRef alertString;
CreateNibReference(CFSTR("main"), &nibRef);
CreateWindowFromNib(nibRef, CFSTR("Localized Strings"), &wind);
DisposeNibReference(nibRef);
ControlID alertID = {'strn',cmdId};
ControlRef alertRef;
GetControlByID(wind, &alertID,&alertRef);
GetControlData(alertRef, kControlNoPart, kControlStaticTextCFStringTag, sizeof(CFStringRef), &alertString, NULL);
return alertString;
}
Every time I call the function, I release the returned object.
CFStringRef lstr;
lstr = getStringFromLocalizedNIB(20);
//Use lstr;
CFRelease(lstr);
So can anybody please explain where the leak is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确,您没有显示使用 CreateWindowFromNib() 创建的窗口。我希望窗口具有相当于关闭时释放的 Carbon,并且 CreateWindowFromNib() 由 ShowWindow() 来平衡。不过我已经 9 年没有做过 Carbon 了,所以我不确定。
尝试在 Wind 上调用 DisposeWindow() 来平衡创建:
If I understand correctly, you aren't showing the window created using CreateWindowFromNib(). I would expect the window to have the Carbon equivalent of release-on-close, and the CreateWindowFromNib() to be balanced by a ShowWindow(). I haven't done Carbon in 9 years though, so I'm not sure.
Try calling DisposeWindow() on wind to balance the create: