Why there are no memleaks in the following C++ while assign? - c++

the following code demotes my belief that I know C++ more or less. Why valgrind does not show any memleak here? Why I expect memleaks:
B is larger than A: it contains an additional member; so on assign there should be class' fields slicing.
~A() does not have a virtual dtor. So when we call delete a only ~A() should be called and memory allocated in B would lost.
But I receive that the calling order of dtors is: ~A(), ~B(), ~A(). Why?!
struct A {
~A()
{
std::cerr << "~A" << std::endl;
}
};
struct B : A {
int* data;
B() : data(new int[20]) {}
~B()
{
std::cerr << "~B" << std::endl;
delete [] data;
}
};
main():
A* a = new A;
B* b = new B;
*a = *b;
delete a;
delete b;
UPD:
Shame on me! I muddled deletion of an object by the pointer of base class when virtual dtor should called. Here just contents of class copying. Thanks everyone!

delete a;
a is A*, so A::~A is called
delete b;
b is B* so B::~B is called.
Why would there be a problem?
Class fields' slicing? Yeah, so? You're just copying the A fields from *b to *a, nothing more than that. No memory is lost here.

In *a = *b you are copying (and yes, slicing) *b to *a. However, that leaves *b unchanged; it doesn't get sliced. When you delete b, you are asking for ~B() to be called on *b.

The *a = *b; assignment only copy the common A part of the data (and A has no fields).

You made a delete for each new , so there is no memleak.
*a =*b
does not allocate anything, the copy constructor just copies the part of b which can go to a, but it does not call new..

By invoking *a = *b;, you're implicitly calling the assignment operator function created by the compiler that looks like this:
A &operator=( const A &other ){
}
which in your case doesn't do anything since A has no member variable.

Your class A is empty, so it is entirely irrelevant for the purpose of this discussion. The only code that is involved in dynamic allocation is:
B* b = new B;
delete b;
Since you delete an object created with new, there is no leak. Furthermore, the class B contains sufficient code to clean up its own internal allocation in this straight-forward use-case.
(Note that class B is still horribly broken, since it will not survive copying or assignment. The classic Rule of Three (or Five).)

Because what you do here is object slicing. I think you intended to have the base pointer point to the derived object and since the base destructor is not virtual, derived class destructor not called during cleanup. But that is not the case here.
*a = *b
The compiler will 'slice' those derived part of the object and copy only the base part of the object. This is not usually one want to do because this will result in an incomplete object with only base class properties and specialized derived class properties missing.

Related

Read access violation error when declaring a class with a dereferenced pointer

I am trying to write a function that creates a class object from two pointers to other class objects, but I keep getting a read access violation error.
My program has three classes:
class A {
public:
A(const A& a2); // Special copy constructor
private:
int a = 0;
}
class B {
public:
B(const B& b2); // Special copy constructor
private:
int b = 0;
}
class C {
public:
A* aPtr;
B* bPtr;
}
What I am trying to do, is to have a function that takes a pointer to an A object and a B object, and then dereferences these into a C-instance. It is important that the objects themselves get copied since it is for multithreading purposes. The function is for example:
void foo(A* ptrA, B* ptrB){
C* ptrC = new C();
*ptrC->aPtr = *A;
*ptrC->bPtr = *B;
}
But this gives me a read access violation error.
What am I doing wrong? I know that objects themselves get copied without error, so I think it has something to do with how i declare ptrC or dereference ptrC->A and ptrC->B. Am I right about this?
What is a possible solution?
Thanks in advance.
A(const A& a2); // Special copy constructor
What you call a "special copy constructor" is commonly known as a converting constructor.
But this gives me a read access violation error. What am I doing
wrong?
C* ptrC = new C();
This creates a dynamic object and value initialises it. The pointers that are members are value initialised to null.
*ptrC->aPtr = *A;
*ptrC->bPtr = *B;
Here, you indirect through those null pointers and write to the memory. This results in undefined behaviour.
What is a possible solution?
Don't indirect through null pointers. Perhaps storing A and B objects instead of pointers within the C object would fit better to your design?
yes pretty much try:
void foo(A* ptrA, B* ptrB){
C* ptrC = new C();
ptrC->aPtr = ptrA;
ptrC->bPtr = ptrB;
}
instead
(and try to avoid pointers next time they never help you out with anything)

