我有這個類AppController
和功能connectPlayer
:的std :: string內存泄露
/* 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)
任何想法?先謝謝你!
什麼是AppController :: TAG? – PiotrNycz
@PiotrNycz,它只是班級的名字。我用它在記錄器中寫入執行程序期間發生的重要事件。 –
擁有靜態持續時間的_non-POD_對象(就像你的'AppController :: TAG'是'std :: string')可能不是好事。您可能想嘗試像使用簡單的'const char []'一樣使用POD。另請參閱[本節來自Google的C++風格指南](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Static_and_Global_Variables)。 –