当我push_back到向量时,析构函数被调用 [英] Destructor is called when I push_back to the vector

查看:196
本文介绍了当我push_back到向量时,析构函数被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个类定义:

  class FlashStream 
{
public:
显式的FlashStream(const char * url,vector< uint8> * headers,vector< uint8> * data,void * ndata,void * notifyData = NULL,uint32 lastModified = NULL);
〜FlashStream();
private:
NPStream _stream;
// ...
}

NPStream description



及其实现:

/ p>

  FlashStream :: FlashStream(const char * url,vector< uint8> * headers,vector< uint8> * data,void * ndata ,void * notifyData,uint32 lastModified)
{
// ...
memset(& _stream,0,sizeof(NPStream));

_stream.headers = new char [data-> size()];

memcpy((void *)_stream.headers,&(* data)[0],data-> size());
// ...
}

FlashStream ::〜FlashStream()
{
// ...
if .headers)
delete [] _stream.headers;
_stream.headers = NULL;
// ...
}

现在, :

  // ... 
vector< FlashStream> _streams;
// ...
_streams.push_back(FlashStream(url,headers,data,_npp.ndata,notifyData,lastModified));
// ...

有时我在 FlashStream (当我 push_back() c $ c>到向量< FlashStream> _streams



我阅读了这个问题在SO 和另外几个,但是所有相同的不知道如何优雅和有效地解决问题。可能是问题是在复制构造函数,但我不知道如何可以使用内存分配 NPStream.headers NPStream.url


解决方案

p $ p> _streams.push_back(FlashStream(url,headers,data,_npp.ndata,notifyData,lastModified));

等效于:

  {
FlashStream temp(url,headers,data,_npp.ndata,notifyData,lastModified);
_streams.push_back(temp);
// temp get destroyed here
}

FlashStream对象被复制到向量中,然后被破坏。您可以使用C ++ 11中的 emplace_back()避免这种情况:

  _streams.emplace_back(url,headers,data,_npp.ndata,notifyData,lastModified); 


I have this class definition:

class FlashStream
{
public:
    explicit FlashStream(const char * url, vector<uint8> * headers, vector<uint8> * data, void * ndata, void * notifyData = NULL, uint32 lastModified = NULL);
    ~FlashStream();
private:        
    NPStream      _stream;
    // ...
}

(NPStream description)

and its implemetation:

FlashStream::FlashStream(const char * url, vector<uint8> * headers, vector<uint8> * data, void * ndata, void * notifyData, uint32 lastModified)
{
    // ...
    memset(&_stream, 0, sizeof(NPStream));

    _stream.headers = new char[data->size()]; 

    memcpy((void*)_stream.headers, &(*data)[0], data->size());
    // ...
}

FlashStream::~FlashStream()
{
    // ...
    if(_stream.headers)
        delete [] _stream.headers;
    _stream.headers = NULL;
    // ...
}

Now, when I run this code:

// ...
vector<FlashStream> _streams;
// ...
_streams.push_back(FlashStream(url, headers, data, _npp.ndata, notifyData, lastModified));
// ...

Sometimes I have an error at delete [] _stream.headers; in the destructor of FlashStream, which is called when I push_back() to the vector<FlashStream> _streams.

I read this question on SO and a few another, but all the same don't know how to elegantly and efficiently fix the problem. May be the problem is in copy constructor, but I don't know how I can make it with memory allocation for NPStream.headers and NPStream.url?

解决方案

This statement:

_streams.push_back(FlashStream(url, headers, data, _npp.ndata, notifyData, lastModified));

is equivalent to:

{
    FlashStream temp(url, headers, data, _npp.ndata, notifyData, lastModified);
    _streams.push_back(temp);
    // temp gets destroyed here
}

so, you create a temporary FlashStream object that is copied into the vector, then destructed afterwards. You can avoid this by using emplace_back() in C++11:

_streams.emplace_back(url, headers, data, _npp.ndata, notifyData, lastModified);

这篇关于当我push_back到向量时,析构函数被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