Deallocation of passed in pointer in descructor

class A
{
public:
A(){ };
~A(){ };
//stuff
};
class B
{
public:
B(A* a){ pA = a;}
~B(){ };
A* pA;
};
int main()
{
A instOfA = A();
B instOfB = B(&instOfA);
return 0;
}
I am unsure as to whether ~B() should delete its pointer data member *pA. I.e.
~B()
{
if(pA!=nullptr) delete pA;
}
My guess is that a deletion is needed because B is allocating it in A* pA.
This is all about the ownership and thereby the question is a design question. You are not dynamically allocating A so I would not let B touch it though. It will be deleted in the end of the main.
If you would allocate an instance of A dynamically (A* instOfA = new A();
), then you could choose if you want to pass the ownership of that instance to B and have B deleting it.
My guess is that a deletion is needed because B is allocating it in A*
pA.
Your guess is not right. B is not allocating A. All it does is to take a pointer of the instance that was allocated in the main.
You're guess is incorrect, since
B instOfB = B(&instOfA);
passes the address of an A of automatic storage duration to Bs constructor. Operator delete can only be safely used to destroy an object that was created with the corresponding operator new. Using operator delete to destroy an object that was NOT created with the corresponding operator new gives undefined behaviour.
instOfA is guaranteed to cease to exist when main() returns, so no action is needed to destroy it. And any action which results in it being destroyed twice (so its destructor is called twice on the same object) also gives undefined behaviour.
Generally speaking, it would often be better to design your class B so it manages the A completely (e.g. manage its lifetime) to ensure consistency. By accepting a pointer, it is not possible to prevent (other than by strident documentation, and relying on programmers heeding the documentation) an object being passed to the constructor that the destructor does not handle correctly.
If you are going to do delete pA, it is not necessary to test that pA is non-NULL. The standard specifies that the delete operator has no effect on a NULL pointer.

Default destructor when using pointer to base class

class A
{
int a;
};
class B : public A
{
int b;
};
int main(void)
{
A * p = new B;
delete p; // (1)
return 0;
}
In the above code both classes have default compiler-generated destructors. Both classes also have only Plain Old Data as members so I don't need manually written d'tors which would free any resources. So my question is - after the call in (1) will the default destructor free the entirety of B's instance or will there be any memory leaks? I know that I could use a virtual destructor here but I'm not sure how default d'tors behave.
What you are trying to do invokes undefined behavior, so declaring A destructor as virtual can be considered mandatory.
delete p
Will try to delete p as an instance of A but since the destructor is not declared virtual the correct runtime implementation is not called.
Mind that this doesn't happen when you don't have a pointer but just a concrete object, eg
A a = B();
Because object slicing occurs before, so when a exits the scope it's just an A

Does deleting the main class's object pointer delete nested classes pointers as well?

I have a class inside a class, nested class
class A {
public:
int a;
int b;
class B {
int c;
int d;
}
}
In the process, I allocate the class B's object pointer in heap.
B *bobj = new B();
A *a;
auto_ptr<A> A1(new A());
a = A1.release();
Does deleting the a, delete bobj as well??
delete a;
or should we explicitly delete the sub class pointer as well?
This is in a code not written by me, I had put the logs at all alloc and deallocs and I see that B *bobj = new B(), for this there is no delete and for A's object ptr there is a delete. And yet there is no memory leak.That is why I got a doubt what happens in this scenario.
I think you're slightly confused here. If you have an object of type A, it doesn't have a B object inside of it. It just has two ints, a and b. Your class B is just declared as a nested class inside A - similar to declaring it in a namespace. It must be referred to as A::B from outside the class (and must be public to do so).
In the sample code you gave, B *bobj = new B(); A *a;, you're not even creating an A object. I presume you meant this (assuming you make B public):
A::B *bobj = new A::B();
A *a = new A();
Both a and bobj are completely separate objects. They have nothing to do with each other. You have to delete both of them.
If instead you had done this:
class B {
int c;
int d;
};
class A {
int a;
B b;
};
Now an object of class type A has a member called b which is of type B. That b member is part of any object of type A. So if you do A* a = new A();, you get an A object with a B object inside it. You must only do delete a;.
The golden rule: only delete what you have newed.
The way you have written the code, class B is a nested type within class A, but no instance of class B is contained in class A, meaning that the instantiation--and therefore destruction--must be managed separately. So yes, you need to delete both if you new both.
It depends on what your destructor doing. If A contains pointer of type B and in destructor of A this pointer is released - yes. If A doesn't contains pointer of type B and/or in A's destructor pointer to B is not released - you should release it manually.
In short, C++ doesn't automatically delete object pointers (without specialize auto-pointers etc.). So, explicitly delete them in your program.
delete a; call destructor of A class. You can write code to delete B's object inside destructor.

