I've got this class AppController
and the function 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:
==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!
As for sizeof(std::string) , typical values on desktop platforms range from 12 to 32 bytes. This is based on a few tests done using Compiler Explorer (link to the test, if you want to test other platforms):
The best way to avoid memory leaks in C++ is to have as few new/delete calls at the program level as possible – ideally NONE. Anything that requires dynamic memory should be buried inside an RAII object that releases the memory when it goes out of scope.
The string object itself is stored on the stack but it points to memory that is on the heap.
The most common and most easy way to detect is, define a macro say, DEBUG_NEW and use it, along with predefined macros like __FILE__ and __LINE__ to locate the memory leak in your code. These predefined macros tell you the file and line number of memory leaks.
You have std::string
objects in global scope: AppController::TAG
.
When application finished in not very normal way you've got these kind of valgrind errors for global objects. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With