×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 23

Thread: member HBufC

  1. #1
    Regular Contributor
    Join Date
    Jan 2009
    Location
    India,Karnataka
    Posts
    432

    member HBufC

    here is a scenario

    scenario 1
    i want a member HBufC so i declare it in header file
    HBufC* heap;
    in cpp file i do following thing
    ....
    heap=HBufC::NewL(14);
    ....
    .....
    heap=HBufC::NewL(15);
    ...
    ....
    heap=HBufC::NewL(11);

    delete heap;

    does this cause memory leak?


    scenario 2
    heap=HBufC::NewL(14);
    delete heap;
    ....
    .....
    heap=HBufC::NewL(15);
    delete heap;
    ...
    ....
    heap=HBufC::NewL(11);
    delete heap;

    how about this is this correct?
    Last edited by lokesh_kumar_s; 2010-07-09 at 12:39.
    ---
    Regards
    Lokesh Kumar S.

  2. #2
    Nokia Developer Champion
    Join Date
    Mar 2006
    Location
    Helsinki, Finland
    Posts
    8,336

    Re: member HBufC

    Yes, scenario 1 will induce memory leak. Scenario 2 is fine.
    Nokia Developer Wiki Moderation team

  3. #3
    Regular Contributor
    Join Date
    Jan 2009
    Location
    India,Karnataka
    Posts
    432

    Re: member HBufC

    Quote Originally Posted by kiran10182 View Post
    Yes, scenario 1 will induce memory leak. Scenario 2 is fine.
    but in scenario 1 at the end i have deleted the heap right. so no pointer variable.

    i edited the question i just want to ask you have you saw delete heap; before answering?
    ---
    Regards
    Lokesh Kumar S.

  4. #4
    Regular Contributor
    Join Date
    Aug 2009
    Posts
    71

    Re: member HBufC

    Memory is allocated each time when you call NewL();
    every time you will have another pointer on heap. In scenario 1 you allocated three blocks and free only one.

    I advice you to use CleanUpStack and NewLC methods, it will save a lot of your time.

  5. #5
    Nokia Developer Moderator
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,144

    Re: member HBufC

    you definetely do not want to use CleanUpStack with member variables, anyway, with member variables, do set them to NULL after each deletion, so you can avoid double deletions causing nasty Kern-Exec 3 panics.

  6. #6
    Nokia Developer Champion
    Join Date
    Mar 2006
    Location
    Helsinki, Finland
    Posts
    8,336

    Re: member HBufC

    Quote Originally Posted by lokesh_kumar_s View Post
    but in scenario 1 at the end i have deleted the heap right. so no pointer variable.

    i edited the question i just want to ask you have you saw delete heap; before answering?
    Yes, I did read it. In scenario 1, each time to call NewL() , I believe it calls constructor in that code, which will allocate new area on the heap for your object and previously occupied memory will be leaked. Since you are calling delete only once, it will call respective detructor for NewL(11). But for NewL(14) and NewL(15), there is a memory leak.
    Nokia Developer Wiki Moderation team

  7. #7
    Regular Contributor
    Join Date
    Aug 2009
    Posts
    71

    Re: member HBufC

    2symbianyucca: Why not?

    As I remember we should push any allocated pointer to the cleanup stack, in case of leave, this memory will be freed.
    The only difference - if you intend to use this buffer further (outside the current method) just call CleanupStack::Pop(). If the value is necessary only in the method call CleanupStack::PopAndDestroy().

  8. #8
    Regular Contributor
    Join Date
    Jan 2009
    Location
    India,Karnataka
    Posts
    432

    Re: member HBufC

    Quote Originally Posted by symbianyucca View Post
    you definetely do not want to use CleanUpStack with member variables, anyway, with member variables, do set them to NULL after each deletion, so you can avoid double deletions causing nasty Kern-Exec 3 panics.
    Thanks yucca for your answer. But i want to know what is the need of setting the hbuf variable to NULL after each deletion. can you please guide me.
    ---
    Regards
    Lokesh Kumar S.

  9. #9
    Nokia Developer Champion
    Join Date
    Mar 2006
    Location
    Helsinki, Finland
    Posts
    8,336

    Re: member HBufC

    Quote Originally Posted by lokesh_kumar_s View Post
    But i want to know what is the need of setting the hbuf variable to NULL after each deletion. can you please guide me.
    Calling "delete" on already deleted object is undefined. But calling delete on NULL is fine. The following code snippet will give you KERN-EXEC 3.
    Code:
    heap=HBufC::NewL(14);
    delete heap;
    ....
    .....
    delete heap; // without your knowledge if you try to call delete again on already deleted object. Causes KERN-EXEC 3
    But the following code is fine:
    Code:
    heap=HBufC::NewL(14);
    delete heap;
    heap = NULL;
    ....
    .....
    delete heap; // Fine. No KERN-EXEC 3. Deleting NULL pointer is fine.


    And the follwing code is fine too:
    Code:
    heap=HBufC::NewL(14);
    delete heap;
    ....
    .....
    heap=HBufC::NewL(15); // Now "heap" points to some valid address 
    delete heap; // Fine. No KERN-EXEC 3
    Nokia Developer Wiki Moderation team

  10. #10
    Nokia Developer Champion
    Join Date
    Mar 2006
    Location
    Helsinki, Finland
    Posts
    8,336

    Re: member HBufC

    Quote Originally Posted by veon1 View Post
    As I remember we should push any allocated pointer to the cleanup stack, in case of leave, this memory will be freed.
    The only difference - if you intend to use this buffer further (outside the current method) just call CleanupStack::Pop(). If the value is necessary only in the method call CleanupStack::PopAndDestroy().
    Cleanupstack will be useful for the automatic varibles which are created on the heap. You really don't need to perform cleanup stack operations on member variables. The following case where cleanupstack will be useful:

    Code:
    void CMyClass::SomeLeavingFunctionL()
    {
            CAnotherClass* cl = CAnotherClass::NewL(); // Here cl is an automatic variable created on the heap
            CleanupStack::PushL(cl);
            cl->SomeAnotherLeavingFunctionL(); //If this function leaves then your "cl" will be freed by Cleanupstack
            CleanupStack::PopAndDestroy(cl);
    }
    Nokia Developer Wiki Moderation team

  11. #11
    Regular Contributor
    Join Date
    Jan 2009
    Location
    India,Karnataka
    Posts
    432

    Re: member HBufC

    thank you very much kiran. what about for class's object i.e., C type.

    is this valid

    Cbugger mbug=Cbugger::NewL(_L("mm.txt"));
    ....
    .....
    delete mbug;
    mbug=NULL;
    ...
    ...
    delete mbug;

    or do i need to create pointer variable like this

    Cbugger* mbug=Cbugger::NewL(_L("mm.txt"));
    ....
    .....
    delete *mbug;
    *mbug=NULL;
    ...
    ...
    delete *mbug;
    ---
    Regards
    Lokesh Kumar S.

  12. #12
    Nokia Developer Champion
    Join Date
    Mar 2006
    Location
    Helsinki, Finland
    Posts
    8,336

    Re: member HBufC

    "C" classes are supposed to be created on the heap. So eventually you end up using pointer variables for them. So if the mbug is your automatic variable then you should use the following:

    Code:
    Cbugger* mbug=Cbugger::NewL(_L("mm.txt"));
    ....
    // if you are going to perform some leaving operation on mbug then you might want to consider pushing it on the cleanupstack
    .....
    delete mbug; // delete operator works on pointer. And if you have pushed it on the cleanupstack then you should use PopAndDestroy of cleanupstack
    Nokia Developer Wiki Moderation team

  13. #13
    Regular Contributor
    Join Date
    Aug 2009
    Posts
    71

    Re: member HBufC

    2kiran10182:

    in this case

    Code:
    ...
    CAnotherClass* CMyClass::cl
    ...
    void CMyClass::CreateL()
    {
            cl = CAnotherClass::NewL(); // Here cl is an automatic variable created on the heap
            CleanupStack::PushL(cl);
            User::Leave(-1);
            CleanupStack::Pop(cl);
    }
    
    void CMyClass::~CMyClass()
    {
            if (cl) 
               delete cl;
    }
    It would not be useful?

  14. #14
    Nokia Developer Champion
    Join Date
    Mar 2006
    Location
    Helsinki, Finland
    Posts
    8,336

    Re: member HBufC

    As said: whether you put this way or that way, if cl is a member variable then it won't be useful.
    Nokia Developer Wiki Moderation team

  15. #15
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Budapest, Hungary
    Posts
    28,572

    Re: member HBufC

    Quote Originally Posted by veon1 View Post
    Code:
    ...
    CAnotherClass* CMyClass::cl
    ...
    void CMyClass::CreateL()
    {
            cl = CAnotherClass::NewL(); // Here cl is an automatic variable created on the heap
            CleanupStack::PushL(cl);
            User::Leave(-1);
            CleanupStack::Pop(cl);
    }
    
    void CMyClass::~CMyClass()
    {
            if (cl) 
               delete cl;
    }
    It would not be useful?
    No, it would not. This code is overcomplicated and wrong at the same time.
    Overcomplicated: why would you want to put anything on the Cleanup Stack, which could not be lost anyway?
    Wrong: delete (including delete by Cleanup Stack) does not NULL the pointer, so your "if(cl)delete cl;" would result in a KERN-EXEC 3. Though this "if(cl)delete cl;" sometimes appear even in official examples, it makes no sense. You can freely delete a NULL pointer at any time, and it does not prevent erroneous double deletions. "if(cl)" checks if cl is NULL or not, it does not say anything about its validity - C/C++ is not Java/C#.

Similar Threads

  1. How to append HBufC to HBufC?
    By sundy10 in forum Symbian
    Replies: 3
    Last Post: 2009-09-29, 11:02
  2. constant class member initialization (HBufC*)
    By FrankTheFox in forum Symbian
    Replies: 15
    Last Post: 2009-04-24, 18:51
  3. New Member
    By hilarymee in forum Bluetooth Technology
    Replies: 1
    Last Post: 2007-06-26, 21:45
  4. How can I use CArrayFixFlat as a member?
    By wuxiang00 in forum Symbian
    Replies: 3
    Last Post: 2005-02-01, 14:07

Posting Permissions

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