Overriding function in C++ doesn't work

#include <cstdio>
using namespace std;
class A {
public:
virtual void func() { printf("A::func()"); }
};
class B : public A {
public:
virtual void func() { printf("B::func()"); }
};
int main() {
A a = *(A *)new B();
a.func();
}
The question is simple: why a->func() calls function in class A even though a contains object of class B?
A a = *(A *)new B();
a.func();
Here's what happens in this code, step by step:
new B(): a new object of type B is allocated on the free store, resulting in its address
(A*): the address of the object is cast to A*, so we have a pointer of type A* actually pointing to an object of type B, which is valid. All OK.
A a: here the problems start. A new local object of type A is created on the stack and constructed using the copy constructor A::A(const A&), with the first paremeter being the object created before.
The pointer to the original object of type B is lost after this statement, resulting in a memory leak, since it was allocated on the free store with new.
a.func() - the method is called on the (local) object of class A.
If you change the code to:
A& a = *( A*) new B();
a.func();
then only one object will be constructed, its pointer will be converted to pointer of type A*, then dereferenced and a new reference will be initialized with this address. The call of the virtual function will then be dynamically resolved to B::func().
But remember, that you'd still need to free the object since it was allocated with new:
delete &a;
Which, by the way, will only be correct if A has a virtual destructor, which is required that B::~B() (which luckily is empty here, but it doesn't need to in the general case) will also be called. If A doesn't have a virtual destructor, then you'd need to free it by:
delete (B*)&a;
If you would want to use a pointer, then that's the same as with the reference. Code:
A* a = new B(); // actually you don't need an explicit cast here.
a->func();
delete (B*)a; // or just delete a; if A has a virtual destructor.
Now that you've modified your code snippet, the problem is clear. Polymorphism (i.e. virtual functions) are only invoked via pointers and references. You have neither of these. A a = XXX does not contain an object of type B, it contains an object of type A. You've "sliced away" the B-ness of the object by doing that pointer cast and dereference.
If you do A *a = new B();, then you will get the expected behaviour.
The problem you encounter is classic object slicing :
A a = *(A *)new B();
Make a either a reference or pointer to A, and virtual dispatch will work as you expect. See this other question for more explanations.
You commented on another answer that "Compiler should at least give warning or what". This is why is it considered a good practice to make base classes either abstract of non copyable : your initial code wouldn't have compiled in the first place.
This might do that trick.
A &a = *(A *)new B();
a.func();
Or
A *a = new B();
a->func();
Virtual dispatch works only with pointer or reference types:
#include <cstdio>
using namespace std;
class A {
public:
virtual void func() { printf("A::func()"); }
};
class B : public A {
public:
virtual void func() { printf("B::func()"); }
};
int main() {
A* a = new B();
a->func();
}
The problem is the deference and casting of B to A with the A a = *(A *)new B();
You can fix it with just removing the *(A *) changing it to (A *a = new B(); ) but I would take it a step further since your variable name is not good for instantiation of B.
It should be
B *b = new B();
b->func();
Because you performed slicing when you copied the dynamically allocated object into object a of type A (which also gave you a memory leak).
a should be a reference (A&) instead, or just keep the pointer.