内存泄漏C ++ [英] Memory leak C++

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

问题描述

我刚刚用C ++写了一个代码,该代码可以进行一些字符串操作,但是当我运行valgrind时,它显示出一些可能的内存泄漏.将代码调试到精细级别,我编写了一个简单的C ++程序,如下所示:

I just wrote a code in C++ which does some string manipulation, but when I ran valgrind over, it shows some possible memory leaks. Debugging the code to granular level I wrote a simple C++ program looking like:

#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
        std::string myname("Is there any leaks");
        exit(0);
}

然后在上面运行valgrind:

and running valgrind over it I got:

==20943== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 26 from 1)
==20943== malloc/free: in use at exit: 360,645 bytes in 12,854 blocks.
==20943== malloc/free: 65,451 allocs, 52,597 frees, 2,186,968 bytes allocated.
==20943== For counts of detected errors, rerun with: -v
==20943== searching for pointers to 12,854 not-freed blocks.
==20943== checked 424,628 bytes.
==20943== 
==20943== LEAK SUMMARY:
==20943==    definitely lost: 0 bytes in 0 blocks.
==20943==      possibly lost: 917 bytes in 6 blocks.
==20943==    still reachable: 359,728 bytes in 12,848 blocks.
==20943==         suppressed: 0 bytes in 0 blocks.
==20943== Reachable blocks (those to which a pointer was found) are not shown.
==20943== To see them, rerun with: --show-reachable=yes

然后让我感到震惊的是,我们已经强制退出(我也用原始的C ++代码执行了该操作).现在的问题是,我想从程序中退出,因为我之前的旧代码正在等待新代码的退出状态.例如,二进制a.out等待b.out的退出状态.有什么办法可以避免内存泄漏,或者我真的应该担心内存泄漏,因为程序已经在那一点上退出了.

Then it struck me that we have forcefully exited (which i performed in my original C++ code as well). Now the problem is that I want to exit from the program as my previous old code waits for the exit status of the new code. For e.g binary a.out waits for the exit status of b.out. Is there any way to avoid the memory leaks, or should i really worry about the memory leaks as the program is already exiting at that point.

这对我也提出了另一个问题,这样的代码有害吗?

This also raise another question for me, is such a code harmful?

#include<stdio.h>
#include<cstdlib>
int main()
{
        char *p=(char *)malloc(sizeof(char)*1000);
        exit(0);
}

推荐答案

如果您坚持继续使用exit():

#include<iostream>
int main(){
    {
        std::string myname("Are there any leaks?");
    }
    exit(0);
}

此外,当您从main返回时,返回的值将成为应用程序的退出代码.因此,如果要传递退出代码,请在main()中使用return exitCode;而不是exit.

Also, when you return from main the returned value becomes the application’s exit code. So if you want to pass an exit code, use return exitCode; in main() instead of exit.

关于那部分:

这对我也提出了另一个问题,这样的代码有害吗?

This also raise another question for me, is such a code harmful?

是的,因为这是 BAD 的编程习惯.

Yes, because it is a BAD programming habit.

操作系统将清理您未能释放的所有内存,因此,只要您没有设法吃光所有系统内存和页面文件,就不应损坏操作系统.

The OS will clean up any memory you failed to release, so as long as you haven't managed to eat all system memory and the page file, you shouldn't damage the OS.

但是,编写草率的代码可能会变成习惯,因此依靠操作系统清理混乱是一个坏主意.

However, writing sloppy/leaky code might turn into habit, so relying on the OS for cleaning up your mess is a bad idea.

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

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