ich diesen Code haben ..Ist es in Ordnung, einen Std :: Bad_alloc manuell zu werfen?
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;
}
}
Ich habe die meisten der nicht-verwandten Informationen weggelassen, aber ich denke, das Bild klar ist hier.
Ist es in Ordnung, manuell ein Std :: bad_alloc zu werfen, anstatt alle Layer-Kreationen einzeln zu erfassen und abzufragen, bevor bad_allocs erneut gestartet wird?
Eine kleine Anmerkung, wenn Sie nicht einen intelligenten Zeiger für Logger verwenden, dann wird dies leckt, wenn CVideoLayer Konstruktor wirft. –
Ich habe den Video Layer Teil bearbeitet, da ich eigentlich noch keinen Video Layer habe und mein Problem zeigen wollte. Ich entschied mich dafür, es einfacher zu machen, anstatt genau zu sein. – Jookia