Results 1 to 6 of 6

Thread: Do you need to use GlobalUnlock before using GlobalFree?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Do you need to use GlobalUnlock before using GlobalFree?

    In the MSDN page describing ICImageCompress I see this mentioned:
    To obtain the format information from the BITMAPINFOHEADER structure, use the GlobalLock function to lock the data. Use the GlobalFree function to free the DIB when you are finished.
    Nowhere does it mention the use of GlobalUnlock. Is it required to use GlobalUnlock before using GlobalFree? Or is this not required?

  2. #2
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Do you need to use GlobalUnlock before using GlobalFree?

    No. GlobalFree will free the memory:
    The GlobalFree function will free a locked memory object.
    source: http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    you could also verify this by checking the return value of GlobalFree which should indicate if there was a problem freeing the memory block.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: Do you need to use GlobalUnlock before using GlobalFree?

    Quote Originally Posted by digitalShaman View Post
    No. GlobalFree will free the memory:
    The GlobalFree function will free a locked memory object.
    source: http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    you could also verify this by checking the return value of GlobalFree which should indicate if there was a problem freeing the memory block.

    But when using it like that, does it properly decrement the lock-counter (for number of locks on that memory block), or does it leave the lock counter stuck with a now invalid value?

  4. #4
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Do you need to use GlobalUnlock before using GlobalFree?

    GlobalFree frees the memory block, so there wont be any lock counter anymore and all handles to the block become invalid no matter if the block was locked or not. so, yes, you can assume that globalfree does set the lock count to zero.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: Do you need to use GlobalUnlock before using GlobalFree?

    Quote Originally Posted by digitalShaman View Post
    GlobalFree frees the memory block, so there wont be any lock counter anymore and all handles to the block become invalid no matter if the block was locked or not. so, yes, you can assume that globalfree does set the lock count to zero.
    What if it is freed (so that the system thinks it's no longer being used), but left in a locked state, which prevents other applications from using it? If I understand the mechanisms here correctly, if an application happens to allocate a block of memory which has a lock still on it because it's been freed (but not unlocked) then when the next program tries to use that block that it now allocated, when it calls "lock" on it to lock it, the system will say, "Sorry but another program already has locked that section of memory, so you can't access it", needless to say, that will result in crashes. This situation could only be resolved by rebooting the computer. This is how I understand the situation, so whenever something allocates memory dynamically (requiring both the use of GlobalAllocate and GlobalLock, or if another function allocates the memory, such as this ICImageCompress function, I NEVER call GlobalFree (even if Microsoft says I can, because they have been known to be mistaken about other things in the past, as even though they theoretically understand the workings of their own OS better than anyone else, they don't all the time) until AFTER I have called GlobalUnlock. I'm not sure that this is required, but it makes sure that there is no possibility of a system crash.

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Do you need to use GlobalUnlock before using GlobalFree?

    When allocated memory is free, it no longer exists so how can it still have a lock on it. The memory has been freed and its metadata rendered invalid so the locks become irrelevant.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

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



Click Here to Expand Forum to Full Width