简体   繁体   English

std :: string内存泄漏

[英]std::string memory leak

I've got this class AppController and the function connectPlayer : 我有这个类AppController和函数connectPlayer

/* AppController.h */
    class AppController
    {
          // Some other declarations ...
      private:
            static const string TAG;
    };

/* AppController.cpp */

#include "AppController.h"
const string AppController::TAG = "AppController";

AppController::AppController() {
    /* some code here...*/
}

void AppController::connectPlayer() {
    std::string port;
    std::string host;
    port = CM->getMenu()->getData("PORT");
    host = CM->getMenu()->getData("HOST");
    this->setState("Connecting...");
    Logger::info(TAG, "Port: " + port);
    Logger::info(TAG, "Host: " + host);
}

And when I execute the program, I get this from valgrind: 当我执行程序时,我从valgrind得到这个:

==7848== 25 bytes in 1 blocks are definitely lost in loss record 160 of 671
==7848==    at 0x402842F: operator new(unsigned int) (vg_replace_malloc.c:255)
==7848==    by 0x4210A83: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16)
==7848==    by 0x4212CF7: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16)
==7848==    by 0x4212E65: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16)
==7848==    by 0x8080501: AppController::connectPlayer() (in /home/maine/Escritorio/taller7542/UltimaVersion/src/main)

Any ideas? 有任何想法吗? Thank you in advance! 先感谢您!

You have std::string objects in global scope: AppController::TAG . 你在全局范围内有std::string对象: AppController::TAG

When application finished in not very normal way you've got these kind of valgrind errors for global objects. 当应用程序以非常正常的方式完成时,您会遇到全局对象的这种valgrind错误。 Probably nothing to worry. 可能没什么值得担心的。

If you (cannot/don't want to) change your program - read this doc: http://valgrind.org/docs/manual/manual-core.html#manual-core.suppress to get rid of this very error. 如果您(不能/不想)更改您的程序 - 请阅读此文档: http//valgrind.org/docs/manual/manual-core.html#manual-core.suppress以消除此错误。

Sometimes, valgrind gives false positives. 有时候,valgrind会给出误报。 This means that even if valgrind says you lose memory, actually you don't. 这意味着即使valgrind说你失去了记忆,实际上你也没有。

The only thing to worry about is when you call the exit() function, as explained in this question. 唯一要担心的是当你调用exit()函数时,如问题所述。

If you do not want to see these warnings any more, you can create a suppressions file giving valgrind some information on which errors to ignore. 如果您不想再看到这些警告,可以创建一个抑制文件,为valgrind提供一些有关要忽略的错误的信息。

I had seen this issue once when I had a string in a class in global scope. 当我在全局范围内的类中有一个字符串时,我曾经看过这个问题。 Valgrind kept complaining that I was leaking memory. Valgrind一直抱怨说我在泄漏记忆。 I just "deleted" the object at exit and the error was gone. 我只是在退出时“删除”了对象,错误消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM