tengo este código ..¿Está bien lanzar manualmente un std :: bad_alloc?
CEngineLayer::CEngineLayer(void)
{
// Incoming creation of layers. Wrapping all of this in a try/catch block is
// not helpful if logging of errors will happen.
logger = new (std::nothrow) CLogger(this);
if(logger == 0)
{
std::bad_alloc exception;
throw exception;
}
videoLayer = new (std::nothrow) CVideoLayer(this);
if(videoLayer == 0)
{
logger->log("Unable to create the video layer!");
std::bad_alloc exception;
throw exception;
}
}
IEngineLayer* createEngineLayer(void)
{
// Using std::nothrow would be a bad idea here as catching things thrown
// from the constructor is needed.
try
{
CEngineLayer* newLayer = new CEngineLayer;
return (IEngineLayer*)newLayer;
}
catch(std::bad_alloc& exception)
{
// Couldn't allocate enough memory for the engine layer.
return 0;
}
}
he omitido la mayor parte de la información no relacionada, pero creo que la imagen es clara aquí.
¿Está bien lanzar manualmente un std :: bad_alloc en lugar de intentar/capturar todas las creaciones de capa individualmente y el inicio de sesión antes de volver a lanzar bad_allocs?
Una pequeña nota, si no está utilizando un puntero inteligente para el registrador, entonces se perderá si se lanza el constructor de CVideoLayer. –
Edité la parte de la capa de video porque aún no tengo una capa de video (todavía) y quería mostrar mi problema. Decidí hacerlo simple en lugar de preciso. – Jookia