I have a class which has a struct with pointers as one of its member.
struct group {
void *v1,
void *v2;
};
class A {
A (group& handle)
: m_handle(handle)
private :
group m_handle;
};
There are no pointer members in class A. I don't see any issue (such as memory leaks) when no destructor is defined in A. I have learnt that when object A goes out of scope, destructor of A is called and if there are member classes present in A, then their destructors are called and so on. So, what happens to a member struct as m_handle above - do they have anything similar to destructor and how are the two void pointers in struct group deleted when object A goes out of scope?
Answer
Yes, v1 and v2 could be leaked, if they aren't deallocated in some other part of your program. So, in the destructor of A you can delete v1 and v2 (if it's appropriate to), or you can just add a destructor to group (in c++, a struct is exactly like a class except for default visibility - stuff is public by default rather than private) and delete them there. Of course, this depends on appropriateness (maybe some other thing allocated and owns v1 and v2).
No comments:
Post a Comment