我不太喜欢 Boost 库。我需要它来匹配我在 VS 2008 中编译的遗留 C++/MFC 项目中的正则表达式。
这是一个简短的代码片段:
//Need Unicode support
CString ss;
boost::tregex r(L"<(From )?([A-Za-z0-9_]+)>(.*?)");
boost::tmatch what;
CString chat_input(L"<Darker> Hello");
if(boost::regex_match(chat_input, what, r)) //Memory leak on this line
{
// extract $1 as a CString:
ss = L"Match";//CString(what[1].first, what.length(1));
}
else
{
ss = L"No match";
}
当我在 Debug模式下运行它时,我在输出窗口中得到以下内容:
Detected memory leaks!
Dumping objects ->
{287} normal block at 0x00460068, 4096 bytes long.
Data: < > 00 00 00 00 CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
如果我注释掉 boost::regex_match
行,泄漏就会消失。
我在这里错过了什么?
附言。我链接到的 boost 版本是 1_55_0。
最佳答案
单元测试框架CppUnit ,还提示 boost::regex
库中的“内存泄漏”。
我认为这是因为 boost::regex
实现使用静态成员变量,如“Meyers”单例,内存泄漏检测不喜欢它。 boost 库没有问题,它肯定不是泄漏内存!
在 MSVC 2010 之后,您可以使用 std::regex
代替,我可以向您保证不会报告任何泄漏。在 MSVC 2008 中,您应该能够使用 std::tr1::regex
,但很抱歉,我对此没有任何经验。
关于c++ - boost::regex 库中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20133961/