memory crash _BLOCK_TYPE_IS_VALID(pHead -> nBlockUse)

Hello, and thanks for reading
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

1
2
3
4
5
6
7
8
9
#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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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. Should I create a copy constructor for the Curve class?
If anyone can offer some insight it would be much appreciated!
Thanks!
Yes, you need both a copy constructor and an assignment operator.

Standard containers are pretty notorious for copying things around a lot (but it says in their specification that they can do that).

Whenever you have pointers to data in your class you should always have a copy constructor and an assignment operator.

You can do a deep copy, which creates a new array of double and copies the contents over:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    Curve( const Curve& curve )
    {
    *this = curve;
    }
    Curve& operator = ( const Curve& curve )
    {
    if (this != &curve)
      {
      m_n = curve.m_n;  // I assume you keep track of the length of m_x
      m_x = new double[ curve.m_n ];
      copy( m_x, m_x +curve.m_n );  // #include <algorithm> for stl::copy()  [edit] I goofed on this line! See below [/edit]
      }
    return *this;
    }


Or you can maintain a reference count like I do in the Image2D class example in this thread:
http://www.cplusplus.com/forum/general/2236/

Hope this helps.
Last edited on
Thank you for your reply. I do understand the need for a copy constructor and overloaded = operator.
Sorry about having to ask again, but I'm having compilation issues with the copy function (algorithm) in VS2005.
Is #include <algorithm>
all I need?
If I include that file and don't define the std namespace then I get this:
error C3861: 'copy': identifier not found
If I say "using namespace std;" then my compiler yells that
1
2
3
4
5
6
7
c:\documents and settings\apertosa\my documents\visual studio 2005\projects\example\example\example.cpp(20) : error C2780: '`global namespace'::std::_Enable_if<!std::_Is_checked_iterator<_OutIt>::_Result,_OutIt>::_Result std::copy(_InIt,_InIt,_OutIt)' : expects 3 arguments - 2 provided
        c:\program files\microsoft visual studio 8\vc\include\xutility(2370) : see declaration of 'std::copy'
c:\documents and settings\apertosa\my documents\visual studio 2005\projects\example\example\example.cpp(20) : error C2780: '_OutElem *std::copy(_InIt,_InIt,_OutElem (&)[_Size])' : expects 3 arguments - 2 provided
        c:\program files\microsoft visual studio 8\vc\include\xutility(2362) : see declaration of 'std::copy'
c:\documents and settings\apertosa\my documents\visual studio 2005\projects\example\example\example.cpp(20) : error C2780: '`global namespace'::std::_Enable_if<std::_Is_checked_iterator<_OutIt>::_Result,_OutIt>::_Result std::copy(_InIt,_InIt,_OutIt)' : expects 3 arguments - 2 provided
        c:\program files\microsoft visual studio 8\vc\include\xutility(2354) : see declaration of 'std::copy'
Yoinks! I don't know what I was thinking when I wrote that.

Fix it with:

std::copy( curve.m_x, curve.m_x +curve.m_n, m_x );

The std:: is so you don't have to say 'using namespace std', and the copy algorithm copies from curve.m_x to m_x.

Sorry about that! (That was really dumb of me!) :-S

[edit] BTW, you can always go to the homepage of this site and in the search bar type things like

algorithm::copy

and usually the very first link found is the documentation for it. (The only thing cplusplus.com seems to lack is documentation on the <functional> library...)
Last edited on
Topic archived. No new replies allowed.