×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Super Contributor
    Join Date
    Feb 2004
    Location
    Dallas
    Posts
    605

    Trap harness .TRAP and TRAPD

    Can someone tell me in very simple words what is trap harness?

    Also the difference between TRAP and TRAPD ?

  2. #2
    Nokia Developer Expert
    Join Date
    Apr 2003
    Location
    Finland
    Posts
    425
    Hello,

    In the Symbian OS there is used leaving method to indicate some error cases. A kind similar exception handler approach can be found from windows/c++ [try/catch].

    In Symbian it is even better, there is used cleanup stack, where objects (pointers to objects) can be stored. While some critical error happen these objects are destroyed automatically by the system. Here are some simple examples:

    In traditional OS:
    Code:
    CYour::SomethingL ()
    {
    char* text = new(aStream,KMaxTInt);
    Foo1 (); // this cause exception and text will cause memory leak because text is newer destroyed.
    Foo2 ();
    In Symbian OS:
    Code:
    CYour::SomethingL ()
    {
    // Pointer (text) is stored a kind of stack memory
    HBufC* text = HBufC::NewLC(aStream,KMaxTInt);
    
    // If this cause leave and the system will destroy all objects
    // from stack memory (cleanup stack) -> no memory leak.
    // SomethingL method will leave ("return") after this 
    Foo1L ();
    
    // Foo2L is not called becase leave cause 'jump out' from the method.
    Foo2L ();
    In some cases you want to continue your method flow without leave to happen (you want call to the Foo2L method). In this case you will use TRAP/TRAPD to 'catch' the leave.
    Code:
    CYour::Somethig ()
    {
    // text to the the cleanup stack
    HBufC* text = HBufC::NewLC(aStream,KMaxTInt);
    
    // this cause leave and the system will destroy all objects
    // from cleanup stack, which were putted after TRAP begin !
    TRAPD(err, Foo1L ());
    if (err != KErrNone)
    {
      // handle error case here
    }
    // Foo2L is now called becase leave of Foo1L is trapped.
    Foo2L ();
    // Consider do you need to take 'text' out from cleanup stack ?
    // yes..see above
    Difference of TRAP and TRAPD, the later is "trap with declare" and means there is no need to declare the member used to store error value. Code example:
    Code:
    TInt myErr = KErrNone;
    TRAP(myErr, FooL());
    // No need to declare myErr2...
    TRAPD(myErr2, FooL());
    Postfix C means the object is put automatically to cleanup stack. Obviously you can put object yourself to stack if there is not ready made method available:

    This leave/trap is well described in several essential books of symbian, my suggestion is to read some.

    Br V

  3. #3
    Super Contributor
    Join Date
    Feb 2004
    Location
    Dallas
    Posts
    605
    Thnx Vescola ..

    Your Explanation is Good.

  4. #4
    Super Contributor
    Join Date
    Apr 2003
    Location
    Czech Republic
    Posts
    915
    There are some docs about the basics of Symbian development available on Forum Nokia site. It might be a good idea to go through some of them. This kind of things is explained quite frequently in them...

    Also newlc.com site has some interesting tutorials that might be of an interest...:-)

  5. #5
    Super Contributor
    Join Date
    Feb 2004
    Location
    Dallas
    Posts
    605
    Wll Thanks all of you .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×