0

I have several objects which share a data via a pointer. The pointer parameter was sent via in the constructor functions, as follows.

class A
{
public:
    Shared* pB = new Shared();
    User* object1 = new User(pB);
    User* object2 = new User(pB);
}

class User
{
public:
    User(Shared* pB) {m_sharedB = pB};
private:
    Shared* m_sharedB;
}
class Shared
{
public:
    struct Account 
    {
        int account_number;
    }
    void method(){...};
}

My question is related with the C++ destructor function. What happens to the member variable "m_sharedB", when object1 is deleted? Is there any problem of dangling pointer for other peers?

newID
  • 141
  • 1
  • 7
  • 1
    Tons of errors. Did you actually try anything yourself? Please fix the basic syntax errors. – Kerrek SB Jun 21 '11 at 21:48
  • Is it `Shared` pointer your class? Or is it library class? – George Gaál Jun 21 '11 at 21:51
  • Object pointed to by m_sharedB will not be deleted. Ever. I somehow think that this is not the answer you're looking for. Maybe the question you should ask is how to destroy that object right after it's not needed any longer? – Dialecticus Jun 21 '11 at 21:53
  • Shared is not library class, it is simply an ordinary class – newID Jun 21 '11 at 21:59

5 Answers5

6

If you have a class that contains a member that is a pointer,

class Foo
{
  Bar * mp_bar;
};

then upon destruction of a Foo object, nothing happens other than that the pointer, along with its containing object, goes out of scope. It's the same as what happens to p at the end of the following function:

void bar()
{
  int * p;
}

What you may have meant to ask about is "what happens to the object to which the pointer points". That's an entirely different question, and the answer is "nothing".

(So usually when you have a class that contains a pointer member you should think carefully about who owns any resources that may need to be cleaned up.)


Since you mention the word "destructor" in your question, let us spell out once and for all:

A pointer type object has no destructor. When a pointer goes out of scope, there is no automatic invocation of delete.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • @user481034: Well, when an object of type `Foo` lies on the stack and goes out of scope, then its member objects also go out of scope and are unwound. If a `Foo` object has been allocated on the heap, then its member objects get deallocated when `Foo` does. – Kerrek SB Jun 21 '11 at 22:21
  • Does this member variable pointer behave like the local or temporary variable on the stack, in which once the function return, they were just unwind from the stack memory. Also, Do you suggest the pointer owner or creator(i.e., the class A) should delete the pointer? so what if in the non-owner, i,e, the User class' dtor, m_shared was deleted? – newID Jun 21 '11 at 22:22
  • @user481034: This can't be answered in general, it really depends on your design. You have to think about where the pointer comes from and whether it points to a heap-allocated resource. In principle yes, someone will eventually have to free the memory. Usually one makes memory-managing classes which allocate in the constructor and free the memory in the destructor, but then you have to be *very* careful copying such an object around. See `shared_ptr` for how to do it right. And then use `shared_ptr` ;-) – Kerrek SB Jun 21 '11 at 22:24
  • Are there any resources or books explaining the C++ data model in terms of memory operations for ctors and dtors? – newID Jun 21 '11 at 22:33
  • @user481034: Constructors and destructors have nothing to do with "memory operations". They're simply functions that are called automatically upon respectively instantiation and going-out-of-scope (automatic variables) or deallocation (i.e. `delete` for heap variables). Not sure about books, I think Stroustrup's original explains the language extremely well and readably. [Update] OK, construction actually does a bit more by recursively constructing member objects, but nonetheless this is an orthogonal concept to memory management. – Kerrek SB Jun 21 '11 at 22:35
0

Destructing a raw pointer variable is a no-op (read more). The shared object will still be available.

Community
  • 1
  • 1
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
0

When object1 is deleted, the memory pointed to by m_sharedB will not be released as you haven't provided a destructor for class User. That leaves it up to the containing class (class A) to manage the lifetime of the Shared pointer as well, making sure that it is properly deleted once no more references to it exist (i.e., all User instances which share the same Shared pointer are dead).

Martin Törnwall
  • 9,299
  • 2
  • 28
  • 35
0

What happens to the member variable "m_sharedB", when object1 is deleted?

When object1 is deleted, nothing is made to pointer pB. But variable m_shared is destroyed. Also I want to advice you use smart pointers from boost library. Particularly shared_ptr

George Gaál
  • 1,216
  • 10
  • 21
0

It sounds like your question was answered, but I just wanted mention boost::shared_ptr. This may not meet your current needs, but it is useful situations like you describe above.

Sergio Martinez
  • 239
  • 2
  • 2