roscpp and valgrind: memory leaks

asked 2014-02-16 22:58:55 -0500

Mandar gravatar image

updated 2014-02-17 00:09:51 -0500

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 ...
(more)
edit retag flag offensive close merge delete

Comments

There probably aren't a big deal. Can you run with `--leak-check=full` to confirm the source of the memory leaks?

ahendrix gravatar imageahendrix ( 2014-02-16 23:42:12 -0500 )edit

Also, could You check Your valgrind version, please? Updating it usually helps whipping out some of false-positives.

Kamiccolo gravatar imageKamiccolo ( 2014-02-17 00:57:41 -0500 )edit

Well I'm on the latest one for Ubuntu 12.04 - running valgrind-3.7.0

Mandar gravatar imageMandar ( 2014-02-17 01:24:56 -0500 )edit

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.

Kamiccolo gravatar imageKamiccolo ( 2014-02-17 02:52:29 -0500 )edit

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)` and `return 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.7

Mandar gravatar imageMandar ( 2014-02-17 03:26:58 -0500 )edit

Changing to Ubuntu 12.10 won't change a damn thing. Getting up-to-date Valgrind probably will. Good luck with that! :)

Kamiccolo gravatar imageKamiccolo ( 2014-02-17 03:33:28 -0500 )edit