解决泄漏的最简单方法是什么 [英] What are the easiest ways to solve leaks

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

问题描述

追踪和解决泄漏的最简单方法是什么?

1.看一下代码,知道我做了什么:doh:
2.
3.

大家都可以帮我列出一个简单的清单吗?当然,请编辑以上内容.

两个类似,例如在可疑点之前或之后放入一行泄漏的代码".输出窗口中的第一件事是第一件事吗?我不是专家.

然后是三个,使用旧的.net进行提问;可以将某些行(#ifdef _DEBUG等)放在头文件中以获得对泄漏的更好链接"吗?我的问题是,我是否只需将五行代码复制到.cpp中,这将是一个好方法吗?

另一个愚蠢的问题是,报告的泄漏可能是谎言,还是误导.有没有人知道它会误导人?

我的目标是不必添加任何主要内容,只需解决问题即可避免泄漏.我想知道所有查找和修复泄漏的简单技巧.

也许四个应该是使用智能指针",或者更好的是,寻找循环共享指针并使用弱指针来打破循环".五个可能是检查虚拟析构函数",六个是寻找..."

谢谢!

回答1


谢谢克里斯蒂安.

该项目来自VC ++ 4.0,从一天的5.0和6.0跳到了.net,我的朋友认为,该项目的复杂性将真正受益于智能指针.

自学成才,我很幸运有一个才华横溢的程序员,在他的下班时间,教给我很多知识,他还贡献了代码和模式.有一天,他写了一组很棒的小巧的指针类.我最近一直在对其进行测试,要使它泄漏,我唯一能做的就是将一个新对象分配给一个弱指针,而不将其分配给共享指针.但是我为什么要这么做呢?我的意思是显然我可以删除弱指针,但是它不应该那样工作,对吧?

感谢您的回答.如果在NULL指针上调用delete很好,那为什么还要对其进行任何检查,为什么不只调用delete呢?

What are the easiest ways to track down and solve leaks?

1. Look at the code, know what I have done :doh:
2.
3.

Can you all help me make a good simple list? Sure, please edit the above.

Two would be something like, "Put a line of code that leaks before or after a suspected spot". Is the first thing in the output window the first thing that leaks? I''m not the expert.

And then three, using old .net, question; is it true you can put those certain lines (#ifdef _DEBUG etc) in the header file to get a better ''link'' to the leak? My question is, do I just copy the five lines of code to the .cpp, and would that be a good approach?

Another silly question is whether a reported leak can ever be a lie, or misleading. Has anyone ever known it to be misleading?

My goal is not have to add anything major, just solve the issue so it doesn''t leak. I''d like to know all the easy tricks to find and fix leaks.

Maybe four should be something like, "use smart pointers", or better yet, "Look for cyclic shared pointers and use a weak pointer to break the cycle". Five could be, "check for virtual destructors", six "look for..."

Thanks!

Response to Answer 1


Thanks Christian.

The project came from VC++ 4.0 and skipped up one day through 5.0 and 6.0 and on to .net and my friend thought the complexity in project would really benefit from smart pointers.

Self taught, I was lucky to have a talented programmer, in his off-hours, teach me tons and he also contributed code and patterns. One day he wrote a great little smart pointer set of classes. I''ve been testing it lately and the only thing I can do to make it leak is assign a new object to a weak pointer, without in turn assigning that to shared pointer. But why would I ever do that? I mean obviously I could delete the weak pointer but it''s not supposed to work like that, right?

I appreciated your answer. If calling delete on a NULL pointer is fine then why even do any checking on it it, why not just call delete?

推荐答案

您可以购买泄漏检测工具,并且我认为VS也能做到.有些命中率是错误的,但是它们会向您显示他们认为已泄漏的数据,因此您可以确定它是什么,然后找出它在哪里.

you can buy leak detection tools, and I think VS does some as well. Some of the hits are false, but they will show you the data they think has leaked, so you can work out what it is and then figure out where it is.

Brian Bennett写道:
Brian Bennett wrote:

是否可以将某些行(#ifdef _DEBUG等)放在头文件中以获得更好的链接? "到泄漏?

is it true you can put those certain lines (#ifdef _DEBUG etc) in the header file to get a better ''link'' to the leak?



我不知道如何. #define是令人讨厌的东西,我看不出它们如何帮助进行泄漏检测.

我想这意味着您来自.NET,对管理自己的内存感到恐惧吗?真的不是那么难.只需存储您使用的所有指针,并在需要时清理它们即可.主要技巧是删除NULL.什么也没做.清理代码应检查指针是否为NULL,如果不是,则将其删除,然后立即将它们设置为NULL.



I don''t see how. #defines are nasty things, and I don''t see how they can help with leak detection.

I guess this means you''re coming from .NET and freaked out about managing your own memory ? It''s really not that hard. Just store any pointers you use, and clean them up when you need to. The main trick is, delete NULL; does nothing. Clean up code should check if pointers are NULL, and if they are not, delete them then IMMEDIATELY set them to NULL.


这是一个好问题.我想这只是检查的好形式,而不是不断删除并将其分配为null.

我从来没有真正使用过智能指针,而且很长一段时间都没有使用过C ++.您在谈论调用函数和方法时的泄漏,因此答案就在那些方法的代码中.
That''s a good question. I guess it''s just good form to check, rather than keep deleting and assigning it to null.

I''ve never really used smart pointers much, and I''ve not used C++ in a long time. You''re talking about leaks when you call functions and methods, so the answer would lie in the code of those methods.


这篇关于解决泄漏的最简单方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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