c++ - pipe and stringstream - memory leak when writing in the stringstream -
i have memory issue code when try write binary data in stringstream object. valgrind log (first spotted system monitor):
==23562== 16,368 bytes in 1 blocks possibly lost in loss record 1,612 of 1,612 ==23562== @ 0x402a6dc: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==23562== 0x4c72213: std::string::_rep::_s_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.19) ==23562== 0x4c73332: std::string::_rep::_m_clone(std::allocator<char> const&, unsigned int) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.19) ==23562== 0x4c733d1: std::string::reserve(unsigned int) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.19) ==23562== 0x4c4e1ff: std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::overflow(int) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.19) ==23562== 0x4c52aec: std::basic_streambuf<char, std::char_traits<char> >::xsputn(char const*, int) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.19) ==23562== 0x4c48e8d: std::ostream::write(char const*, int) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.19) **==23562== 0x8140eac: msgmgt::update_stream() (msgmgt.cpp:524)** ==23562== 0x814192e: msgmgt::thread() (msgmgt.cpp:727) ==23562== 0x807614e: dlib::threaded_object::thread_helper() (threaded_object_extension.cpp:256) ==23562== 0x80ebec2: void dlib::dlib_create_new_thread_helper<dlib::threaded_object, &dlib::threaded_object::thread_helper>(void*) (create_new_thread_extension.h:24) ==23562== 0x80768c3: dlib::threads_kernel_shared::thread_starter(void*) (threads_kernel_shared.cpp:272)
the following function (udpate stream) checks pipe between process , current 1 , writes pipe content inside stringstream object, leads memory leak:
(i removed non necessary code sake of clarity)
std::stringstream _outputstream; unsigned int msgmgt::update_stream() { fd_set set; struct timeval timeout; //number of bytes read on pipe ssize_t read_bytes=0; /* initialize file descriptor set. */ fd_zero(&set); fd_set(_fd[0], &set); fd_set(_fd_stop[0], &set); //allowing stop select /* initialize timeout data structure. */ timeout.tv_sec = _timeout; timeout.tv_usec = 0; int ret; errno=0; if(!should_stop()){ ret = select(fd_setsize, &set, null, null, &timeout); if (ret > 0){ if(fd_isset(_fd[0],&set)){ //activity on pipe char* buffer=new char[data_max_length]; errno=0; //data transfer process pipe _outputstream read_bytes = read(_fd[0],buffer,data_max_length); //none blocking read if(read_bytes<0 && errno !=eagain){ //error in read }else{ //write data in stringstream //supposed memory leak _outputstream.write(buffer,read_bytes); } delete[] buffer; } } } return read_bytes; }
i don't understand what's going on. ideas ?
thank you,
pierre.
your buffer leaked, if _outputstream.write
throws exception.
you use smart pointer store pointer buffer or use vector buffer. both solutions automatically delete in case of exception.
Comments
Post a Comment