mws wrote:not a design tip, but one for accurate programming

try to create the object - check for validity - push back into the vector if everything is ok.
- Code: Select all
Poco::SharedLibrary * somePluginLib = new (Poco::SharedLibrary(path));
if (somePluginLib != null ) // and maybe some more checks...
{
vector.push_back(somePluginLib);
}
pretty useless. There's no way that pointer will be null, if something fail an exception is thrown and the check will be not executed.
The problem is the exception thrown in the constructor (always avoided in my code throwing in constructors).
I thinked to somethink like that
- Code: Select all
class PluginWrapper
{
Poco::SharedLibrary * myPlugin;
public:
Poco::SharedLibrary * getPlugin(){ return myPlugin; }
PluginWrapper(std::string & path)
{
try
{
myPlugin = new Poco::SharedLibrary(path);
}
catch(exc&)
{
myPlugin = 0;
}
}
~PluginWrapper()
{
if(myPlugin)
{
myPlugin->unload();
delete myPlugin;
}
}
};
The only question now is: If the exception is thrown in the ctor does this cause a memory leach? AFAIK if the allocation is done there's no way to retrieve the pointer and deallocate it. But I don't know if the exception will stop the allocation or not.