0

I have a problem with memory leaks when i allocate almost identical classes but one has a member variable as string instead of integer.

The class with the string gives memory leaks but not the one with integer. I have deleted everything i can delete but i still get memory leaks please help.

So the soundbook class is giving me memory leaks i dont know why because i havnt allocated anything but when i remove the string member i don't get the memory leak anymore why does this happen?

//main

#include <iostream>

#include "PappersBok.h"
#include "SoundBook.h"

int main()
{
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    Books *bk[5];

    bk[0] = new SoundBook();
    bk[1] = new PappersBok();
    bk[2] = new PappersBok();
    bk[3] = new PappersBok();
    bk[4] = new PappersBok();

    for (int i = 0; i < 5; i++)
    {
        delete bk[i];
    }


    system("pause");

    return 0;
}

//soundbook class .h and .cpp

#ifndef SOUNDBOOK_H
#define SOUNDBOOK_H


#include "books.h"

class SoundBook : public Books
{
private:
    std::string medium;
public:
    SoundBook(std::string title = "?", std::string author = "?", std::string medium = "?");
    ~SoundBook();

    std::string toString() const;
    void setMedium(std::string medium);

};


#endif

//.cpp
#include "SoundBook.h"



SoundBook::SoundBook(std::string title, std::string author, std::string medium)
    :Books(title, author)
{
    this->medium = medium;
}

SoundBook::~SoundBook()
{
}  
std::string SoundBook::toString() const
{
    return ", Medium: " + this->medium;
}

//Pappersbok class .cpp and .h

#ifndef PAPPERSBOK_H
#define PAPPERSBOK_H


#include "books.h"

class PappersBok : public Books
{
private:
    int nrOfPages;
public:
    PappersBok(std::string title = "?", std::string author = "?", int nrOfPages = 0);
    ~PappersBok();

    std::string toString() const;
};

#endif

//.cpp

#include "PappersBok.h"



PappersBok::PappersBok(std::string title, std::string author, int nrOfPages)
    :Books(title, author)
{
    this->nrOfPages = nrOfPages;
}


PappersBok::~PappersBok()
{
}

std::string PappersBok::toString() const
{
    return ", Number of pages: " + std::to_string(this->nrOfPages);
}
Alex
  • 93
  • 1
  • 4

1 Answers1

6

Any introductory text on polymorphism in C++ will tell you to use a virtual destructor.

Otherwise, delete can't do its job properly when you call it on a base pointer.

bk[0] is a Books* pointing to a SoundBook, but delete doesn't know that it's pointing to a SoundBook, and so cannot fully destroy the members of its derived part. Hence the memory leak (and, more broadly, the program has undefined behaviour).

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Oh that helped me, i forgot that i need to use a virtual destructor but why was it only the class with the string that got memory leaks and not the one with an integer. – Alex Mar 13 '18 at 15:13
  • @Alex: Because the one with an integer doesn't really have anything to do. The one with a string needs to, in turn, invoke the string destructor (in order to free dynamically allocated memory) and that's not happening at the moment. – Lightness Races in Orbit Mar 13 '18 at 15:15