-
Aug 18th, 2014, 08:44 PM
#1
Thread Starter
Frenzied Member
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?
-
Aug 20th, 2014, 09:45 AM
#2
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.
-
Aug 20th, 2014, 01:23 PM
#3
Thread Starter
Frenzied Member
Re: Do you need to use GlobalUnlock before using GlobalFree?
Originally Posted by digitalShaman
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?
-
Aug 21st, 2014, 01:56 AM
#4
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.
-
Aug 27th, 2014, 01:17 AM
#5
Thread Starter
Frenzied Member
Re: Do you need to use GlobalUnlock before using GlobalFree?
Originally Posted by digitalShaman
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.
-
Aug 27th, 2014, 03:01 AM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|