c++ 这样的内存泄漏怎么解决?

allmai_net 2014-10-30 02:29:11
typedef struct my_struct_{
std::string name;
std::string address;
}my_struct,*lp_my_struct;

class abc{
public:
abc();
~abc();

private:
my_struct packet_;
};

abc abc_;


程序结束时提示:
c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0 (592): std::allocator<std::_Container_proxy>::allocate
c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (671): std::_String_alloc<0,std::_String_base_types<char,std::allocator<char> > >::_Alloc_proxy + 0xF bytes
c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (650): std::_String_alloc<0,std::_String_base_types<char,std::allocator<char> > >::_String_alloc<0,std::_String_base_types<char,std::allocator<char> > > + 0xA bytes
c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (749): std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string<char,std::char_traits<char>,std::allocator<char> > + 0x3E bytes
...全文
305 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
leftbackfielder 2014-10-30
  • 打赏
  • 举报
回复
可能只是释放时间的问题. 你试一下,让string立即释放呢? string s; string().swap(s);
我看你有戏 2014-10-30
  • 打赏
  • 举报
回复
代码没问题啊
Saleayas 2014-10-30
  • 打赏
  • 举报
回复
这只是全局变量析构的时间问题。不是泄漏。
铖邑 2014-10-30
  • 打赏
  • 举报
回复
如果这样的代码都会内存泄露的话,那就只能是std::string的问题了
景语 2014-10-30
  • 打赏
  • 举报
回复
#include <string>

typedef struct my_struct_
{
    std::string name;
    std::string address;
} my_struct,*lp_my_struct;

class abc
{
    public:
         abc(){};
         ~abc(){};

    private:
          my_struct packet_;
};



int main()
{
    abc abc_;
    
    return 0;
}
这样用没问题啊,我编译运行过了
allmai_net 2014-10-30
  • 打赏
  • 举报
回复
显示泄漏的地方和关联的代码,我就没有用过new,malloc 之类的。 就像这样也会提示泄漏: class abc{ ..... public: std::string buffer_; } abc::abc():buffer_(){ } 然后我在其它地方这样用: abc inst; 然后退出程序,就会报上面的这个泄漏信息。
老王爱上猫 2014-10-30
  • 打赏
  • 举报
回复
还有其它代码吧, new后没delete?
linxin3333520 2014-10-30
  • 打赏
  • 举报
回复
引用 2 楼 coyerhc 的回复:
---------- Block 18388 at 0x000000006A8C0F30: 16 bytes ---------- Leak Hash: 0xF73FA629, Count: 1, Total 16 bytes Call Stack (TID 6080): f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\afxmem.cpp (313): mfc110d.dll!operator new c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0 (592): Demo.exe!std::allocator<std::_Container_proxy>::allocate c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (671): Demo.exe!std::_String_alloc<0,std::_String_base_types<char,std::allocator<char> > >::_Alloc_proxy + 0xF bytes c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (650): Demo.exe!std::_String_alloc<0,std::_String_base_types<char,std::allocator<char> > >::_String_alloc<0,std::_String_base_types<char,std::allocator<char> > > + 0xA bytes c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (749): Demo.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string<char,std::char_traits<char>,std::allocator<char> > + 0x3E bytes 这不算泄漏?
楼上的意思是,贴出来的代码看不出来内存泄漏,他让你把有关内存泄漏代码发出来
allmai_net 2014-10-30
  • 打赏
  • 举报
回复
---------- Block 18388 at 0x000000006A8C0F30: 16 bytes ---------- Leak Hash: 0xF73FA629, Count: 1, Total 16 bytes Call Stack (TID 6080): f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\afxmem.cpp (313): mfc110d.dll!operator new c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0 (592): Demo.exe!std::allocator<std::_Container_proxy>::allocate c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (671): Demo.exe!std::_String_alloc<0,std::_String_base_types<char,std::allocator<char> > >::_Alloc_proxy + 0xF bytes c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (650): Demo.exe!std::_String_alloc<0,std::_String_base_types<char,std::allocator<char> > >::_String_alloc<0,std::_String_base_types<char,std::allocator<char> > > + 0xA bytes c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (749): Demo.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string<char,std::char_traits<char>,std::allocator<char> > + 0x3E bytes 这不算泄漏?
铖邑 2014-10-30
  • 打赏
  • 举报
回复
这样根本就看不出来有没有内存泄露好不好?

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