C++ – pointer being freed was not allocated error

Welcome to the exciting world of C++!

Short answer: you’re passing Store as a value. All your menu functions should take a Store& or Store* instead.

When you’re passing Store as a value then an implicit copy is done (so the mainStore variable is never actually modified). When you return from the function the Store::~Store is called to clean up the copied data. This frees mainStore.bookList without changing the actual pointer value.

Further menu manipulation will corrupt memory and do many double frees.

HINT: If you’re on linux you can run your program under valgrind and it will point out most simple memory errors.

Leave a Comment