Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 管道和stringstream-在stringstream中写入时内存泄漏_C++ - Fatal编程技术网

C++ 管道和stringstream-在stringstream中写入时内存泄漏

C++ 管道和stringstream-在stringstream中写入时内存泄漏,c++,C++,当我试图在stringstream对象中写入二进制数据时,我的代码有内存问题。 Valgrind日志(首先通过系统监视器发现): ==23562==1个块中的16368字节可能在1612的丢失记录1612中丢失 ==23562==at 0x402A6DC:运算符new(unsigned int)(在/usr/lib/valgrind/vgpreload\u memcheck-x86-linux.so中) ==23562==by 0x4C72213:std::string::_Rep::_S_cr

当我试图在stringstream对象中写入二进制数据时,我的代码有内存问题。 Valgrind日志(首先通过系统监视器发现):

==23562==1个块中的16368字节可能在1612的丢失记录1612中丢失
==23562==at 0x402A6DC:运算符new(unsigned int)(在/usr/lib/valgrind/vgpreload\u memcheck-x86-linux.so中)
==23562==by 0x4C72213:std::string::_Rep::_S_create(unsigned int,unsigned int,std::allocator const&)(in/usr/lib/i386 linux gnu/libstdc++.so.6.0.19)
==23562==by 0x4C73332:std::string::_Rep::_M_clone(std::allocator const&,unsigned int)(in/usr/lib/i386 linux gnu/libstdc++.so.6.0.19)
==23562==by 0x4C733D1:std::string::reserve(unsigned int)(在/usr/lib/i386 linux gnu/libstdc++.so.6.0.19中)
==23562==by 0x4C4E1FF:std::basic_stringbuf::overflow(int)(in/usr/lib/i386 linux gnu/libstdc++.so.6.0.19)
==23562==by 0x4C52AEC:std::basic_streambuf::xsputn(char const*,int)(在/usr/lib/i386 linux gnu/libstdc++.so.6.0.19中)
==23562==by 0x4C48E8D:std::ostream::write(char const*,int)(在/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\u object::thread\u helper()(threaded\u object\u extension.cpp:256)
==23562==by 0x80EBEC2:void dlib::dlib_create_new_thread_helper(void*)(create_new_thread_extension.h:24)
==23562==0x80768C3:dlib::threads\u kernel\u shared::thread\u starter(void*)(threads\u kernel\u shared.cpp:272)
以下函数(udpate stream)检查另一个进程与当前进程之间的管道,并将管道内容写入stringstream对象中,从而导致内存泄漏:

(为了清晰起见,我删除了不必要的代码)

std::stringstream\u outputstream;
unsigned int MsgMgt::update_stream()
{
fd_集;
结构timeval超时;
//管道上读取的字节数
ssize_t read_bytes=0;
/*初始化文件描述符集*/
FD_零位(&set);
FD_集(_FD[0],&SET);
FD_SET(_FD_stop[0],&SET);//允许停止选择
/*初始化超时数据结构*/
timeout.tv_sec=_timeout;
timeout.tv_usec=0;
int ret;
errno=0;
如果(!应该停止()){
ret=select(FD_SETSIZE,&set,NULL,NULL,&timeout);
如果(ret>0){
if(FD_ISSET(_FD[0],&set)){
//管道上的活动
字符*缓冲区=新字符[数据最大长度];
errno=0;
//数据传输过程管道到_outputstream
read_bytes=read(_fd[0],缓冲区,数据_MAX_LENGTH);//无阻塞读取

if(read_bytes缓冲区将泄漏,if
\u outputstream.write
抛出异常


您可以使用智能指针存储指向缓冲区的指针或使用向量作为缓冲区。这两种解决方案都会在出现异常时自动删除它。

哪里定义了
\u outputstream
(它是否已销毁?)嗨,\u outputstream在头文件中定义(它是类的私有属性)。在这个函数中它没有被破坏,因为内容是在另一个函数中读取的。谢谢您的回答。但是,写入函数没有发送任何异常。我还检查了stringstream fail()函数,该函数返回false。
==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)
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;
}