NO

Author Topic: Stack Overflow when using malloc() inside __try {}  (Read 3719 times)

Zink

  • Guest
Stack Overflow when using malloc() inside __try {}
« on: December 06, 2009, 08:10:26 PM »
When I use malloc() and/or realloc() inside a block guarded with __try{} ... __finally{}, then the program causes Stack Overflow exception when exiting the function containing that code. This happens only for Pocket PC applications, and PPC needs soft reset after that.

PellesC version: 5.00.8 (can't test for 6, because of this problem: http://forum.pellesc.de/index.php?topic=3029.0).
WinCE version: WM2003SE.

Codeto reproduce the problem:
Code: [Select]

int *test; //test is a global variable

//I have used the autogenerated OnCommand handler, but the problematic code
//works the same regardless of the place it is used in.
static void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
    switch (id)
    {
        case IDM_ABOUT:
            __try {
        test = malloc(1 * sizeof(int));
    }
    __finally {

    }
            break;

        case (...) //all standard cases
    }
} // <-- exception occurs here

The exception occurs at the exiting of the function (if the function returns a value, then it occurs at the return xxx line).
The exception only occurs if the final size of the variable being malloc-ed is above zero. For example if after malloc() free(temp) or realloc(temp, 0) is used, the exception will not occur.