开发者

error in class destructor

I just started my container class and already I'm having issues:

class Container
{
    private:

    string* BasePointer; // The starting pointer.
    unsigned int Capacity; // The number of values the container can hold.

    public:

    Container() // Default constructor.
    {
        Capacity = 1;
        BasePointer = new string[Capacity];
    }

    ~Container() // Destructor.
    {
        delete BasePointer; // Delete the container to prevent memory leaking.
    }
};

I get the error Container Classes(26467) malloc: *** error for object 0x100100088: po开发者_StackOverflowinter being freed was not allocated. What am I doing incorrectly?


The XXX ptr = new XXX[size] should be matched with array version delete [] ptr, not just regular delete.

Read about free store management in C++, and as James reminds us - follow the rule of three in cases like this.


you must use delete[]

delete[] BasePointer;


Others have mentioned that you have mismatched new[] with delete and you must change delete to delete[] to fix it. However, this is just the first of your issues.

You also need to implement (or at least declare as private) the copy constructor and copy assignment operator. Otherwise, think about what happens when you do this:

{
    Container c1;
    Container c2(c1);
}   // c2.~Container(); // frees the memory pointed to by 'BasePointer'
    // c1.~Container(); // also frees the memory pointed to by 'BasePointer'.

Since the BasePointer member of both c1 and c2 points to the same array, it gets freed twice.

There are some easier-to-use alternatives:

  • Consider using a std::vector anywhere you might otherwise use a dynamically allocated array. Since your class is called Container, I assume you are attempting to implement a resource-owning container, so perhaps you don't want to use this.

  • Consider using a boost::scoped_ptr or std::unique_ptr (if your compiler supports it) to manage ownership of the pointer. Both of these suppress generation of the implicitly declared copy constructor, forcing you to implement your own if you actually attempt to use them.

Even though you are implementing a Container, you should still take advantage of more primitive containers to do the heavy lifting for you (or, at least reimplement those primitive containers so that the heavy lifting is consolidated into a small set of utilities).

Finally, as a stylistic point, you don't need to use malloc for a container. The standard library provides std::allocator which can be used to allocate space for and construct objects.


Hi if you create array you must use delete []BasePointer;


To be more explicit than the other ones - when you new-ed an array type, you need to delete the pointer using delete[].

This doesn't only happen when you use new T[size], consider the following sample:

typedef int T[42];
int* x = new T;
delete[] x; // needs delete[] though you used new without []

On a general note, if your object "owns" an object it holds via a pointer, you should consider using a smart pointer (like boost::scoped_array) for that. That way, you don't need to worry about deconstruction, what happens when an exception gets thrown, implementing assignment op and copy constructor ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