__UHEAP_FAILNEXT tests

Login to reply to this topic.
Mon, 2005-06-27 10:47
Joined: 2005-06-27
Forum posts: 3
Hi,

I've seen code of the form:

Code:
{
    TInt err = KErrNoMemory;
    TInt i = 1;

    while (err == KErrNoMemory)
        {
            __UHEAP_FAILNEXT(i++);
            TRAP(err, SomeFunction());
       }
    __UHEAP_RESET();

    DisplayResults(i - 1);
}

Q: Can anyone tell me the purpose of these tests?
What I think it's doing is finding out how many heap allocations are necessary for SomeFunction() to execute correctly.

Also, some of these tests take a long time.
Q: Is it really necessary to iterate for i, or can a quicker search be used such as a binary search.
I have implemented this and get the same results, but I'm not sure it's ok to do so as I may be missing out on something.
So if in the above example, if the test passes with i = 10, will it always pass for values of i >= 10?

Mon, 2005-06-27 13:47
Joined: 2004-07-28
Forum posts: 1379
Re: __UHEAP_FAILNEXT tests
It's calling SomeFunction (should really be called SomeFunctionL) with the heap failure rate increased once each time.  For example, with i set to 1, the first memory allocation SomeFunctionL does will fail (leave with KErrNoMemory).  The next iteration round, with i set to 2, the first allocation will succeeded, but the second will leave with KErrNoMemory.

It's all about testing the code in SomeFunction for cleanup handling  -  making sure evey object goes onto the cleanup stack, and in tern making sure everything gets cleaned up nicely if memory allocation failes.

For example, if SomeFunction was coded like this:

void SomeFunctionL()
{
HBufC* p1 = HBufC::NewLC();
HBufC* p2 = HBufC::NewLC();
HBufC* p3 = HBufC::NewL();
HBufC* p4 = HBufC::NewLC();

CleanupStack::PopAndDestroy(p1);
CleanupStack::PopAndDestroy(p2);
delete p3;
CleanupStack::PopAndDestroy(p4);


Then the testing code would panic with an alloc panic when i = 4.  Why?  Because the allocation for "HBufC* p4 = HBufC::NewLC();" is set to fail, causing a leave with KErrNoMemory.  This will cause p1 and p2 to be popped and destroyed as expected, but will cause a leak from p3 since it's not on the CS when it should be.  If you didn't write testing code like above, you might never notice that bug, since it relys on the allocation for p4 failing.  If it doesnt, as it wont 99.9% of the time, the code will pass without error.

didster

Mon, 2005-06-27 14:28
Joined: 2005-06-27
Forum posts: 3
Re: __UHEAP_FAILNEXT tests
Sweet,

thanks
  • Login to reply to this topic.