-1

I am getting an invalid write of size 8 with valgrind for the following code. Any help in this regard please.

#include <string>
#include <iostream>

using namespace std;

class Base {
 public:
  Base()  {
    cout << "Base constructor ... ";
    m_strPath = "";
    m_total = 0;
    cout << "Completed Base constructor" << endl;
  }

  ~Base() {
    cout << "Base destructor ... ";
    m_strPath = "";
    m_total = 0;
    cout << "Completed Base destructor" << endl;
  }

 protected:
  string m_strPath;
  unsigned int m_total;
};

class Derived : public Base {
 public:
  Derived() {
    m_offset = 0;
    cout << "  Size of offset (off_t) = "
         << sizeof(m_offset) << endl;
  }

  ~Derived() {
    cout << "Derived destructor" << endl;
  }

 private:
  off_t m_offset;
};

int main() {
  cout << "Starting execution" << endl;
  Derived *listFile = new Derived();
  cout << "  size of dynamic object is = "
       << sizeof(*listFile) << endl;
  delete listFile;

  cout << "-----" << endl;

  Derived listFile1;
  cout << "  size of object is = " << sizeof(listFile1) << endl;
  cout << "Ended execution" << endl;
  return 0;
}

The valgrind error points me to offset member variable of derived class. "Invalid write of size 8" happens for both object created on stack and on heap.

Thanks

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
Sravan
  • 131
  • 1
  • 2
  • 10
  • 2
    I don't see this reports in given example (which is not compilable without `#include `, btw). What is compilation command? Are you sure you tested it on exactly same code, and code/binary are in sync? – keltar Nov 29 '13 at 10:46
  • See http://stackoverflow.com/questions/461203/when-to-use-virtual-destructors – Adam Liss Nov 30 '13 at 13:21
  • @AdamLiss: That's not the issue. Since the new expression and delete expression use the same class type, the code is valid. (A virtual destructor is probably a good idea, yes.) – aschepler Nov 30 '13 at 14:22

1 Answers1

1

This link specifies when it's not necessary to use a virtual destructor: When should you not use virtual destructors?

There is no need to use a virtual destructor when any of the below is true:

    No intention to derive classes from it
    No instantiation on the heap
    No intention to store in a pointer of a superclass

No specific reason to avoid it unless you are really so pressed for memory.

Because of the last two, I don't believe you're going to have any memory leaks (and I checked on valgrind.) However the consensus seems to be that you should always make your base destructor virtual if you have any classes that derive from it. However you are not using a base pointer and you have no other virtual functions in your base class so I'm not sure if it's a problem.

As already stated, your original code (before the edit) does not even compile, which means that you didn't post your real code. As a result, I'm unable to reproduce the same invalid write error you were getting.

However, when running the new edit's code through valgrind, I get this output:

Starting execution
Base constructor ... Completed Base constructor
  Size of offset (off_t) = 8
  size of dynamic object is = 24
Derived destructor
Base destructor ... Completed Base destructor
-----
Base constructor ... Completed Base constructor
  Size of offset (off_t) = 8
  size of object is = 24
Ended execution
Derived destructor
Base destructor ... Completed Base destructor

When virtual is added to the base destructor, the number becomes 32 instead of 24 (maybe that's related to your issue.)

Community
  • 1
  • 1