locating an alloc error

Login to reply to this topic.
Sat, 2008-03-15 02:19
Joined: 2007-10-21
Forum posts: 18

i wrote the most basic hello world GUI program which also implements an active object which appears after a fixed time interval when instantiated. The active object class's name is CDelayedHello its object is iDelayedHello.
I have instantiated this object in my App view class's ConstructL function :

.
.
.
.
void CSimpleExAppView::ConstructL(const TRect& aRect)
        {
    CreateWindowL();
    iDelayedHello = CDelayedHello::NewL();
    SetRect(aRect);
    ActivateL();
        }
.
.
.

Here is what my CDelayedHello looks like:

#include "SimpleEx.h"
#include <eikenv.h>

void CDelayedHello::ConstructL()
        {
        iEnv = CEikonEnv::Static();
        User::LeaveIfError(iTimer.CreateLocal());
        }

CDelayedHello::CDelayedHello()
: CActive(CActive::EPriorityStandard)
        {
        CActiveScheduler::Add(this);
        }

TInt CDelayedHello::RunError(TInt /*aError*/)
        {
        return KErrNone;
        }
// request

CDelayedHello* CDelayedHello::NewL()
        {
        CDelayedHello* self = new (ELeave) CDelayedHello();
        CleanupStack::PushL(self);
        self->ConstructL();
        CleanupStack::Pop(self);
        return self;
        }

CDelayedHello::~CDelayedHello()
        {
        Cancel();
        iTimer.Close();
        }

void CDelayedHello::SetHello(TTimeIntervalMicroSeconds32 aDelay)
        {
        _LIT(KDelayedHelloPanic, "CDelayedHello");
        __ASSERT_ALWAYS(!IsActive(), User::Panic(KDelayedHelloPanic, 1));

        iTimer.After(iStatus, aDelay);
        SetActive();
        }

// from CActive

void CDelayedHello::RunL()
        {
        iEnv->InfoMsg(_L("Message Flash!!"));
        }

void CDelayedHello::DoCancel()
        {
        iTimer.Cancel();
        }

Now as soon as i exit the application an alloc error ocurrs
APP Closed
ALLOC.1e9003240

The error dissappears if i comment out the instantiation of iDelayedHello in APPView's ConstructL. My destructor of the active object class looks fine to me... so wher's the memory leak happening??? plz help


Sat, 2008-03-15 08:18
Joined: 2005-12-07
Forum posts: 56
Re: locating an alloc error

make sure that you are deleting iDelayedHello in ~CSimpleExAppView

void CSimpleExAppView:: ~CSimpleExAppView()
{

if(iDelayedHello)
delete iDelayedHello;

}

Sat, 2008-03-15 09:54
Joined: 2005-11-20
Forum posts: 1058
Re: locating an alloc error

if(iDelayedHello)
delete iDelayedHello;

Just a small comment: Checking the iDelayedHello pointer before delete is unnecessary. C++ delete includes that NULL check itself already and silently does just nothing if the pointer is NULL.

Otherwise, I would agree: That's the probable source of the memory leak, given the information in the post.


René Brunner

Sat, 2008-03-15 12:56
Joined: 2005-12-07
Forum posts: 56
Re: locating an alloc error

agree

Sat, 2008-03-15 15:15
Joined: 2007-10-21
Forum posts: 18
Re: locating an alloc error

