_BLOCK_TYPE_IS_VALID(phead->nBlockUse) — assertion faliure

Please help me.
When the deconstructor is called and the line if(m_director != NULL) delete[] m_director; is executed I get error that says :"_BLOCK_TYPE_IS_VALID(phead->nBlockUse) — assertion faliure".
Why it is occurred and how to fix?
My code is below.
Thank you.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <string.h>
#include <iostream>
using namespace std;

const int MAX_LEN_OF_NAME = 100;

class Person
{
    char* m_fname;
    char* m_lname;
    int m_age;
public:
    Person()
    {
        m_fname = m_lname = NULL;
    }
    ~Person()
    {
        if(m_fname != NULL)
            delete[] m_fname;
        if(m_lname != NULL) delete[] m_lname;
    }
    Person(const char* strFirstName, const char* strLastname, int iAge) : m_age(iAge), m_fname(NULL), m_lname(NULL)
    {
        SetName(strFirstName, strLastname);
    }
    //Set name of the person
    void SetName(const char* strFirstName, const char* strLastname)
    {
        //Don't reallocate memory if the length of the new name is less than the current one
        if(m_fname == NULL || strlen(m_fname) < strlen(strFirstName))        
            m_fname = new char[strlen(strFirstName) + 1];
        strcpy(m_fname, strFirstName);        
        if(m_lname == NULL || strlen(m_lname) < strlen(strLastname))        
            m_lname = new char[strlen(strLastname) + 1];
        strcpy(m_lname, strLastname);
    }
    void SetAge(int iAge)
    {
        m_age = iAge;
    }
    const char* GetFirstName() const
    {
        return m_fname;
    }
    const char* GetLastName() const
    {
        return m_lname;
    }
    int GetAge() const
    {
        return m_age;
    }
};
class Movie
{
    char* m_name;
public:    
    Movie() : m_name(NULL)
    {        
    }
    ~Movie()
    {
        if(m_name != NULL)
            delete m_name;
    }
    void SetName(const char* strName)
    {
        if(m_name == NULL || strlen(m_name) < strlen(strName))        
            m_name = new char[strlen(strName) + 1];
        strcpy(m_name, strName);
    }
    const char* GetName() const
    {
        return m_name;
    }
};
class Director : public Person
{
    Movie* m_movies;
    int m_numOfMovies;
public:    
    Director(const char* strFirstName, const char* strLastname, int iAge) : m_numOfMovies(0)
    {
        //Set name of the director
        SetName(strFirstName, strLastname);
        SetAge(iAge);    
        m_movies = NULL;
    }
    ~Director()
    {
        if(m_movies != NULL)
            delete[] m_movies;
    }
    //Allocate memory for specific number of movies
    void SetNumOfMovies(int iNum)
    {
        if(m_numOfMovies == iNum)  
            return;

        m_numOfMovies = iNum;

        if(m_numOfMovies > 0)
        {
            delete[] m_movies;
            m_movies = new Movie[m_numOfMovies];
        }
    }    
    void SetMovies()
    {
        int iNumOfMovies;        

        cout << "How many movies he produced?" << endl;
        cin >> iNumOfMovies;

        SetNumOfMovies(iNumOfMovies);

        char movieName[MAX_LEN_OF_NAME];
        for(int i = 0; i < m_numOfMovies; i++)
        {
            cout << "What the name of the " << i + 1 << " movie?" << endl;
            cin >> movieName;
            m_movies[i].SetName(movieName);
        }
    }
    const Movie* GetMovies() const
    {
        return m_movies;
    }
    int GetNumOfMovies() const
    {
        return m_numOfMovies;
    }
};
class Actor : public Person
{
    Movie* m_movies;
    int m_numOfMovies;
public:    
    Actor(const char* strFirstName, const char* strLastname, int iAge) : m_numOfMovies(0)
    {
        //Set name of the director
        SetName(strFirstName, strLastname);
        SetAge(iAge);        
        m_movies = NULL;
    }
    ~Actor()
    {
        if(m_movies != NULL) delete[] m_movies;
    }
    //Allocate memory for specific number of movies
    void SetNumOfMovies(int iNum)
    {
        if(m_numOfMovies == iNum)  
            return;

        m_numOfMovies = iNum;

        if(m_numOfMovies > 0)
        {
            delete[] m_movies;
            m_movies = new Movie[m_numOfMovies];
        }
    }    
    void SetMovies()
    {
        int iNumOfMovies;        

        cout << "In how many movies he played?" << endl;
        cin >> iNumOfMovies;

        SetNumOfMovies(iNumOfMovies);

        char movieName[MAX_LEN_OF_NAME];
        for(int i = 0; i < m_numOfMovies; i++)
        {
            cout << "What the name of the " << i + 1 << " movie" << endl;
            cin >> movieName;
            m_movies[i].SetName(movieName);
        }
    }
    const Movie* GetMovies() const
    {
        return m_movies;
    }
    int GetNumOfMovies() const
    {
        return m_numOfMovies;
    }
};
//Class for person who is Director and Actor
class DirActor : public Person
{
    Director* m_director;
    Actor* m_actor;
public:    
    DirActor(const char* strFirstName, const char* strLastname, int iAge)
    {        
        SetName(strFirstName, strLastname);
        SetAge(iAge);
        m_director = new Director(strFirstName, strLastname,iAge);
        m_actor = new Actor(strFirstName, strLastname,iAge);
    }
    ~DirActor()
    {
        if(m_director != NULL) delete[] m_director;
        if(m_actor != NULL) delete[] m_actor;
    }
    Director* GetDirector() const
    {
        return m_director;
    }
    Actor* GetActor() const
    {
        return m_actor;
    }
};
void main()
{    
    DirActor da("Clint", "Eastwood", 79);
    da.GetDirector()->SetMovies();
    da.GetActor()->SetMovies();
    cout << "=============================================================================" << endl;
    cout << "Printing information about " << da.GetFirstName() << " " << da.GetLastName() << endl;
    cout << "Age:" << da.GetAge() << endl;
    cout << "The movies he produced are:" << endl;
    for(int i = 0; i < da.GetDirector()->GetNumOfMovies(); i++)
    {    
        cout << i + 1 << " movie:" << da.GetDirector()->GetMovies()[i].GetName() << endl;
    }
    cout << endl;
    cout << "The movies he played in are:" << endl;
    for(int i = 0; i < da.GetActor()->GetNumOfMovies(); i++)
    {    
        cout << i + 1 << " movie:" << da.GetActor()->GetMovies()[i].GetName() << endl;
    }
}
Last edited on
You're using delete[] on a non-array. This causes a segmentation fault. Only use delete[] on objects that were created with the new T[size] syntax, not the new T syntax.
What this means -
This causes a segmentation fault
?
And what is the difference? new T also allocate memory
http://en.wikipedia.org/wiki/Segmentation_fault

Both allocate memory, but new T allocates one object, while new T[] allocates an array of objects.
Using delete[] on a single object causes a segmentation fault because the library is expecting a pointer to an array.

In short:
If you allocate with new T and deallocate with delete:
No problem.
If you allocate with new T[] and deallocate with delete[]:
No problem.
If you allocate with new T[] and deallocate with delete:
Generates a memory leak.
If you allocate with new T and deallocate with delete[]:
Causes a segmentation fault.
Topic archived. No new replies allowed.