Dynamic memory allocation hangs when using new.

17 Nov 2012

Is there any progress on how "new" works when dynamically allocating an instance of a class?

As I understand it, when the allocation fails it should do one of two things. Either it should return NULL or an exception is generated. I suspect in this environment it will return NULL. In practice it just seems to hang. For example, in the following program:

#include "mbed.h"
#include "Rectangle.h"

#define COUNT 500

typedef unsigned char u8;

static Rectangle<u8> *rectangles[ COUNT ];

int main() {
    Rectangle<u8> *rect;
    for( int i = 0; i < COUNT; ++i ) {
        printf( "Creating %d ", i );
        rect = new Rectangle<u8>( 1, 1, 10, 10 );
        fputs( rect == NULL ? "(FAILED)\r\n" : "(OK)\r\n", stdout );
        rectangles[ i ] = rect;
    }
    for( int i = 0; i < COUNT; ++i ) {
        printf( "Destroying %d\r\n", i );
        delete rectangles[ i ];
    }
    int i = 0;
    while( true ) {
        printf( "Still running! : %d\r\n", i++ );
        wait( 0.5 );
    }
}

It counts up to 192 and then stops, like this:

Creating 190 (OK)
Creating 191 (OK)
Creating 192 (OK)
Creating 193

I haven't bothered to include the code for Rectangle.h but it doesn't do anything particularly clever or dangerous.

I have seen other posts about this and it has been suggested that this bug has been fixed (or should have been) but I am using the latest update of the mbed library and it still fails.

Any chance of a fix on this? I realise there are various ways of figuring out how much memory you have left but it would be a much cleaner solution if I could just check for NULL after a call to new.

I am using an LPC11U24.

24 Jul 2014

Hi Richard, Im also facing a similar issue which i couldnt debug why..the Dynamic memory allocation using new operator gets hang at one point.. it neither throws memory allocation failure nor exits.. it just hang forever..But this hang is occuring at very rare condition..Below is the code snippet for which im facing issue..

for (temp = 0;temp < NUMOFPIXMAS;temp++) { try { memcpy(UiPngPath,UI_PATH,MAX_LENGTH); BmpTable[temp]= new QPixmap(strcat(UiPngPath,BmpFileNameTable[temp])); printf("Memallocated for %d .Size = %d . %d.",ntemp,sizeof(QPixmap),sizeof(*BmpTable[temp])); fflush(stdout); } catch (...) { mReportMemoryAlarm(); } system("free -b"); }

Here the for loop runs for 431 times and the process got hanged inside the try block forever after memalloc for 405th(not always 405th time) iteration.. Below are the debug printf lines for your reference..Here the printf and system(free -b) are debug lines added by me..

Memallocated for 404 .Size = 16 . 16. total used free shared buffers cached Mem: 64356352 63012864 1343488 0 1589248 43859968 -/+ buffers/cache: 17563648 46792704 Swap: 0 0 0

Memallocated for 405 .Size = 16 . 16. total used free shared buffers cached Mem: 64356352 63029248 1327104 0 1589248 43868160 -/+ buffers/cache: 17571840 46784512 Swap: 0 0 0

This process is a User interface process designed with Qt3.0 version and kernal version is 2.4.18.. I have surfed the internet and found your post and mine are same.. have u fixed this?? do you have any idea over this? pls suggest as im struck with this for over 2 months..

24 Jul 2014

Hello Arunkumar Ravi,

look at a thread: http://mbed.org/forum/mbed/topic/3803/?page=1#comment-24724 which might give you some ideas.

25 Jul 2014

Hi Martin, Thanks for your usefull information... i have gonethrough the link u shared.. also some of the interlinks available in the thread.. i too had a suspect on new.. i will debug more and share some more on this.. i will keep you posted..

31 Jul 2014

Hi Martin, From your referenced link, i took the program and made slight modifications which i have posted below..

#include "stdio.h"
#include "stdlib.h"

int main()
{
    printf("Start\r\n");
    int i =409600;
    printf("i is at %x\r\n", (long)(&i));
    int j=0;
    while(1)
    {
        printf("While:j is at %d.\n",j);
        fflush(stdout);
        void* test = malloc(i);
        int i2 = 2;
        printf("Mallocced %x bytes at %d, diff = %x\r\n", i, test, (long)(&i2) - (long)test);
        if (test == NULL)
        {
                printf("Malloc failure..No Memory available\n");
                break;
        }
        j++;
    }
}

Below is the output: Start

i is at bffffc64

While:j is at 0. Mallocced 64000 bytes at 1076625416, diff = 7fd3fc50

While:j is at 1. Mallocced 64000 bytes at 1077039112, diff = 7fcdac50

While:j is at 2. Mallocced 64000 bytes at 1077452808, diff = 7fc75c50

.......................................

While:j is at 5201. Mallocced 64000 bytes at -1978253280, diff = 35e9bc38

While:j is at 5202. Mallocced 64000 bytes at -1977614312, diff = 35dffc40

While:j is at 5203. Mallocced 64000 bytes at -1977204704, diff = 35d9bc38

While:j is at 5204.(It just stopped here..)

As mentioned above,it just stopped(literally Hanged forever) after printing 5204 on the console. The program niether printed malloc failure or exited... Why is it not entering the failure loop? Please share your thoughts on this?

01 Aug 2014

Hi Martin, Can you just look at the new thread https://mbed.org/questions/4220/Different-behaviors-of-New-operator-Dyna/ created by me. This is relevant too. pls share your thoughts..