Memory leaks after using typeinfo::name()

Posted by icabod on Stack Overflow See other posts from Stack Overflow or by icabod
Published on 2011-11-29T09:46:59Z Indexed on 2011/11/29 9:50 UTC
Read the original article Hit count: 218

Filed under:
|
|
|

I have a program in which, partly for informational logging, I output the names of some classes as they are used (specifically I add an entry to a log saying along the lines of Messages::CSomeClass transmitted to 127.0.0.1). I do this with code similar to the following:

std::string getMessageName(void) const {
    return std::string(typeid(*this).name());
}

And yes, before anyone points it out, I realise that the output of typeinfo::name is implementation-specific.

According to MSDN

The type_info::name member function returns a const char* to a null-terminated string representing the human-readable name of the type. The memory pointed to is cached and should never be directly deallocated.

However, when I exit my program in the debugger, any "new" use of typeinfo::name() shows up as a memory leak. If I output the information for 2 classes, I get 2 memory leaks, and so on. This hints that the cached data is never being freed.

While this is not a major issue, it looks messy, and after a long debugging session it could easily hide genuine memory leaks.

I have looked around and found some useful information (one SO answer gives some interesting information about how typeinfo may be implemented), but I'm wondering if this memory should normally be freed by the system, or if there is something i can do to "not notice" the leaks when debugging.

I do have a back-up plan, which is to code the getMessageName method myself and not rely on typeinfo::name, but I'd like to know anyway if there's something I've missed.

© Stack Overflow or respective owner

Related posts about c++

Related posts about visual-c++