By Alex


2018-03-13 15:08:08 8 Comments

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

1 comments

@Lightness Races in Orbit 2018-03-13 15:10:58

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).

@Alex 2018-03-13 15:13:30

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.

@Lightness Races in Orbit 2018-03-13 15: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.

Related Questions

Sponsored Content

52 Answered Questions

[SOLVED] Creating a memory leak with Java

50 Answered Questions

[SOLVED] Are memory leaks ever ok?

  • 2008-11-07 19:01:52
  • Imbue
  • 29332 View
  • 208 Score
  • 50 Answer
  • Tags:   c++ c memory-leaks

1 Answered Questions

[SOLVED] Memory leak of temp param arrays

  • 2018-02-26 23:10:28
  • code
  • 42 View
  • 0 Score
  • 1 Answer
  • Tags:   c++

6 Answered Questions

[SOLVED] C++: memory leaks

1 Answered Questions

mainwindow.obj:-1: error: LNK2019:

  • 2016-05-19 23:44:41
  • JOHNKIA
  • 75 View
  • -1 Score
  • 1 Answer
  • Tags:   c++ qt

2 Answered Questions

[SOLVED] Valgrind complaining possible memory leak in std string's new operator

1 Answered Questions

[SOLVED] QListWidget memory leak

1 Answered Questions

[SOLVED] Memory leak when create std::shared_ptr to assign member variable

  • 2015-07-29 03:08:52
  • je-woong.ku
  • 113 View
  • 0 Score
  • 1 Answer
  • Tags:   c++ memory-leaks

3 Answered Questions

1 Answered Questions

[SOLVED] C++ program memory leak on SIGINT

  • 2011-05-07 23:10:08
  • azat
  • 710 View
  • 2 Score
  • 1 Answer
  • Tags:   c++ c memory-leaks

Sponsored Content