Function returning HBufC*

Login to reply to this topic.
Thu, 2008-01-17 18:12
Joined: 2005-09-04
Forum posts: 145

Hello all
Please take a look at the code below.
Using the function GetSearchVal() as below - will it result in a memory leak?

If yes, what would be the recommended way to to use functions like this directly (without assigning to a variable)?

Thank you

void CMyApp::DoSomething()
{
	if (aTBufVar.Find(GetSearchVal()->Des()) == 0)
	{
		//Do something
	}
}

HBufC* CMyApp::GetSearchVal()
{
	HBufC* buf = HBufC::NewL(20);
	//Put a value in buf
	return buf; 				
}


Fri, 2008-01-18 09:11
Joined: 2007-05-15
Forum posts: 14
Re: Function returning HBufC*

First you need to assign that HBufC to a variable and after using it, it needs to be deleted.

Fri, 2008-01-18 09:43
Joined: 2007-12-19
Forum posts: 49
Re: Function returning HBufC*

You can write something like this

class CMyApp:
{
...
HBufC* iLastSearchVal;
...
};

void CMyApp::DoSomething()
{
        if (aTBufVar.Find(GetSearchVal()->Des()) == 0)
        {
                //Do something
        }
}

HBufC* CMyApp::GetSearchVal()
{
        if ( iLastSearchVal != NULL )
        {
                delete iLastSearchVal;
                iLastSearchVal = NULL;
        }
        iLastSearchVal = HBufC::NewL(20);
        //Put a value in buf
        return iLastSearchVal;
}

CMyApp::~CMyApp()
{
        ...
        if ( iLastSearchVal != NULL )
        {
                delete iLastSearchVal;
                iLastSearchVal = NULL;
        }
        ...
}

But you should avoid such coding style.

Fri, 2008-01-18 12:38
Joined: 2005-09-04
Forum posts: 145
Re: Function returning HBufC*

Thank you very much kriger and lman !

Sat, 2008-01-19 11:06
Joined: 2007-05-15
Forum posts: 14
Re: Function returning HBufC*

Ok, the most tidy solution would be

void CMyApp::DoSomethingL()
{
        HBufC* myDescriptor = GetSearchValLC();
        if (aTBufVar.Find(myDescriptor ->Des()) == 0)
        {
                //Do something
        }
        CleanupStack::PopAndDestroy( myDescriptor );
}

HBufC* CMyApp::GetSearchValLC()
{
        HBufC* buf = HBufC::NewLC(20);
        //Put a value in buf
        return buf;                                
}

Sun, 2008-01-20 07:31
Joined: 2005-09-04
Forum posts: 145
Re: Function returning HBufC*

Thank you kriger
If I understand correctly, your suggestion would also work without assigning the variable and with using PopAndDestroy() , right?

void CMyApp::DoSomethingL()
{
        if (aTBufVar.Find(GetSearchValLC() ->Des()) == 0)
        {
                //Do something
        }
        CleanupStack::PopAndDestroy( );
}

HBufC* CMyApp::GetSearchValLC()
{
        HBufC* buf = HBufC::NewLC(20);
        //Put a value in buf
        return buf;                                 
}

Mon, 2008-01-21 09:05
Joined: 2007-05-15
Forum posts: 14
Re: Function returning HBufC*

Yes, it will work but it is more readable and you know what object are popping.

  • Login to reply to this topic.