Why do GlobalAlloc fail?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Why do GlobalAlloc fail?

  1. #1
    Join Date
    Jul 2001
    Posts
    273

    Why do GlobalAlloc fail?

    Hello,

    I try to allocate approx. 50MB of memory for a DIB:

    hDIB= GlobalAlloc(GHND,dw /*approx 50MB*/);

    It fails. But Windows task manager told me that more than 500MB available.

    The OS is win xp.

    Why??? What is the problem?
    Please help.

    br
    Ralf

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,004

    Re: Why do GlobalAlloc fail?

    Quote Originally Posted by Ralf Schneider View Post
    I try to allocate approx. 50MB of memory for a DIB:

    hDIB= GlobalAlloc(GHND,dw /*approx 50MB*/);

    It fails. But Windows task manager told me that more than 500MB available.

    The OS is win xp.

    Why??? What is the problem?
    This needs to allocate a contiguous block of 50MB. This may not be available, though many smaller blocks may be available that together make up more than 500MB.
    Maybe you could try preallocating the large block of memory early in your program. Or you could try enabling the low-fragmentation heap if you are running on XP.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Jul 2001
    Posts
    273

    Re: Why do GlobalAlloc fail?

    Hello,

    that sounds complicated.
    My problem occurs in a DLL.
    Do I have to enable the low-fragmentation heap at the beginning of the calling app?

    br
    ralf

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,004

    Re: Why do GlobalAlloc fail?

    Quote Originally Posted by Ralf Schneider View Post
    that sounds complicated.
    My problem occurs in a DLL.
    Do I have to enable the low-fragmentation heap at the beginning of the calling app?
    It's not that complicated, but you should do it early in your application (.exe).
    See the (second) example for HeapSetInformation.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  5. #5
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    7,994

    Re: Why do GlobalAlloc fail?

    Just an aside note:
    Generally, for the question "Why Xyz (Windows API) function fails?", GetLastError can give a more valuable answer than Task Manager.
    Have a look at: How to get the reason for a failure of a SDK function?
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center