Std :: string memory leak

I have this AppController class and the connectPlayer function:

 /* 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 run 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!

+4
source share
3 answers

You have std::string objects in the global scope: AppController::TAG .

When the application ended not quite normally, you have such valgrind errors for global objects. Probably nothing to worry about.

If you (can not / do not want) change your program - read this document: http://valgrind.org/docs/manual/manual-core.html#manual-core.suppress to get rid of this error.

+7
source

Sometimes valgrind gives false positives. This means that even if valgrind says you are losing memory, you are not actually doing this.

The only thing to worry about is when you call exit() , as described in this question.

If you no longer want to see these warnings, you can create a suppressionions file that gives valgrind some information about which errors to ignore.

+1
source

I saw this problem once when I had a row in a class in a global scope. Valgrind continued to complain that I spilled my memory. I just β€œdeleted” the object on exit and the error went away.

+1
source

Source: https://habr.com/ru/post/1441147/


All Articles