直接传递字符串作为参数是否会导致c++内存泄漏
Can directly passing strings as parameter cause Memory leak in C++?
在我的MFC应用程序中,当我在Visual Studio中从调试模式关闭它时,输出窗口有这样的行-
f:ddvctoolsvc7libsshipatlmfcsrcmfcstrcore.cpp(156) : {415} normal block at 0x028BC410, 21 bytes long. Data: <T x > 54 CD FB 78 04 00 00 00 04 00 00 00 01 00 00 00 "
当我使用分配号创建断点时,它指向代码:
if (CMFCToolBar::GetUserImages() == NULL)
{
if (m_UserImages.Load(_T(".\UserImages.bmp"))) //call-stack points to this line.
{
CMFCToolBar::SetUserImages(&m_UserImages);
}
}
另一个示例如下:
CMainFrame::CMainFrame()
{
theApp.m_nAppLook = theApp.GetInt(_T("ApplicationLook"), ID_VIEW_APPLOOK_VS_2008);//call-stack points to this line.
}
我发现所有其他分配编号都导致字符串直接作为参数传递给函数的行。我的问题是:这真的是内存泄漏吗?如果是,那么如何避免这种情况;也许初始化一个CString变量并将其作为该函数的参数传递?
回答你的标题-字符串文字不需要被释放。
至于传递给函数——这取决于函数用它做什么。
例如,不管你传递给它什么,这个函数都会产生内存泄漏:
void foo(const char* s) {
strdup(s); // Memory leak!
}
foo("abc"); // There's a memory leak because foo just created a copy using strdup which it didn't free!
相关文章:
- valgrind-hellgrind与泄漏检查的结果不同
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 尽管遵循了规则,内存泄漏在哪里
- 为什么调用堆栈数组会导致内存泄漏
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 使用模板类的自定义列表类型中的内存泄漏
- 为什么以下C++代码中存在内存泄漏?
- Klocwork Inside的资源泄漏
- OpenCV 我应该使用智能指针来防止内存泄漏吗?
- 我是否生成线程并导致内存泄漏?
- 多线程程序中出现意外的内存泄漏
- 为什么此函数会导致内存泄漏?
- 在 C++ 库中使用cythonized python时内存泄漏
- 需要帮助查找内存泄漏
- C++功能泄漏内存,我是C++新手,不确定如何解决
- 瓦尔格林德的内存泄漏使用新的
- CPP 中的瓦尔格林德和记忆泄漏:"Conditional jump or move depends on uninitialised values"
- 无法找出我的代码中的内存泄漏