My function to fetch Contacts leaks memory, please help...
| Sun, 2006-03-05 18:41 | |
|
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 ); } |
|






Forum posts: 364
Forum posts: 9
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.
Forum posts: 5
phonebookResource.Close();
CleanupStack::PopAndDestroy( phonebookEngine );
Forum posts: 9
Thanks anyway!
Any other ideas...?
Forum posts: 194
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?
Forum posts: 9
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.
Forum posts: 194
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
Forum posts: 9
Forum posts: 34
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?