I have the following code:
main.cpp
MovieLibrary* library = new MovieLibrary(maxMovies);
delete[] library; // This throws the error.
MovieLibrary.cpp
MovieLibrary::MovieLibrary(int maxMovies) {
this->maxMovies = maxMovies;
this->numMovies = 0;
this->movies = new Movie*[maxMovies];
}
MovieLibrary::~MovieLibrary() {
for (int i=0; i<this->numMovies; i++) {
delete this->movies[i];
}
delete[] this->movies;
}
Movie.cpp
Movie::Movie(Text* title, Text* publisher, int year) {
this->title = title;
this->publisher = publisher;
this->year = year;
}
This is not all the code, but it's all the code I believe should be required to find the cause of the error. I'm pretty novice to C++ and dynamic memory allocation. Running the program gives me a "pointer being freed was not allocated" and I've researched but to no avail. Most posts just say it's trying to clear something not initialized with malloc or new, but I mean MovieLibrary* library = new MovieLibrary(maxMovies);
is initialized with new, so I'm lost.
Thank you in advance.
As per @PeteBecker and @RemyLebeau:
Use delete library;
, rather than delete[] library;