GNOME Bugzilla – Bug 390978
Glib::Module does not report errors correctly
Last modified: 2007-04-16 15:53:25 UTC
When I use this code: std::string filename = Glib::Module::build_path(COMPONENT_DIR, PREFIX + p_name); Glib::Module* module = new Glib::Module(filename); try { if (!module) { throw std::runtime_error(Glib::Module::get_last_error()); } } catch(...) {...} I get an empty error message, while this. std::string filename = Glib::Module::build_path(COMPONENT_DIR, PREFIX + p_name); GModule* module = g_module_open(filename.c_str(), (GModuleFlags)0); try { if (!module) { throw std::runtime_error(g_module_error()); } } catch(...) {...} works fine!
So I think you are just saying that get_last_error() returns an empty string. Could we have a simpler test case then?
OK, debugged a bit more and the solution is trivial: if (!module) will check if the pointer to module is NULL. What I expected was (!module->operator bool()). IMHO this is a bit confusing but I doubt that we can change that. At least it is no "bug" of glibmm, it's just a bit ugly. Usually it would be better to throw an exception with last_error as argument when the construction of module fails but exceptions in constructors are also non-trivial.
If there is no Glib::Module operator bool() then we can add one. But I guess it's there and you just meant to do if (!(*module)).
Yes, there is an operator bool and if (!(*module)) will do what I expected.
Feel free to add some text to the class documentation to make this clearer, particularly in this case of using new and a pointer. I assume that we can close this bug.