我有这个类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);
}

当我执行程序时,我从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)

有任何想法吗?先感谢您!

最佳答案

您在全局范围内有std::string对象:AppController::TAG

当应用程序以不太正常的方式完成时,对于全局对象,您将遇到此类valgrind错误。也许没有什么可担心的。

如果您(无法/不想)更改程序,请阅读此文档:http://valgrind.org/docs/manual/manual-core.html#manual-core.suppress以摆脱此错误。

关于c++ - std::string内存泄漏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13001784/

10-11 10:46