Thanks.. no errors now.but as rbrunner said.. doesnt it automatically happen in the destructor? why the need to write somethin that actuaaly happens (which din't happen in this case Puzzled )

Sat, 2008-03-15 17:36
Joined: 2005-11-20
Forum posts: 1058
Re: locating an alloc error

Ah, we seem to have a little misunderstanding here: Nothing happens automatically in the destructor. I just said that in C++ instead of writing
  if (pointer) delete pointer;
you can just write
  delete pointer;
without checking anything, because delete does that already for you.


René Brunner

Mon, 2008-03-17 04:57
Joined: 2007-10-21
Forum posts: 18
Re: locating an alloc error

Just wen i thought that my application was leak proof... got the familiar shock again!!! Thanks rbrunner for the info... i'm hopin u'l come to my rescue again Smiling The aim of the followin file is actually to read an incoming SMS and programmatically delete it. At LINE X in the following snippet i shall be replacing the code for displaying content l8r. Right now its just a message box.
The troublemaker this time is my SMSReceiver class. similar alloc error on exit. Here's the code for the class

CSMSReceiver::CSMSReceiver()
{
        // No implementation required
}


CSMSReceiver::~CSMSReceiver()
{
        if(iSession)
                delete iSession;   
        //iSession = NULL;
}

CSMSReceiver* CSMSReceiver::NewL()
{
        CSMSReceiver* self = new(ELeave)CSMSReceiver();
        self->ConstructL();
        return self;
}

void CSMSReceiver::ConstructL()
{
        iSession = CMsvSession::OpenSyncL(*this);
}

void CSMSReceiver::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* /*aArg3*/)
        {
                static TInt iNewMessageId = 0;

                switch (aEvent)
                        {
                                // A new entry has been created in the message server
                        case EMsvEntriesCreated:
                                {
                                if(*(static_cast<TMsvId *>(aArg2)) == KMsvGlobalInBoxIndexEntryId)
                                        {
                                        CMsvEntrySelection* entries = static_cast<CMsvEntrySelection *>(aArg1);
                                        iNewMessageId = entries->At(0);
                                        }

                                break;
                                }
                        case EMsvEntriesChanged:       
                                {
                                TMsvId* entryId = static_cast<TMsvId*>(aArg2);          // entry id from the session event
                                if (*entryId == KMsvGlobalInBoxIndexEntryId)  // new entry has been created in Inbox folder
                                        {
                                               
                                        CMsvEntrySelection* entries = static_cast<CMsvEntrySelection*>(aArg1);
                                        TInt index = entries->Find(iNewMessageId);
                                        if(KErrNotFound == index)
                                                {
                                                break;
                                                }

                                        _LIT(KMessage,"Message!!!!");
                                        CEikonEnv::Static()->AlertWin(KMessage);              //LINE  X
                                        entries=NULL;

                                       TMsvId entryId2;
                                                                               
                                        CMsvEntry* entry = iSession->GetEntryL(KMsvGlobalInBoxIndexEntryId);
                                        entries=entry->ChildrenL();
                                        entry=NULL;
                                        entryId2=entries->At(0);
                                        entry=iSession->GetEntryL(entryId2);
                                        CleanupStack::PushL(entry);

                                        TMsvEntry msvEntry = entry->Entry();

                                        //entry->DeleteL(msvEntry.Id());

                                        CleanupStack::PopAndDestroy(entry); // entry
                                        _LIT(KMsg,"Message deleted");
                                        CEikonEnv::Static()->AlertWin(KMsg);                                      //LINE Y


                                       
                                        }
                                break;
                                }
       
        // Handle close session
               case EMsvCloseSession:
                       {
                       iSession->CloseMessageServer();
                       break;
                       }
        // Handle server terminated
               case EMsvServerTerminated:
                       {
                       iSession->CloseMessageServer();
                       break;
                       }
               default:
                       {
                       break;
                       }
                        }

I'm even deleting the object in my view class's destructor this time.

.
.
.
CSimpleExAppView::~CSimpleExAppView()
        {
        if(iDelayedHello)
                delete iDelayedHello;
        if(rcvr)
                delete rcvr;
        }
.
..

Another wird thing happned when i ran this. On the arrival of a new message, the LINE X dialog popped only once, while the LINE Y dialog popped up twice !!! Puzzled Am totally freaked out with that non traceable memory leak and the strange sequence of dialog boxes??? Plz help

Mon, 2008-03-17 15:00
Joined: 2005-12-07
Forum posts: 56
Re: locating an alloc error

here is the leak..

CMsvEntry* entry = iSession->GetEntryL(KMsvGlobalInBoxIndexEntryId);
CleanupStack::PushL(entry);
entries=entry->ChildrenL();
CleanupStack::PopAndDestroy(entry); // entry

entry=NULL;
entryId2=entries->At(0);
entry=iSession->GetEntryL(entryId2);
CleanupStack::PushL(entry);
TMsvEntry msvEntry = entry->Entry();
//entry->DeleteL(msvEntry.Id());
CleanupStack::PopAndDestroy(entry); // entry
delete entries;

i think bold lines should solve the problem...

Mon, 2008-03-17 22:04
Joined: 2007-10-21
Forum posts: 18
Re: locating an alloc error

It seems symbian never fails to surprise you!! The leak is still there and is solely because of the alertwin function.. Here's my new HandleSessionEventL function which still is the cause of the leak:

                        case EMsvEntriesCreated:
                                {
                                      break;
                                }
                        case EMsvEntriesChanged:      
                                {
                                        CEikonEnv::Static()->AlertWin(_L("Message!!"));           

                                break;
                                }

Commenting out the alert shows no leak. Puzzled
Regarding the double message box problem i asked above, i checked out during debug, for every new message, the EMsvEntriesChanged event gets called twice (No clue why) I need to delete the incoming message but due to the event being called twice, another existing message in the inbox is geting deleted!! Still holed up by these two issues (memory leak & double deletion) help needed urgently!!

Tue, 2008-03-18 07:07
Joined: 2005-12-07
Forum posts: 56
Re: locating an alloc error

i think you need HookLogger now..

http://www.newlc.com/Hooklogger-Tracking-leaked-heap.html

  • Login to reply to this topic.