×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Regular Contributor
    Join Date
    Jul 2004
    Posts
    59

    the usage of cleanup Stack

    i am a newbie,and i am confused about the usage of cleanup stack. i don't know when to use CleanupStack::PushL().
    thanks in advance.

    and there is a code belwo:
    HBufC* numberBuf = NULL;
    TPbkContactItemField* phonenoField = contact->FindField(EPbkFieldIdPhoneNumberMobile);

    if(phonenoField)
    {
    CContactTextField* phoneno = phonenoField->TextStorage();
    numberBuf = phoneno->Text().AllocLC();
    //delete phonenoField;
    //delete phoneno;
    .......

    i don't know if i need to push variable phonenoField and phoneno into cleanup stack .
    if don't ,whether i need to use DELETE to release the variables.

  2. #2
    Nokia Developer Champion
    Join Date
    Jul 2004
    Posts
    2,015
    You need to put automatic variables onto the stack that have been allocated on the heap (so in 99% of the cases they are 'C' types) or allocate handles (are 'R' Types) AND there is the possibility that your code will leave and thus your code to deacllocate from the heap or close the handle is not executed.

    There are plenty of tutorial papers and articles knocking around, I sugest you search the web and find some.

    In your code you do not put phonenoField on the stack because it does not 'own' any memory i.e. you would not put a call to delete phonenoField. However I see you have commented out a line to delete phonenoField. I am not familiar with findField() but I can tell trying to delete it is incorrect because this function must be returning a pointer and not ownership - I can tell this because a) if it was returning ownership of a pointer to some memory it shouldn't be returning a 'T' type but a 'C' type b) if it was returing ownership of memory it would first have to allocate memory so the function would be called FindFieldL(). If you uncomment the delete the code crashes right?

    With phoneno its presumambly the same thing, it might be a pointer to a 'C' class that is returned because the function is called TextStorage() and not TextStorageL() so it shouldn't be allocating any memory.

    The documentation should be checked for both of these functions to confirm they only return a pointer - if they don't they are breaking the Symbian conventions because they don't have L in their name.

    If both of these function *did* return ownership then you are now the owner of the memory so its your responsibility to delete it, so you'd have to push them onto the cleanup stack because the call to Text().AllocLC() may leave and your calls to delete wouldn't be reached.
    Last edited by hotcheese; 2004-08-17 at 16:04.

  3. #3
    Regular Contributor
    Join Date
    Jul 2004
    Posts
    59
    thanks for your reply

Posting Permissions

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