Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::string memory leak

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!

like image 443
Maria Ines Parnisari Avatar asked Oct 21 '12 20:10

Maria Ines Parnisari


People also ask

How much memory does std::string take?

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):

How do you stop a memory leak in C++?

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.

Is std::string on heap?

The string object itself is stored on the stack but it points to memory that is on the heap.

How do I know if I have memory leaks C++?

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.


1 Answers

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.

like image 91
PiotrNycz Avatar answered Oct 24 '22 01:10

PiotrNycz