Click here to Skip to main content
15,348,829 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,

I'im using the WTL 8 library, and I found there's a problem in one of the headers (atldlgs.h), which makes use of the GlobalReAlloc function.

Basically, the function fails, returns a NULL pointer, and GetLastError() returns 8, which means "not enough memory". Of course I have plenty of memory available... and the amount of requested bytes is very low.

Does some know the reason why GlobalReAlloc would fail? is it some bug ?
Or did I miss something?
Thanks in advance for any information about this,
Olivier.

I'm using Windows Vista SP2 + VS 2008.

Here is a simple program to reproduce the error:

stdafx.h:

XML
#pragma once
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <conio.h>



testGlobalReAlloc.cpp:

C#
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
    int size1 = 10240;
    int size2 = 20480;
    for (int i=0; i<15; i++)
    {
        HGLOBAL h = ::GlobalAlloc(GPTR, size1);
        if (NULL == h)
        {
            _tprintf(_T("NULL! %d\r\n"), i);
            break;
        }
        HGLOBAL h2 = ::GlobalReAlloc(h, size2, 0);
        if (NULL == h2)
        {
            ::GlobalFree(h);
            _tprintf(_T("NULL 2! %d\r\n"), i); // gets here the first time GlobalReAlloc is used
            break;
        }
        ::GlobalFree(h2);
        _tprintf(_T("ok %d\r\n"), i);
    }
    _tprintf(_T("press key\r\n"));
    getch();
    return 0;
}
Posted
Updated 4-Sep-10 3:03am
v4
Comments
CPallini 4-Sep-10 11:40am
   
You didn't specify GMEM_MOVEABLE. Is that a requirement?
olivier gg 8-Sep-10 9:19am
   
I have try to reproduce somewhat what is done by WTL 8.0 in one of its headers with GlobalAlloc and GlobalReAlloc.
(I initially encountered the GlobalReAlloc problem problem while using this nice library)

Hi,

This bug in WTL 8.0 (Memory for dialog template is allocated using GMEM_FIXED flag and re-allocated without GMEM_MOVEABLE flag)[^] is fixed in the current (beta) WTL 8.1 files.

If you want to stay on 8.0 you can download the fixed atldlgs.h at http://wtl.svn.sourceforge.net/viewvc/wtl/trunk/wtl/include/atldlgs.h?revision=346[^].

cheers,
AR
   
Comments
Niklas L 5-Sep-10 4:59am
   
Reason for my vote of 5
Spot on.
cccfff777 9-Sep-10 5:05am
   
Ah that answers also something I had a problem with some month ago! Thanks
Hi,

thanks a lot for the informations.

Best regards,
Olivier.
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900