Why does my app leak?

Login to reply to this topic.
Mon, 2008-06-30 14:20
Joined: 2008-06-30
Forum posts: 10

Hi,

I'm using the example provided by newlc.com to read out the cell id (see http://www.newlc.com/Retrieving-IMEI-IMSI-Network-Info.html) but adding an instance of SystemManager to my own app results in an "ALLOC" error when I quit the app. I have no clue why this happens and all I did to use SystemManager is:

- declared a private instance of the SystemManager in the header of my class
- called the constructor in the ConstructL Method
- extended the destructor to delete the local SystemManager instance

After applying these three steps my app throws the alloc error when I exit it.

Does anybody have an idea what might cause this problem?

Cheers
Simon


Tue, 2008-07-01 09:23
Forum Nokia Champion
Joined: 2006-10-12
Forum posts: 463
Re: Why does my app leak?

Well, idea yes..the only thing that can be told from what you have described :

check each line of code to see memory allocations and see if the same are freed for each one of them!!

Tue, 2008-07-01 09:34
Joined: 2008-06-30
Forum posts: 10
Re: Why does my app leak?

Ok, here are is the code which causes the trouble:

#include <avkon.hrh>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <HelloWorldBasic.rsg>
#include <f32file.h>
#include <s32file.h>
#include <e32def.h>
#include <eiklabel.h>

#include "HelloWorldBasic.pan"
#include "HelloWorldBasicAppUi.h"
#include "HelloWorldBasicAppView.h"
#include "HelloWorldBasic.hrh"
#include "SystemManager.h"
#include "CellIdGetter."

_LIT( KHelloFileName, "\\private\\A000017F\\Hello.txt" );
_LIT( KHelloText, "HELLO WORLD!");
_LIT( KNeuerText, "Neuer Text");


void CHelloWorldBasicAppUi::ConstructL()
    {
    BaseConstructL(CAknAppUi::EAknEnableSkin);
        iAppView = CHelloWorldBasicAppView::NewL( ClientRect() );
    iManager = CSystemManager::NewL();
   
    }

CHelloWorldBasicAppUi::CHelloWorldBasicAppUi()
    {
    }

CHelloWorldBasicAppUi::~CHelloWorldBasicAppUi()
    {
    if ( iAppView )
        {
        delete iManager;
        delete iAppView;
        }
    }

void CHelloWorldBasicAppUi::HandleCommandL( TInt aCommand )
    {
    switch( aCommand )
        {
        case EEikCmdExit:
        case EAknSoftkeyExit:
            Exit();
            break;

        case EHelloWorldBasicCommand1:
            {
           
            iManager->GetNetworkInfoL(aLocationAreaCode, aCellId);
            TBuf<20> numBuf;
            numBuf.AppendNum(aCellId);
           
            iAppView->SetTextL(numBuf);
            }
            break;
       
                default:
            Panic( EHelloWorldBasicUi );
            break;
        }
    }

void CHelloWorldBasicAppUi::HandleStatusPaneSizeChange()
{
        iAppView->SetRect( ClientRect() );
       
}

As the alloc error doesn't appear when I commen everything concerning SystemManager out I suppose this to be the guilty object. But I added the delete line to destructor as it is supposed to be so I don't understand why the error occurs.
Maybe you see more than I do.

Simon

Tue, 2008-07-01 12:23
Forum Nokia Champion
Joined: 2006-10-12
Forum posts: 463
Re: Why does my app leak?

You are allocating iManager independently. I would have deleted it independently in the destructor.

Also its a good practive to write it like :

if ( iManager)
        {
        delete iManager;
        iManager = NULL;
        }

Tue, 2008-07-01 12:30
Joined: 2004-11-29
Forum posts: 1233
Re: Why does my app leak?

Seems to me that there is some problem in either how you use that "SystemManager", or there is a bug in it.
I have no experience with that piece of code, so can't really help.

Though, to find out the callstack of your leak, to easily pinpoint what function call causes it, try use this extremely useful tool:
http://developer.symbian.com/main/tools_and_sdks/developer_tools/supported/hook_logger/index.jsp

I have lost count on how many hours this wonderful tool has saved me when looking for leaks.
Its probably weeks... Smiling

Wed, 2008-07-02 07:59
Joined: 2008-06-30
Forum posts: 10
Re: Why does my app leak?

Thx for the tipp, I'm going to try out this hook logger.

Wed, 2008-07-02 08:06
Joined: 2003-12-05
Forum posts: 672
Re: Why does my app leak?

Off topic, but actually instead of doing:

if ( iManager)
        {
        delete iManager;
        iManager = NULL;
        }

you can just do (in the destructor only):
delete iManager

Since:
* you do not have to check if the pointer is pointing to somewhere, since delete NULL does nothing.
* with CBase'd objects, you are guaranteed to have the pointer NULL after construction.
* if the pointer is not valid, it will crash your application both ways anyway.
* there is no need to set the pointer to NULL in the end, since you are in the destructor now, and nobody is going to use that member variable to anything after destructor is left.

Instead, if you are deleting your object outside the destructor, then of course you always must set the pointer to NULL after deleting the object.

  • Login to reply to this topic.