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);
}