Announcement

Collapse

Maintenance

The forum could be offline for 30-60 minutes in the very near future for maintenance (said 3pm Pacific). I was behind on getting this notice. I do apologize.
See more
See less

Heap problems: double allocations

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

  • Heap problems: double allocations

    Hello Everybody,


    I have been playing around with the heap functions and have seen
    something that doesn't make sense. I have included an example of
    the code below.

    Heres the problem, HeapCreate should preallocate a block of memory
    from which HeapAlloc can request a portion. When I create my own
    heap the task manager reports that X amount has been allocated(OK).
    but when I call HeapAlloc to request memory from my own heap the
    tasm manager reports that even more memory get allocated.

    HeapCreate(5000000 bytes) Task manager shows increase of ~5000000 bytes
    HeapAlloc(myheap,2000000) Task manager shows increase of ~2000000 bytes

    As I understand it this shouldn't happen until I request more memory
    than what I reserved with HeapCreate.

    Code:
    #compile exe
    #register none
    
    
    %HEAP_NO_SERIALIZE                           = &h00000001
    %HEAP_GROWABLE                               = &h00000002
    %HEAP_GENERATE_EXCEPTIONS                    = &h00000004
    %HEAP_ZERO_MEMORY                            = &h00000008
    
    
    declare function HeapFree lib "KERNEL32.DLL" alias "HeapFree" (byval long,byval long,any) as long
    declare function HeapAlloc lib "KERNEL32.DLL" alias "HeapAlloc" (byval long,byval long,byval long) as long
    declare function HeapCreate lib "KERNEL32.DLL" alias "HeapCreate" (byval long,byval long,byval long) as long
    declare function HeapDestroy lib "KERNEL32.DLL" alias "HeapDestroy" (byval long) as long
    
    
    function pbmain as long
        local Heap as long
        local Memory as long
        
        
        rem create 20mb heap
        Heap = HeapCreate(%HEAP_ZERO_MEMORY,20000000,0)
        msgbox str$(Heap)
        
        
        rem Task manager memory usage increases by 20mb
        
        
        rem allocate 5mb from our heap
        Memory = HeapAlloc(Heap,0,5000000)
        msgbox str$(Heap)
        
        
        rem For some reason task manager increases again?
        rem I dont think this should happen because I have
        rem already set allocated the space in the heap
    
    
        rem free memory block
        HeapFree Heap,0,byval Memory
        msgbox "done"
        
        rem ddestroy heap
        HeapDestroy Heap
    end function

    Any help on this would be greatly appreciated!

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

  • #2
    Hi Mark,

    The MS documentation does not have the %HEAP_ZERO_MEMORY value
    associated with HeapCreate. This is a parameter for the HeapAlloc
    function. You only have two choices for HeapCreate:

    %HEAP_GENERATE_EXCEPTIONS, or
    %HEAP_NO_SERIALIZE

    I'm not sure this is your problem, but its worth correcting.

    Jeff

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

    Comment


    • #3
      Mark, here are some additional comments:

      1. I notice you are not specifying any equate for the HeapAlloc function
      Again, I,m not sure how Windows will behave, however MS does not
      describe a NULL parameter as an option.

      2. If you are using Win 95/98 (ME?)


      Windows 95/98: The heap managers are designed for memory blocks smaller than four megabytes.
      If you expect your memory blocks to be larger than one or two megabytes,
      you can avoid significant performance degradation by using the VirtualAlloc or VirtualAllocEx function instead.

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

      Comment

      Working...
      X