This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Unable to free heap with HeapMem_free()

Hi,

I have an intermittent problem with using the HeapMem_free() and have no idea how to solve it, any help will be greatly appreciated.

 

Here is the description of the system:

Processor: c6472 

BIOS: 6.31.4.27    

IPC : 1.22.3.23

XDC: 3.20.8.88

XDAIS : 6.23

CCS : 4.2.4.00033

Code

#define CFG_HEAP_SIZE 0x03FFFFFF


#pragma DATA_SECTION(gHeapMemMgr_Heap, ".ddr2")
Uint8 gHeapMemMgr_Heap[2][CFG_HEAP_SIZE];

HeapMem_Handle gHeapMemHandle;
HeapMem_Params heapMemParams;
int i = 0;
int numPackets =219136;
int packetSize = 512;
char *storagePtr = NULL;

    // Initialize the heap handles
    HeapMem_Params_init(&heapMemParams);
    heapMemParams.buf = (void*) &gHeapMemMgr_Heap[0][0];
    heapMemParams.size = CFG_HEAP_SIZE;
    gHeapMemHandle = HeapMem_create(&heapMemParams, NULL);

    // Assert if fail to create heapMem Obj
    assertFatal( (gHeapMemHandle != NULL), 12345, (Uint32)gHeapMemHandle);
   
    for( i = 0; i < 1000; i ++)
    {    
    // Allocate memory
    storagePtr = HeapMem_alloc(gHeapMemHandle , (numPackets * packetSize), CACHELINE_SIZE, &errorBlock);

    // Assert if memory cannot be allocate
        assertFatal( (storagePtr != NULL), 12346, (Uint32)storagePtr);
        assertFatal( !Error_check(&errorBlock), 12347, Error_getCode(&errorBlock));

    // Free memory
    HeapMem_free(gHeapMemHandle, storagePtr, numPackets * packetSize );
          
    }

PROBLEM:

Often time, the HeapMem_free() will free the memory without any problem. However, every once in a while the HeapMem_free() will not be able to free the memory and causing the  HeapMem_alloc() to fail. And when that happens, the content of the variable "i" is fairly random. 

 

Thanks much,

Dominic

  • Here's more information, I didn’t pay much attention to the error code that was return from the HeapMem_alloc(). Instead I added another call to HeapMem_getStats() after HeapMem_alloc() and HeapMem_free() to retrieve the memory status.

    Often time, the memory status is as expected. However, when it doesn’t work, the PC will ended inside HeapMem_getStatus() infinitely and  the totalFreeSize and largestFreeSize will keep changing for the gMemoryStatPre variable.


    Expected results for memory status

     

    gMemoryStatPost = {...}

                totalSize = 67108856

                totalFreeSize = 53240

                largestFreeSize = 53240

     

     

    gMemoryStatPre = {...}

                totalSize = 67108856

                totalFreeSize = 67108856

                largestFreeSize = 67108856

     

    Modified code Snippet

    Memory_Stats gMemoryStatPre;

    Memory_Stats gMemoryStatPost;

     

     

    for( i = 0; i < 1000; i ++)
        {    
        // Allocate memory
        storagePtr = HeapMem_alloc(gHeapMemHandle , (numPackets * packetSize), CACHELINE_SIZE, &errorBlock);
       
    HeapMem_getStats( gHeapMemHandle, & gMemoryStatPost);


        // Assert if memory cannot be allocate
            assertFatal( (storagePtr != NULL), 12346, (Uint32)storagePtr);
            assertFatal( !Error_check(&errorBlock), 12347, Error_getCode(&errorBlock));

        // Free memory
        HeapMem_free(gHeapMemHandle, storagePtr, numPackets * packetSize );
        
    HeapMem_getStats( gHeapMemHandle, & gMemoryStatPre);

              
        }

     

  • Hi Dominic,

    have you looked at what the ROV is showing as the time the error happens?

    Are you using instrumented or non-instrumented BIOS libraries? The instrumented libs may catch something that we aren't aware of.

    In the .cfg file:

    BIOS.libType = BIOS.LibType_Instrumented;
    BIOS.assertsEnabled = true;
    BIOS.logsEnabled = true;

    Could you please attach the .c and .cfg files? What exactly are you trying to do?