deque中的内存泄漏? [英] Memory Leak in deque ?

查看:351
本文介绍了deque中的内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



您好,


我声明了以下类型:


typedef pair< long,string> CEventPair;


typedef deque< CEventPair *> EventPairCont;


//这里我添加一些新元素


//我是一个很长的


// value是一个字符串


//事件是EventPairCont


if((npair = new CEventPair(l,value)) != NULL){


events.push_back(npair);


}


/ /这里我用这个做点什么


...


//最后我用clear-method来销毁所有


events.clear();


关机后Boundschecker报告我使用新的CEventPair分配在

行中的内存泄漏。如果我单步执行代码,我会为字符串分配

内存。但是我没有进入字符串的析构函数

从清除()


问题出在哪里?


感谢所有人。

-

通过发表http://dbforums.com

推荐答案

stp写道:
你好,
<关闭后Boundschecker报告我使用新的CEventPair分配在
行中的内存泄漏。如果我单步执行代码,我会为字符串分配内存。但是我不进入字符串的析构函数
从清除()

问题出在哪里?
Hello,

After Shutdown Boundschecker reports me Memory leaks allocated in the
line with new CEventPair. If I step throught code I do allocation
memory for string. But I dont step into the destructor of the string
down from clear ()

Where is the problem ?




*你*正在为CEventPair分配内存,所以*你*应该清理它。你不能指望deque删除你存储在其中的指针(毕竟,它不会知道你是否还有一个位于某处的引用)。你需要存储非指针,但是对象本身(对于deque或delete中的对象提供的复制构造函数,它们可以工作)你自己分配的

指针。


-

不可饶恕


" ;大多数人总结

Freek de Jonge



*You* are allocating memory for CEventPair, so *you* should clean it up. You
cannot expect deque to delete the pointers you store in it (after all, it
cannot know if you still have a reference lying around somewhere). You
either need to store non-pointers, but the objects themselves (that''ll work
fine with the copy-constructor supplied by pair) in the deque or delete the
pointers you allocated yourself.

--
Unforgiven

"Most people make generalisations"
Freek de Jonge




" stp" <我********* @ dbforums.com>在消息新闻中写道:33 **************** @ dbforums.com ...

"stp" <me*********@dbforums.com> wrote in message news:33****************@dbforums.com...
//最后我使用clear-摧毁所有事件的方法

events();
// at the end I use clear-method to destroy all

events.clear ();




这并不会破坏任何CEventPairs。你的容器只有
存储指针。仅清除或删除容器

销毁存储在其中的指针,它不会在

每个元素上调用delete。你需要遍历deque并且

删除每个元素。


C ++设计通常是对称的。你删除

中的东西的方式比它们创建的方式要好。



That doesn''t destory any CEventPairs. Your container only
stores pointers. Clearing or deleting the container only
destroys the pointers stored in it, it doesn''t call delete on
each element. You need to iterate over the deque and
delete each element.

C++ design is usually symetrical. You delete things in
a complementary manner than they were created.




" stp" <我********* @ dbforums.com>在消息中写道

news:33 **************** @ dbforums.com ...

"stp" <me*********@dbforums.com> wrote in message
news:33****************@dbforums.com...

你好,

我声明了以下类型:

typedef pair< long,string> CEventPair;

typedef deque< CEventPair *> EventPairCont;

//这里我添加了一些新元素

//我是一个很长的
//值是一个字符串

//事件是EventPairCont

if((npair = new CEventPair(l,value))!= NULL){

events.push_back(npair);

}
// //这里我用这个做点什么


//最后我用的清除所有事件

events.clear();

关机后Boundschecker报告我在新的CEventPair的
行中分配的内存泄漏。如果我单步执行代码,我会为字符串分配内存。但是我没有进入字符串的析构函数
从清除()

问题出在哪里?

感谢所有人。

-
通过 http://dbforums.com 发布



尝试使用autoptr或在析构函数中编写一个循环以清除内存

CEventPair等


Try using autoptr or write a loop in your destructor to clear the memory in
CEventPair etc


这篇关于deque中的内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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