泄漏和无效* [英] leak and void*

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

问题描述




与指针,void *和析构函数有关的问题。


假设我有这样的代码;


int main(int argc,char ** argv)

{

X * x = new X();

void * tmp = x;

x = 0;

删除tmp;


返回0;

}


任何人都可以肯定地告诉我上面的代码会泄漏内存。我

意思是我在tmp指针上调用delete(类型为void *)但是

析构函数没有被调用。我*绝对*我手上有泄漏

在这里,不是我。我要问的原因是我正在评估的工具。

泄漏分析没有报告上述代码的任何内容。我看到

X析构函数没有被调用但是它仍然没有报告泄漏

给我。


Am我认为这是一个泄漏是正确的,不是我。为了不泄漏,我需要声明tmp;


X * tmp = x;

删除tmp;


这样就可以调用正确的析构函数。在

第一个场景中使用void *是一个泄漏,对吗?


谢谢


g

Hi,

Question relating to pointers, void* and destructors.

assuming I have code like this;

int main(int argc, char** argv)
{
X* x = new X();
void* tmp = x;
x = 0;
delete tmp;

return 0;
}

Can anybody tell me definitively that the above code leaks memory. I
mean I''m calling delete on the tmp pointer (type void*) however the
destructor isn''t being called. I *definitely* have a leak on my hands
here, don''t i. The reason I''m asking is cos the tool I''m evaluating for
leak analysis is not reporting anything for the above code. I see that
the X destructor is NOT called however it''s still not reporting leaks
to me.

Am I correct in thinking this is a leak, aren''t I. In order for it not
to leak, I would need to declare tmp thus;

X* tmp = x;
delete tmp;

That way the correct destructor will be called. Using a void* as in the
first scenario is a leak, correct?

thanks

g

推荐答案

Gr ***** @ nospam .com 写道:


有关指针,void *和析构函数的问题。

假设我有这样的代码;

int main(int argc,char ** argv)
{* X * x = new X();
void * tmp = x;
x = 0;
删除tmp;

返回0;
}
Hi,

Question relating to pointers, void* and destructors.

assuming I have code like this;

int main(int argc, char** argv)
{
X* x = new X();
void* tmp = x;
x = 0;
delete tmp;

return 0;
}




删除空指针有_undefined_ behavior。


引用5.3.5p2

在第一个备选(删除对象)中,操作数的值为

delete应该是指向非阵列对象的指针或指向表示这种对象的基类的

子对象(1.8)的指针(子句

10)。如果不是,则行为未定义。在第二个替代方案

(删除数组)中,delete的操作数的值应该是指针

的值,这是由前一个数组newexpression.IIf得到的,

行为未定义。


因为它是一个未定义的行为,它可能会也可能不会泄漏内存

甚至可能会覆盖整个记忆。基本上写这样一个

代码是一个严重错误。



Deleting a void pointer has _undefined_ behaviour .

Quoting 5.3.5p2
In the first alternative (delete object), the value of the operand of
delete shall be a pointer to a nonarray object or a pointer to a
subobject (1.8) representing a base class of such an object (clause
10). If not, the behavior is undefined. In the second alternative
(delete array), the value of the operand of delete shall be the pointer
value which resulted from a previous array newexpression.IIf not, the
behavior is undefined.

since it is an undefined behaviour, it might or might not leak memory
or might even overwrite the entire memory. Basically writing such a
code is a serious error.


>假设我有这样的代码;
> assuming I have code like this;

int main(int argc,char ** argv)
{
X * x = new X();
void * tmp = x;
x = 0;
删除tmp;

返回0;
}

任何人都可以告诉我明确地说上面的代码泄漏了内存。

int main(int argc, char** argv)
{
X* x = new X();
void* tmp = x;
x = 0;
delete tmp;

return 0;
}

Can anybody tell me definitively that the above code leaks memory.






删除`void *''未定义。

另外,valgrind在这里报告内存泄漏。

--Sumedha Swamy



Hi,
Deleting `void*'' is undefined.
Also, valgrind reports a memory leak here.
--Sumedha Swamy


来自软件验证的验证器不会在这里报告泄漏......

这就是我所追求的。我2天前才安装了这个工具,它看起来不错,但这很烦人。我支持他们看看他们要为自己说些什么。


我认为borland(版本6 cpp builder)正在做一些奇怪的事情(这是'b $ b允许任何它想要的东西,因为我们处于未定义的行为

领土,如你所指出的那样)。当上述分配在1循环中放入胖胖的
时,内存消耗不会超过

屋顶(保持静态/平坦)。即


而(1)

{

X * x =新X();

void * tmp = x;

x = 0;

删除tmp;

}

当我监控这个处理内存消耗保持不变然而

X ::〜X()永远不会被调用。编译器是否有可能在不调用dtor的情况下回收内存?b $ b?我的意思是,当我们进入未定义的行为时,它允许做任何*它想要的任何事情。那可能是因为我没有看到流程大小增加的原因。即Borland正在回收内存但是以HIS的方式进行,而不是调用dtor。或者是那个

废话,还有另一个完全合理的解释我正在看的

行为。


谢谢


G

Validator from software verify does not report a leak for me here...
that''s what I''m after. I''ve only installed the tool 2 days ago, it
looks good but this is annoying me. I''m onto their support to see what
they have to say for themselves.

I think borland (version 6 cpp builder) is doing something weird (it''s
allowed to anything it wants really as we''re in "undefined behaviour"
territory as you pointed out). The memory consumption isn''t going thru
the roof (stays static/flat) when the above allocation is put in a fat
while 1 loop. i.e.

while (1)
{
X* x = new X();
void* tmp = x;
x = 0;
delete tmp;
}
When I monitor this process the memory consumption stays flat, however
X::~X() is NEVER invoked. Is it possible for the compiler to reclaim
the memory without invoking the dtor? I mean, it''s allowed to do
*anything* it wants when we get into undefined behaviour. Could that
be why I see no increase in process size. i.e. Borland is reclaiming
the memory but doing it HIS way instead of calling the dtor. Or is that
nonsense and there''s another perfectly reasonably explanation for the
behaviour I''m seeing.

thanks

G


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

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