roscpp and valgrind: memory leaks
I've been building and testing a program that reads the serial port and the publishes some data off to a topic. Now in the course of checking memory leaks, I realized that just declaring a Publisher or Timer gives rise to memory leaks of the type 'possibly lost'. Here's the basic code I have. It does nothing:
int main(int argc, char **argv)
{
std::string node_name = "node";
std::string pub_type;
std::string init_time;
ros::init(argc, argv,"talker");
ros::Timer timer2;
ros::Publisher pubsensID;
ros::Publisher chatter_pub[10] = {};
ros::NodeHandle n;
pubsensID = n.advertise<std_msgs::String>("SensorIDs", 1000);
std::cout<<"Publisher service started with this configuration: "<<endl;
std::cout<<"\t#Node name: "<< node_name <<endl;
ros::shutdown();
close(sd);
return status;
}
If I comment out the lines declaring the ros::Timer
and ros::Publisher
I get a clean output from Valgrind. If not, then I get this:
[EDIT: Reran it with -leak-check=full option as suggested by @ahendrix]
HEAP SUMMARY:
in use at exit: 1,421 bytes in 43 blocks
total heap usage: 2,877 allocs, 2,834 frees, 137,330 bytes allocated
16 bytes in 1 blocks are possibly lost in loss record 11 of 39
at 0x402B9B4: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
by 0x44027D3: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16)
by 0x146B003: ???
16 bytes in 1 blocks are possibly lost in loss record 12 of 39
at 0x402B9B4: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
by 0x44027D3: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16)
by 0x4404A47: 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)
by 0x4404BB5: 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)
by 0x474B805: log4cxx::Level::getAll() (in /usr/lib/liblog4cxx.so.10.0.0)
by 0x474171D: log4cxx::Hierarchy::Hierarchy() (in /usr/lib/liblog4cxx.so.10.0.0)
by 0x380170BF: ??? (in /usr/lib/valgrind/memcheck-x86-linux)
17 bytes in 1 blocks are possibly lost in loss record 13 of 39
at 0x402B9B4: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
by 0x44027D3: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16)
by 0x4404A47: 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)
by 0x4404BB5: 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)
by 0x474BDA5: log4cxx::Level::getInfo() (in /usr/lib/liblog4cxx.so.10.0.0)
by 0x4641D30: __static_initialization_and_destruction_0(int, int) (in /home/mharshe/ros/core/devel/lib/librosconsole.so)
by 0x4641F5E: _GLOBAL__sub_I_rosconsole.cpp (in /home/mharshe/ros/core/devel/lib/librosconsole.so)
by 0x400EEAA: call_init (dl-init.c:85)
by 0x400EF93: _dl_init (dl-init.c:134)
by 0x400120E: ??? (in /lib/i386-linux-gnu/ld-2.15.so)
[snip]
LEAK SUMMARY:
definitely lost: 0 bytes in 0 blocks
indirectly lost: 0 bytes in 0 blocks
possibly lost: 233 bytes in 11 blocks
still reachable: 1,188 bytes in 32 blocks
suppressed: 0 bytes in 0 blocks
Reachable blocks (those to which a pointer was found) are not shown.
To see them, rerun with: --leak-check=full --show-reachable=yes
For counts of detected and suppressed errors, rerun with: -v
ERROR SUMMARY: 11 errors from 11 contexts (suppressed: 0 from 0)
Should I just ignore this and deal with the rest of the errors? Or will these memory leaks come and bite me later on?
Asked by Mandar on 2014-02-16 23:58:55 UTC
Comments
There probably aren't a big deal. Can you run with
--leak-check=full
to confirm the source of the memory leaks?Asked by ahendrix on 2014-02-17 00:42:12 UTC
Also, could You check Your valgrind version, please? Updating it usually helps whipping out some of false-positives.
Asked by Kamiccolo on 2014-02-17 01:57:41 UTC
Well I'm on the latest one for Ubuntu 12.04 - running valgrind-3.7.0
Asked by Mandar on 2014-02-17 02:24:56 UTC
With valgrind 3.9.0 and Ubuntu 12.10 it outputs this: definitely lost: 0 bytes in 0 blocks, indirectly lost: 0 bytes in 0 blocks. Next time, please, be so kind and provide code snippet ready to compile. Sadly, Valgrind webpage is down, so can't point an exact bug-fix. Stumbled upon similar issue few days ago - older version of Valgrind couldn't work with some optimizations used in string operations.
Asked by Kamiccolo on 2014-02-17 03:52:29 UTC
Sorry, I guess I had some left-over statements that I didn't snip out properly while copying here. But I removed the
close(sd)
andreturn status
commands, made a new package & ran it. I get the same leaks. Can't/Shouldn't really change to ubuntu12.10 right now. Guess it's valgrind 3.7Asked by Mandar on 2014-02-17 04:26:58 UTC
Changing to Ubuntu 12.10 won't change a damn thing. Getting up-to-date Valgrind probably will. Good luck with that! :)
Asked by Kamiccolo on 2014-02-17 04:33:28 UTC