Why does my app leak?
| Mon, 2008-06-30 14:20 | |
|
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 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 |
|






Forum posts: 463
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!!
Forum posts: 10
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
Forum posts: 463
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;
}
Forum posts: 1233
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...
Forum posts: 10
Thx for the tipp, I'm going to try out this hook logger.
Forum posts: 672
Off topic, but actually instead of doing:
if ( iManager){
delete iManager;
iManager = NULL;
}
you can just do (in the destructor only):
delete iManagerSince:
* 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.