简体   繁体   English

取消分配内存时发生访问冲突

[英]Access Violation when Deallocating Memory

I'm currently going through Accelerated C++ and I'm trying to create my version of in CH11. 我目前正在使用Accelerated C ++,并且正在尝试在CH11中创建我的版本。 When trying to deallocate the memory through the destructor I get the read access memory whether I create an empty "Vec" or one using pushback. 尝试通过析构函数释放内存时,无论是创建空的“ Vec”还是使用推回创建一个“ Vec”,我都会获得读访问内存。 I'm not really sure what I'm missing. 我不太确定自己缺少什么。 Thanks for any help. 谢谢你的帮助。

#include "Vec.h"
int main()
{
Vec<int> v;

return 0;
}

///////// /////////

#include <algorithm>
#include <cstddef>
#include <memory>


using std::max;

/////////////////////////////
template <class T> class Vec {
public:
    typedef T* iterator;
    typedef const T* const_iterator;
    typedef size_t size_type;
    typedef T value_type;
    typedef T& reference;
    typedef const T& const_reference;

    //Constructor
    Vec() { create();}
    explicit Vec(size_type n, const T& = T()) {create(n,t);}

    //Copy
    Vec(const Vec& v) {create(v.begin(), v.end());}
    //Assignment
    Vec& operator=(const Vec&);
    //Destructor
    ~Vec() { uncreate();}

    //Indexing
    T& operator[](size_type i) {return data[i]; }
    const T& operator[](size_type i) const { return data[i]; }



    //Size
    size_type size() const { return avail - data; } 

    //Return member variables
    iterator begin() { return data; }
    const_iterator begin() const { return data; }

    iterator end() { return avail; }
    const_iterator end() const {return avail; }

    //Uncreate
    void clear() { uncreate(); }
    //Check empty
    bool empty() const { return data == avail; }
//////////////////////
private:
    iterator data; //first
    iterator avail; //(one past) last element
    iterator limit; //(one past) allocated memory

    std::allocator<T> alloc; // object for memory allocation

    //allocate and initialize thorugh constructors
    void create();
    void create( size_type, const T&);
    void create(const_iterator, const_iterator);

    //destory 
    void uncreate();


};
////////////////////////////
template <class T> void Vec<T>::create()
{
    data = avail = limit;
}

template <class T> void Vec<T>::create(size_type n, const T& val)
{
    data = alloc.allocate(n , 0);
    limit = avail = data + n;
    std::uninitialized_fill(data, limit, val);
}

template <class T> void Vec<T>::create(const_iterator i, const_iterator j)
{
    data = alloc.allocate(j - i, 0);
    limit = avail = std::uninitialized_copy(i , j, data);
}

template <class T> void Vec<T>::uncreate()
{
    if(data) {
        iterator it = avail;
        while(it != data)
            alloc.destroy(--it);
        alloc.deallocate(data, limit - data);
    }
    data = limit = avail = 0;
}

Both constructors need to initialize their members in an initializer list prior to usage, (which SB pointed out are being used in create() prior to having been assigned determinate values). 这两个构造函数都需要在使用之前在初始化器列表中初始化其成员(SB指出在create()分配确定值之前已在create()使用该成员)。 When the destructor is fired you may well be using a indeterminate pointer value, and sending it to be deallocated. 当析构函数被触发时,您很可能会使用不确定的指针值,并将其发送给释放对象。

Change this: 更改此:

Vec() { create();}
explicit Vec(size_type n, const T& = T()) {create(n,t);}

To this: 对此:

Vec() : data(), avail(), limit() { create();}
explicit Vec(size_type n, const T& t = T()) 
    : data(), avail(), limit() {create(n,t);}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM