-3

I am using a C++ compiler with the -std=c++0x flag.

I am writing a simulation for a virtual memory system for class and have ran into a problem:

One of the classes, VirtualMemory, initializes several objects of a different class called OurPointer by giving them an integer and a pointer to itself.

The program runs several pointers that are all linked to the same virtual memory. the constructor of OurPointer looks like so:

OurPointer::OurPointer(int adr , VirtualMemory* vrtlMem)
{
    _adr = adr;
    _vrtlMem = vrtlMem;
}

This is because OurPointer is used to work with VirtualMemory. The problem is, every time an OurPointer object is destroyed, it calls the destructor of _vrtlMem, which points to the same VirtualMemory object that is still used by the program.

Is there a way to exclude that field from the destructor? I tried to turn it into a static field, or change the pointer but it didn't stop the destructor.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
Matan
  • 1
  • 1
  • That's weird. Destruction of `_vrtlMem` should not deallocate the memory pointed by it, it will only destroy the pointer itself. Are you sure that you are not deallocating the memory yourself? – Rakete1111 Feb 03 '17 at 19:00
  • If you store only a pointer to the virtual memory, no destructor gets called in the first place – MikeMB Feb 03 '17 at 19:00
  • My guess is that vrtlMem's destructor is called because the object is going out of scope where it was created, not because of OurPointer being destroyed. – Mathieu Pagé Feb 03 '17 at 19:03
  • What is `_vrtlMem`??? What type does it have? If it is also a pointer of `VirtualMemory*` type, just like `vrtlMem` parameter, then your problem description is misleading. Raw pointers in C++ do not have destructors. – AnT stands with Russia Feb 03 '17 at 19:03
  • 2
    Off topic: [On the off chance you don't know the rules](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier), be careful with underscore prefixes. – user4581301 Feb 03 '17 at 19:08
  • 6
    Hard to say much of value without more information. An [mcve] would be good, but this may be answerable with the definition of `OurPointer` and `~OurPointer::OurPointer` – user4581301 Feb 03 '17 at 19:16
  • We can answer the question in your title, but I'm pretty sure that won't solve your problem. If you want help with that, you should provide an [mcve](/help/mcve) as suggested by user4581301. – MikeMB Feb 03 '17 at 19:31

1 Answers1

1

The problem is, every time an OurPointer object is destroyed, it calls the destructor of _vrtlMem

That would be true only if:

  1. you have coded a destructor to do that, eg:

    OurPointer::~OurPointer()
    {
        delete _vrtlMem;
    }
    
  2. _vrtlMem is a smart pointer like std::auto_ptr or std::unique_ptr, which destroy the pointed-to object when they are themselves destructed.

which points to the same VirtualMemory object that is still used by the program.

That is exactly the kind of situation that C++11's std::shared_ptr was designed for. A shared object will not be destructed until all references to it have been cleared.

Since you are not using C++11, you could use boost::shared_ptr instead, or you could just implement reference counting manually.

Is there a way to exclude that field from the destructor?

If _vrtlMem is a smart pointer, no. But shared_ptr handles reference counting for you.

Otherwise, if _vrtlMem is just a raw pointer, simply don't destruct the pointed-to object when you don't want it destructed.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770