Mir

serious bug that might cause memory leaks

Bug #1183507 reported by Eleni Maria Stea
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Mir
Fix Released
Medium
Eleni Maria Stea

Bug Description

I found a serious bug in several mir classes which was that classes with virtual functions did not have a virtual destructor as they should.

Whenever such a class is used polymorphically with an instance of a subclass being used through a pointer of "base class" type:
if an attempt is made to delete it through that pointer, only the destructor of the base class will be called resulting in a memory leak since the decision of which destructor to call is taken at compile time for non-virtual functions.

Related branches

summary: - serious bug that might be causing memory leaks
+ serious bug that might cause memory leaks
Changed in mir:
assignee: nobody → Eleni Maria Stea (hikiko)
status: New → In Progress
Revision history for this message
Alexandros Frantzis (afrantzis) wrote :

We only use such classes through shared_ptrs, which, even when upcasted, "remember" the derived class type and call the proper destructor.

Of course, that's not to say that we shouldn't fix the missing virtual destructors (we should!), just that they are not a super critical issue in our case.

Revision history for this message
Alan Griffiths (alan-griffiths) wrote :

This is more an "accident waiting to happen" than a serious problem. I doubt that any of these classes are deleted through a pointer to base. (They are typically created using make_shared - and the resulting pointer deletes the correct type.)

Changed in mir:
importance: Critical → Low
Revision history for this message
Eleni Maria Stea (hikiko) wrote :

hmm... I added the virtual both in the base classes and the subclasses for clarity reasons (you don't have to follow the whole hierarchy to see what is virtual) but ok, I'll remove them.

About the shared pointer now, I think if the shared pointer points to the base class we will have problem (because the destructor that will be called is chosen at compile time for the virtual functions) but if it points to the subclass we are fine.

lol @tab :-D

Revision history for this message
Alan Griffiths (alan-griffiths) wrote :

"About the shared pointer now, I think if the shared pointer points to the base class we will have problem (because the destructor that will be called is chosen at compile time for the virtual functions) but if it points to the subclass we are fine."

Either you're saying something irrelevant, or you've misunderstood something. To clarify what do you expect the output of this to be?

#include <memory>
#include <iostream>

struct example { ~example() { std::cout << "No problem\n"; } };

int main()
{
    std::shared_ptr<void> pointer_to_void = std::make_shared<example>();
}

Revision history for this message
Eleni Maria Stea (hikiko) wrote :

Ok, then! I didn't realize that shared pointers keep knowledge of the assigned type, I thought it was all based on the template parameter and thus work like regular pointers! :)

Revision history for this message
Eleni Maria Stea (hikiko) wrote :

Well, from what I see, there are many classes and structs that have the issue and don't make use of shared pointers (eg. inside the 3rd_party directory). I have already updated my branch to fix them.

A random example of a potential memory leak in 3rd_party tests is this:
class MockANativeWindow that inherits from ANativeWindow and ANativeWindowInterface but it doesn't have a virtual destructor and it doesn't use shared pointers.

I guess that the memory leaks in tests are not quite important but I found such problems elsewhere as well, all fixed in the proposed branch. Maybe we should use the virtual destructors from now on.

Revision history for this message
Eleni Maria Stea (hikiko) wrote :

(I mean when we don't make use of shared pointers)

Changed in mir:
importance: Low → Medium
Changed in mir:
status: In Progress → Fix Committed
status: Fix Committed → In Progress
Revision history for this message
Daniel van Vugt (vanvugt) wrote :

Re-linked the old branches. It's good to have a paper trail.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :

Fix committed into lp:mir at revision None, scheduled for release in mir, milestone 0.0.4

Changed in mir:
status: In Progress → Fix Committed
Changed in mir:
milestone: none → 0.0.4
Changed in mir:
milestone: 0.0.4 → 0.0.5
Changed in mir:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.