Thread: _BLOCK_TYPE_IS_VALID(pHead -> nBlockUse)

  1. #1
    Registered User
    Join Date
    Jul 2006
    Location
    US
    Posts
    14

    _BLOCK_TYPE_IS_VALID(pHead -> nBlockUse)

    Hello,
    I have been struggling with a memory corruption/crash that causes the message in the subject and humbly ask for your help.
    I narrowed down the code to just a few lines hopefully it will be easy to see what is that I'm doing wrong.
    Here is the main program
    Code:
    #include "example.h"
    #include <vector>
    int main()
    {
        std::vector<exampleClass> objs;
        exampleClass test;
        objs.push_back(test);
        return 0;
    }
    And here is the header that defines the classes and the implementation:
    Code:
    class Curve
    {
    private:
        double *m_x;
    public:
        Curve(int n, double *x)
        {
            m_x = new double[n];
        }
        ~Curve()
        {
            delete [] m_x;
        }
    };
    ///////////////////////////////////
    class exampleClassComp
    {
    private:
        double val;
        Curve cv;
    public:
        exampleClassComp() : cv(0,0)
        { }
    friend class exampleClass;
    };
    //////////////////////////////
    class exampleClass
    {
    private:
        int id;
        exampleClassComp obj;
    public:
        exampleClass(void); 
    };
    ///////////////////////////////
    exampleClass::exampleClass(void)
    {
        id = 0;
        obj.val = 0.0;
    }
    It seems that the crash occurs in the dst of the class Curve. Also, I noticed that it is called when the object "test" is pushed onto the vector.
    If anyone can offer some insight it would be much appreciated!
    Thanks!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The problem is that your class lacks a copy constructor.
    The same pointer is copied to two objects (one when creating it and one when pushing it into the vector), so the destructor tries to delete the same memory twice.
    Either you need to perform a deep copy (ie allocate new storage in new object and copy data over) or make the copy constructor private.
    Last edited by Elysia; 06-05-2008 at 07:24 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Location
    US
    Posts
    14
    Thanks for the reply.
    I am assuming that the class that needs to have copy constructor and overloaded operator = is the Curve class.
    I will give it a shot and report.
    Thanks again.
    Andrea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. small -> big -> bigger -> bigger than bigger -> ?
    By happyclown in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 03-11-2009, 12:12 PM
  2. Dev-C++ -> Tools - > Editor -> Syntax
    By Yuri2 in forum C++ Programming
    Replies: 19
    Last Post: 07-03-2006, 07:48 AM
  3. > > > Urgent Help < < <
    By CodeCypher in forum C Programming
    Replies: 2
    Last Post: 01-31-2006, 02:06 PM
  4. electricity > AC circuits > tesla coil
    By dbaryl in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 03-14-2002, 02:16 PM