code
code

Reputation: 5642

Interpreting Valgrind Memory Leak Summary Log

I am using Valgrind (a memory leak tool) to find a potential memory leak. It was run as such:

$ valgrind --leak-check=full ./myApp

The following was reported:

==9458== 15,007 bytes in 126 blocks are possibly lost in loss record 622 of 622
==9458==    at 0x4029FDE: operator new(unsigned int) (vg_replace_malloc.c:313)
==9458==    by 0x415F213: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.19)
==9458==    by 0x4161125: 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.19)
==9458==    by 0x41617AF: 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.19)
==9458==    by 0x808B061: Parser::parseLinear(rapidxml::xml_node<char>*, Linear*) (Parser.cpp:663)

==9458== 
==9458== LEAK SUMMARY:
==9458==    definitely lost: 0 bytes in 0 blocks
==9458==    indirectly lost: 0 bytes in 0 blocks
==9458==      possibly lost: 20,747 bytes in 257 blocks
==9458==    still reachable: 57,052 bytes in 3,203 blocks
==9458==         suppressed: 0 bytes in 0 blocks
==9458== Reachable blocks (those to which a pointer was found) are not shown.
==9458== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==9458== 
==9458== For counts of detected and suppressed errors, rerun with: -v
==9458== ERROR SUMMARY: 21 errors from 21 contexts (suppressed: 0 from 0)

It looks like there is a "possibly lost" memory leak based on the summary. However, after tracking down line 663 in Parser.cpp, I can't seem to identify the issue. xml_node<>* is part of the open source library RapidXML. The source code looks like this:

line 661: Tracker track;
line 662: xml_node<>* trackingNode = node->first_node(); // rapidxml API
line 663: track.setValue(trackingNode->first_node()->value());

Where setValue is defined as:

void Tracker::setValue(const string& s) {
    this->val = s;
}

Upvotes: 2

Views: 747

Answers (1)

ralv
ralv

Reputation: 46

According to the rapidxml manual, xml_base::value() doesn't return zero-terminated string with the rapidxml::parse_no_string_terminators option.

Terminate your string according to xml_base::value_size() if this option was set.

Also, before calling xml_base::value() check if the value is not empty. In other case value() returns empty string that can be another memory leak issue.

Upvotes: 1

Related Questions