Announcement

Collapse
No announcement yet.

HeapAlloc/Free/Size problems

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • mark smit
    Guest replied
    Thanks Florent!

    I only did that because I wanted some sort of notification if
    there was an error of any kind.

    ------------------
    Cheers

    Leave a comment:


  • Florent Heyworth
    replied
    Hi Mark

    just a tip: I noticed that you are using HEAP_GENERATE_EXCEPTIONS when
    calling HeapAlloc; you should only use this if you really want to
    display a GPF message box to your users in the case that the HeapAlloc function
    fails (out of memory, or..)

    The HEAP_GENERATE_EXCEPTIONS modifier, is in general only called
    by applications which use SEH (Structured Exception Handling). I'd avise
    you not to use this flag. Use 0& or HEAP_ZERO_MEMORY to initialize
    memory or HEAP_NO_SERIALIZE in single threaded applications.

    That way if HeapAlloc fails it'll simply return NULL instead of
    an exception.

    Cheers

    Florent

    ------------------

    Leave a comment:


  • Jim Huguley
    replied
    Mark..

    1. The GlobalAlloc is only maintained for backward compatibility with
    16 bit code. It is recommended that you use the newer heap function.
    So, I would assume unless you have a compeling reason to use
    GlobalAlloc with the GMEM_? constants you move on to them.

    2. According to MSDN, the memory is not movable. The pointer is
    valid until you de-allocate the memory. Of course you will have
    to maintain the pointer in a manner within the scope of your usage
    (i.e. sub/function/global/etc..)

    3. There are some thread issues if your program is multi-threaded
    that you should read about in the Remarks on MSDN under HeapFree
    and HeapAlloc.



    ------------------
    Jim..
    [email protected]

    Leave a comment:


  • mark smit
    Guest replied
    Thanks!

    I'm also having a hard time understanding how the Global and Local
    memory functions work. I know they must call the Heap functions
    but I dont see how you can set the %GMEM_MOVABLE and %GMEM_FIXED
    properties with the Heap functions. Do the Global and Local functions
    use the Heap functions at a lower level?

    Also, when you use Heap functions to allocate memory does the pointer
    returned stay valid throughout the life of the allocated block or
    does the block move around and thus invalidate the pointer?




    ------------------
    Cheers

    Leave a comment:


  • Jim Huguley
    replied
    Mark..

    Call HeapSize third parameter with Byval since the third parameter
    of HeapSize in the Win32API is defined "as ANY"..

    Code:
    #COMPILE EXE
    #INCLUDE "win32api.inc"
    
    FUNCTION PBMAIN AS LONG
        LOCAL lHeapPtr AS LONG
        LOCAL lsize    AS DWORD
    
        lHeapPtr = HeapAlloc(GetProcessHeap,%HEAP_GENERATE_EXCEPTIONS,1024)
        lsize = HeapSize(GetProcessHeap, 0, BYVAL lHeapPtr)
    
        MSGBOX FORMAT$(lHeapPtr) & $CRLF & FORMAT$(lsize) & $CRLF & "&h" & HEX$(lsize)
    
        lHeapPtr = HeapFree(GetProcessHeap,0,lHeapPtr)
        lsize = HeapSize(GetProcessHeap, 0, BYVAL lHeapPtr)
    
        MSGBOX FORMAT$(lHeapPtr) & $CRLF & HEX$(lsize) & $CRLF & "&h" & HEX$(lsize)
    END FUNCTION

    ------------------
    Jim..
    [email protected]

    Leave a comment:


  • mark smit
    Guest started a topic HeapAlloc/Free/Size problems

    HeapAlloc/Free/Size problems

    Hello all,


    I was just wondering why HeapSize returns (-1/FFFFFFFF). This indicates
    that the function failed but I can't figure out why?

    Code:
    #compile exe
    #include "win32api.inc"
    
    
    function pbmain as long
        dim lookup  as long ptr
        dim lsize   as long
        
        
        lookup = HeapAlloc(GetProcessHeap,%HEAP_GENERATE_EXCEPTIONS,1024)
        lsize = HeapSize(GetProcessHeap,0,lookup)
        
        
        msgbox format$(GetProcessHeap)
        
        
        msgbox format$(lookup) & $CRLF & hex$(lsize)
        
        lookup = HeapFree(GetProcessHeap,0,lookup)
        lsize = HeapSize(GetProcessHeap,0,lookup)
        
        msgbox format$(lookup) & $CRLF & hex$(lsize)
    end function
    Thanks for any help!

    ------------------
    Cheers
Working...
X
😀
🥰
🤢
😎
😡
👍
👎