Use constructL for construction that might leave

We have to work around this restriction by providing a separate function to do any initialization that might leave. We call this function the second-phase constructor and, we usually name it ConstructL () .

In memorymagic, we have at last coded a class correctly: cz. This class is like cy, but it uses a second-phase constructor. Here's the class definition:

class CZ : public CBase {

public:

static CZ* NewLC(); void ConstructL(); ~CZ(); public:

Here are the constructL () and destructor functions (we'll return to NewL () and NewLC() later):

iX = new(ELeave) CX; }

So CZ: :ConstructL() performs the same task as cy::cy() , but the leaving function new (ELeave) is now in a second-phase constructor, rather than the C++ constructor. Here's how we can use it:

case EMagicCmdUse5: {

CZ* z = new(ELeave) CZ; CleanupStack::PushL(z); z->ConstructL(); z->iX->UseL();

CleanupStack::PopAndDestroy(z); }

It's quite clear here that ConstructL () is cleanup safe. You can try it out by running memorymagic and setting the heap to fail on the first, second, and third allocations.

0 0

Post a comment

  • Receive news updates via email from this site