簡體   English   中英

管道和字符串流-寫入字符串流時發生內存泄漏

[英]pipe and stringstream - memory leak when writing in the stringstream

當我嘗試在stringstream對象中寫入二進制數據時,我的代碼存在內存問題。 Valgrind日志(首先在系統監視器中發現):

==23562== 16,368 bytes in 1 blocks are possibly lost in loss record 1,612 of 1,612
==23562==    at 0x402A6DC: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==23562==    by 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==    by 0x4C73332: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned int) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.19)
==23562==    by 0x4C733D1: std::string::reserve(unsigned int) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.19)
==23562==    by 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==    by 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==    by 0x4C48E8D: std::ostream::write(char const*, int) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.19)
**==23562==    by 0x8140EAC: MsgMgt::update_stream() (MsgMgt.cpp:524)**
==23562==    by 0x814192E: MsgMgt::thread() (MsgMgt.cpp:727)
==23562==    by 0x807614E: dlib::threaded_object::thread_helper() (threaded_object_extension.cpp:256)
==23562==    by 0x80EBEC2: void dlib::dlib_create_new_thread_helper<dlib::threaded_object, &dlib::threaded_object::thread_helper>(void*) (create_new_thread_extension.h:24)
==23562==    by 0x80768C3: dlib::threads_kernel_shared::thread_starter(void*) (threads_kernel_shared.cpp:272)

以下函數(udpate流)檢查另一個進程與當前進程之間的管道,並將該管道內容寫入stringstream對象內部,這會導致內存泄漏:

(為了清楚起見,我刪除了不必要的代碼)

std::stringstream _outputstream;

unsigned int MsgMgt::update_stream()
{
fd_set set;

struct timeval timeout;
//Number of bytes read on the pipe
ssize_t read_bytes=0;

/* Initialize the file descriptor set. */
FD_ZERO(&set);
FD_SET(_fd[0], &set);
FD_SET(_fd_stop[0], &set); //allowing to stop the select

/* Initialize the 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 THE PIPE
            char* buffer=new char[DATA_MAX_LENGTH];
            errno=0;
            //Data transfer process pipe to _outputstream
            read_bytes = read(_fd[0],buffer,DATA_MAX_LENGTH);  //None blocking read

            if(read_bytes<0 && errno !=EAGAIN){
            //Error in read
            }else{
            //write the data in the stringstream
            //supposed memory leak
                _outputstream.write(buffer,read_bytes); 
            }
        delete[] buffer;


        }
    }

}

return read_bytes;
}

我不明白發生了什么。 有任何想法嗎 ?

謝謝,

皮埃爾。

如果_outputstream.write引發異常,則緩沖區將泄漏。

您可以使用智能指針來存儲指向緩沖區的指針,也可以使用向量作為緩沖區。 如果出現異常,兩種解決方案都會自動將其刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM