Memory leak

Login to reply to this topic.
Fri, 2008-01-11 10:00
Joined: 2005-09-04
Forum posts: 145

Hi
it seems that I have a memory leak using the following view as I get a "memory full" message after several times of switching to it.
I tried the hooklogger but for now can't find the leak (can't find the relevant allocation).
I think that the reason of the leak is the text array that I use in SetItemTextArray() - its a large array and I can't delete it,not in the destructor and not as a local var (I get E32User Cbase 21 when trying to reset or delete it).
I guess there is something basic here that I do wrong,
please take a look at the code and help me prevent these memory problems.

Thank you very much

//############# VIEW ##############
void CMyAppView::DoActivateL(const TVwsViewId& ,TUid ,const TDesC8& )
{
	if ( !iContainer )
    {
		iContainer = CMyAppViewContainer::NewL(this);
        iContainer->SetMopParent(this);
		TRect rect = ClientRect();
		iContainer->ConstructL(rect);
        AppUi()->AddToStackL( *this, iContainer );
		SetTitlePaneL();
        iContainer->MakeVisible( ETrue );
    }
}

void CMyAppView::DoDeactivate()
{
    if (iContainer)
    {
        AppUi()->RemoveFromStack(iContainer);
        delete iContainer;
    }
}
//############# VIEW CONTAINER ##############
void CMyAppViewContainer::ConstructL( const TRect& aRect )
{
	CreateWindowL();     
    SetRect( aRect );
	MakeListBoxL();
    ActivateL(); 
	DrawNow();
}

CMyAppViewContainer::~CMyAppViewContainer()
{
	if(iFindBox)
		delete iFindBox;
	if(iListBox)
		delete iListBox; 
	if (textArray) 
		delete textArray; //### E32User Cbase 21 error message!
}

void CMyAppViewContainer::MakeListBoxL()
{
    iListBox   = new( ELeave ) CAknSingleStyleListBox();
	iListBox->ConstructL(this,EAknListBoxSelectionList);
        //### textArray:tried also as a local var
	textArray = new (ELeave) CDesCArrayFlat(iArrayCount);
	//### here I put values to textArray using AppendL()
	iListBox->Model()->SetItemTextArray(textArray);
    iListBox->Model()->SetOwnershipType(ELbmOwnsItemArray);
	iListBox->CreateScrollBarFrameL( ETrue );
    iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto );
	iListBox->SetRect(Rect());
	iFindBox = CreateFindBoxL(iListBox, iListBox->Model(),CAknSearchField::ESearch);
	SizeChanged();
	iListBox->ActivateL();
	iListBox->SetCurrentItemIndex(0);
	UpdateScrollBar(iListBox);
}


Fri, 2008-01-11 10:32
Joined: 2005-11-20
Forum posts: 1242
Re: Memory leak

With this statement

iListBox->Model()->SetOwnershipType(ELbmOwnsItemArray);

you hand the array over to the listbox that will own it and also free it. If you try to free the array yourself (a second time, then), this won't work.

I am not quite sure, but I think the problem is here:

void CMyAppView::DoDeactivate()
{
    if (iContainer)
    {
        AppUi()->RemoveFromStack(iContainer);
        delete iContainer;
    }
}

'delete iContainer' contrary to what you probably expect does *not* set 'iContainer' to NULL. Try to set it to NULL after the delete and check what happens.


René Brunner

Fri, 2008-01-11 16:32
Joined: 2005-09-04
Forum posts: 145
Re: Memory leak

Thank you very much rbrunner
I made these 2 changes (set container to null and not deleting the textArray).

I still get the "memory full" error,
this happens after many switches from a setting list to this view.
I also got one time (during some changes and tests) a strange error: "aknlayouttreepan 6".

Do you have any idea what this error means (searches resulted with nothing)?
Can it be related to the memory problem?

Can you please refer me to a good article about releasing memory in symbian ? (I found a few but they didn't give me enough info)

Thank you

  • Login to reply to this topic.