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.

Compiler/TMS320F28069: malloc failed

Part Number: TMS320F28069

Tool/software: TI C/C++ Compiler

description:

 CCS Version: 7.0.0.00042 

  C2800 Compiler Tools 16.9.1.LTS 

the program always down, with the malloc failure:

what the program is doing is to receive USB message (with about 140 bytes payload), then break it down to CAN messages (up to 8 byte payload),  and forward the CAN message reply from device to the USB port.

there's some malloc( ) and free( ) in the code.

I allocated a big heap size (len: 0x2000, from 0xA000) in the linker configuration

.esysmem 1 0000a000 00002000 UNINITIALIZED
0000a000 00000001 rts2800_fpu32.lib : memory.obj (.esysmem)
0000a001 00001fff --HOLE-

I also wrote a array to record the start address and length of allocated  memory for each malloc / delete operation.

bool recordMalloc(Uint32 startAddr, Uint32 len)
{
#ifdef MEMORY_RECORD_LEN

    int i;
    for (i=0; i<MEMORY_RECORD_LEN; i++) {
    if (memory_record[i].len == 0) {
        memory_record[i].startAddr = startAddr;
        memory_record[i].len = len;
        return true;
    }
}
return false;

#endif
}

bool recordFree(Uint32 startAddr)
{
#ifdef MEMORY_RECORD_LEN

    int i;
    for (i=0; i<MEMORY_RECORD_LEN; i++) {
        if (memory_record[i].startAddr == startAddr) {
        memory_record[i].startAddr = 0;
        memory_record[i].len = 0;
        return true;
    }
}
return false;

#endif
}

I find that when the program dies there seems no memory leakage:

it's still tons of heap memory left.

Any clue? thanks.

  • I saw the program stops here after malloc / free for about 1400 times:

  • Hello Lei,

    I have moved your post to the compilers forum.

    Best Regards,
    Adam Dunhoft
  • The C28x RTS library comes with extra malloc debugging functions. What do the functions free_memory() and max_free() return when malloc fails?
  • hi Archaeologist,

    Thanks! I think this will help. I have not used these two functions before and have not found them in help page of CCS.
    Shall I call them in any console or embedded these functions in my application code before the malloc() operation?

    Thanks.

    Charlie

  • Lei Cai said:
    have not found them in help page of CCS

    They are not documented.  But you do have the source code for them.  They are in the RTS source file memory.c, which is located in a directory similar to:

    C:\ti\ccsv7\tools\compiler\ti-cgt-c2000_16.9.2.LTS\lib\src

    You'll find these source lines ...

    /*****************************************************************************/
    /*                                                                           */
    /*  FREE_MEMORY - returns the total amount of free memory available for      */
    /*		  allocation.  The memory may be fragmented.                 */
    /*                                                                           */
    /*****************************************************************************/
    int free_memory(void)
    ...
    
    /*****************************************************************************/
    /*                                                                           */
    /*  MAX_FREE - returns the size of the largest single block of memory	     */
    /*	       available for allocation.                                     */
    /*                                                                           */
    /*****************************************************************************/
    int max_free(void)
    

    Lei Cai said:
    Shall I call them in any console or embedded these functions in my application code before the malloc() operation?

    Embed them in the application.  Capture the return value in a variable you can see in the watch window.

    Thanks and regards,

    -George

  • Dear George,

    I embedded the get_free() and max_free() after each malloc() and free() operation, and recorded the history result in two rolling buffer.

    Unexpected result: no memory leakage, no memory fragmentation.

    Seems the memory management structure was damaged, perhaps overwritten by something?

    Best Regards,

    Charlie

  • some additional info:

    after I embedded the free_memory() and max_free() code, the program began to enter an endless loop after some operation in the free_memory() than malloc(). The MCU is not down, it's still working, however the chain list of memory structures was broken and cannot get out of the loop (line 620 of memory.c)
  • Somehow, the first few words of the heap were overwritten with 0.  Of course, none of the malloc routines work after that.  Unfortunately, I cannot tell you how this occurred.  It seems likely that code elsewhere in your application ran away and wrote 0 into some range of memory it shouldn't have, including the heap.

    Thanks and regards,

    -George

  • dear George,

    I agree with you. The unexpected overwrite to the heap should be the root cause. Thank you.

    Best Regards,

    Charlie