03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

If the new(ELeave) fails, then iObject2 is still pointing to the previously allocated object,<br />

even though that object has been deleted. The result is going to be a double-deletion error,<br />

one way or another.<br />

Important<br />

When reallocating, you must always zero the pointer after delete and<br />

before new.<br />

The code should have been<br />

case EMagicCmdNew2:<br />

delete iObject2;<br />

iObject2 = NULL;<br />

iObject2 = new(ELeave) CX;<br />

iEikonEnv->InfoMsg(_L("New 2"));<br />

break;<br />

Once again, there is no need to set pointers to zero after a delete in the destructor<br />

because there is no chance that the pointer will ever be used again. But at all other times, if<br />

you delete something, you should zero its pointer immediately.<br />

6.2.5 How Does Leave Work?<br />

The User::Leave() function causes execution of the active function to terminate, and on<br />

through all calling functions, until the first function is found that contains a TRAP() or<br />

TRAPD() macro.<br />

User::Leave() is defined in e32std.h; you pass a single 32-bit integer error code.<br />

class User<br />

{<br />

public:<br />

IMPORT_C static Leave(TInt aErrorCode);<br />

...<br />

};<br />

The TRAP() and TRAPD() macros are also defined in e32std.h; edited slightly, they look<br />

like this:<br />

#define TRAP(_r,_s) { \<br />

TTrap __t; \<br />

if(__t.Trap(_r) == 0) \<br />

{ \<br />

_s; \<br />

TTrap::UnTrap(); \<br />

} \<br />

}<br />

#define TRAPD(_r,_s) TInt _r; \<br />

{ \

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!