My function to fetch Contacts leaks memory, please help...

Login to reply to this topic.
Sun, 2006-03-05 18:41
Joined: 2005-11-14
Forum posts: 9
Hello.

I have a problem. I want to fetch some contacts from S60 phonebook using CPbkContactEngine, but something in my ShowContactsL() leaks memory. I think the problem is somewhere within the lines before "if( okPressed )", but I cannot be too sure though. If anyone could help me, I'd greatly appreciate!


Here's the code:

Code:
void CQueryView::ShowContactsL()
{
  RPbkViewResourceFile phonebookResource( *(CEikonEnv::Static() ) );
  if ( !phonebookResource.IsOpen() )
  {
    phonebookResource.OpenL();
  }

  CPbkMultipleEntryFetchDlg::TParams params;

  // Make an instance of the phonebook
  CPbkContactEngine* phonebookEngine = CPbkContactEngine::NewL();
  CleanupStack::PushL( phonebookEngine );
  params.iContactView = &phonebookEngine->AllContactsView();
 
  // Make the multiselection dialog
  CPbkMultipleEntryFetchDlg* fetcher = CPbkMultipleEntryFetchDlg::NewL( params, *phonebookEngine );
  fetcher->SetMopParent( this );

  TInt okPressed = fetcher->ExecuteLD();

  if ( okPressed )
  {
    TInt paramCount = params.iMarkedEntries->Count();

    TPbkContactItemField* url;

    // Insert participants' contact info to iMessage's iSIPAddresses
    for ( TInt i = 0; i < paramCount; ++i )
    {
      const TContactItemId contactID = ( *params.iMarkedEntries )[ i ];
      // Open the current contact from Phonebook (using Pbk-engine)
      CPbkContactItem* contact = phonebookEngine->ReadContactL( contactID );

      // Fetch the URL-field and first & last name
      if ( ( url = contact->FindField( EPbkFieldIdURL ) ) != NULL )
      {
        // Pass the contents of the URL field to the appropriate slot of
        // message's addresses-array
        iMessage->SetAddressL( url->TextStorage()->Text(), i );
      }
    }
  }
 
  phonebookResource.Close();
  CleanupStack::PopAndDestroy( phonebookEngine );
}

Sun, 2006-03-05 19:39
Joined: 2004-07-10
Forum posts: 364
Re: My function to fetch Contacts leaks memory, please help...
I don't know the phone book engine but totally obvious question to ask is where is contact deleted? (and does LD indicate that fetcher is automatically destroyed)
Sun, 2006-03-05 20:27
Joined: 2005-11-14
Forum posts: 9
Re: My function to fetch Contacts leaks memory, please help...
Hi, and thanks for your prompt reply.

Somehow the contact isn't deleted on my snippet...but I tried the following:
- introduce 'contact' in for-loop, delete 'contact' in for-loop
- introduce 'contact' right before 'if ( okPressed )', delete after the if
- introduce 'contact' in 'if ( okPressed )', and delete it right before the end-} of 'if'.

No luck though, I'm still getting the same panic when closing the app on emulator.
Mon, 2006-03-06 08:47
Joined: 2005-12-19
Forum posts: 5
Re: My function to fetch Contacts leaks memory, please help...
did you try reversing order of following lines at the end ?

phonebookResource.Close();
CleanupStack::PopAndDestroy( phonebookEngine );


Mon, 2006-03-06 10:57
Joined: 2005-11-14
Forum posts: 9
Re: My function to fetch Contacts leaks memory, please help...
Now that you mentioned it, I gave it a go. Unfortunately, it didn't help at all.

Thanks anyway!

Any other ideas...?
Mon, 2006-03-06 21:57
Joined: 2006-03-04
Forum posts: 194
Re: My function to fetch Contacts leaks memory, please help...
Have you heard of __UHEAP_MARK/MARKEND? Go and read about them and think how you could use them to diagnose your problem.



P.S.

>> - introduce 'contact' right before 'if ( okPressed )', delete after the if
- introduce 'contact' in 'if ( okPressed )', and delete it right before the end-} of 'if'.


Think. How would doing this prevent a memory leak if paramCount is > 1? When debugging software you have to think things through not just try things at random in the hope it'll solve things.


>> introduce 'contact' in for-loop, delete 'contact' in for-loop
If you are doing this in the correct place then the fact that you still have a memory leak afterwards tells you something, what could it tell you?
Tue, 2006-03-07 09:30
Joined: 2005-11-14
Forum posts: 9
Re: My function to fetch Contacts leaks memory, please help...
Hi NumptyAlert,

and thanks for your reply. No, I didn't know of __UHEAP_MARK and MARKEND, so I did a search for them. Found this: http://www.symbian.com/developer/techlib/tutorials/progtech.html

They seem to be really handy macros to have, and I also learned of a certain key-combination of ctrl-shift-alt-A. This certainly is bound to help me tracking the error, so a big thanks to you.

The reason why my problems may seem obvious and simple, is because I'm partly forced to program Symbian. You see, this is for a school project, and my actual areas of interest are HCI and user interfaces. Yet somehow I am partly responsible of the coding, which I know nothing about (actually, I know some Java and C#, but C++ and it's memory handling go WAY over my head, and Symbian...sigh).

So thanks for your patience, I'm off smacking my head to the wall then. Wink
Tue, 2006-03-07 18:26
Joined: 2006-03-04
Forum posts: 194
Re: My function to fetch Contacts leaks memory, please help...
If you already have a memory leak and know you have one then ctrl-shift-alt-A isn't any use in finding out what the cause is, its only real use is in assisting to reveal if you have them in the first place.

Now that you know about __UHEAP_MARK/MARKEND
use them in your code to narrow down where a memory leak might be, start off by putting MARK at the very start of CQueryView::ShowContactsL() and MARKEND at the very end of CQueryView::ShowContactsL(). This will confirm that you actually do have a memory leak in that function.

You need to delete contact, thats for sure, everytime its allocated - so inside the for loop. If you already tried deleting it in the loop and still have a problem it means you also have another memory leak elsewhere.
A D on the end of a function indicates the function is responsible for deleting something, in this case check the documentation to check that
if you call TInt okPressed = fetcher->ExecuteLD(); then fetched is deleted, if it isn't then you have another leak.

P.S.
There are techniques for finding a memory leak from the heap address reported when the leak occurs. There should be a paper on the Symbian site I think called something like how to track down memory leaks
Tue, 2006-03-07 18:53
Joined: 2005-11-14
Forum posts: 9
Re: My function to fetch Contacts leaks memory, please help...
Yes, I think I understood how to start the tracking process. A big thank you to you, I would still be unaware of those UHEAP-macros if it wasn't for you to tell me.  I try to not ask stupid questions again...  Wink
Sun, 2007-10-14 18:14
Joined: 2006-06-27
Forum posts: 34
Re: My function to fetch Contacts leaks memory, please help...

Hi anttti, i faced with the same problem on Series60 3rd edition.
I also using CPbkMultipleEntryFetchDlg and get memory leak if selecting any item from address book and pressing OK softkey, if nothing to select in address book and simple press Cancel then no memory leaks occurs.
I hope that you fight this memory leak, so can you give me some advice how to solve that problem?

  • Login to reply to this topic.