GNOME Bugzilla – Bug 557290
Glib::NodeTree doesn't have virtual destructor
Last modified: 2009-04-26 22:39:25 UTC
Somebody on IRC was having problems subclassing Glib::NodeTree. He was trying to do something like: class Foo : public Glib::NodeTree<Glib::Dir> {...} Which was failing since Glib::Dir is non-copyable. So one possible solution would be to change it to be Glib::NodeTree<Glib::Dir*>, but that would require that you free the data member in the destructor, like so: Foo::~Foo() { delete data(); } However, Glib::NodeTree does not have a virtual destructor, so this becomes quite risky. If the node is ever freed as a base class pointer, the data member will not get freed. So I guess we should either document very clearly that NodeTree should not be subclassed, or break ABI to make the destructor virtual...
Classes with no virtual destructors just shouldn't be used polymorphically. If you try to add a virtual method so you can do that then g++ will warn you. So I don't think people are likely to do this without seeing a warning, and I don't think it needs documentation - we have plenty of other classes with no virtual destructors. I generally like to have virtual destructors, for flexibility, but other people don't like increasing object sizes unnecessarily.
fair enough, I just wasn't thinking clearly enough while he was asking the question and I figured that a note might help for others who don't realize that it doesn't have a virtual destructor