PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

GlobalFree

Hi Thierry,

I've been browsing some examples to use OleLoadPicture on desktops and found something.

For example, in the examples:
http://www.codeproject.com/bitmap/render.asp?print=true
http://www.codetools.com/bitmap/load_jp ... ource..asp

... and several extras, a global memory area is allocated using GlobalAlloc, locked, a bitmap is copied while it's locked, and it's then unlocked and used as a stream. From what I can observe the memory area is never freed.

So I checked PocketFrog and you also don't execute GlobalFree(). It seems that in all samples I find on the web, all use GlobalLock/Unlock etc, but none of them ever free the memory.

My questions are:
(1) Why is never GlobalFree executed?
(2) Not related, but interesting anyways. :) If you call GetDC(NULL) to get the DC for the entire display (like the examples & PocketFrog do), is it not necessary to release it afterwards with ReleaseDC(NULL, hDC) like it is with all other DCs? I could not find anything on this in the help file as well.

If it is a coding error, and GlobalFree should indeed be called, then we probably have lots of applications out there with heavy memory leaks...

!Updated: I just saw on the codetools forum that it does indeed cause memory leaks to call GetDC(NULL) and never release it. So here's one anyways...

/Johan
Right from the SDK documentation:

Code:

WINOLEAPI CreateStreamOnHGlobal(
HGLOBAL hGlobal, //Memory handle for the stream object
BOOL fDeleteOnRelease, //Whether to free memory when the
// object is released
LPSTREAM *ppstm //Address of output variable that
// receives the IStream interface pointer
);

In PocketFrog:

Code:

HRESULT hr = ::CreateStreamOnHGlobal( hGlobal, TRUE, &stream; );

If you don't see it yet, the second parameter is a boolean used to link the lifetime of the memory handle (hGlobal) to the stream.


Concerning the DC: of course you have to release it... Don't let anyone tell you otherwise. I think that this might be true on Win9x, but it is definitely not right on NT systems. Better release it when you are finished with it.
Aha. I never thought it could be related to the stream object. I never liked this OLE stuff anyways.

But the examples on the web (codetools esp) are no less incorrect. For example, if a GlobalLock or the CreateStreamOnHGlobal would fail, most of them just return with an errorcode, where they should call GlobalFree before returning (perhaps more of an issue on handhelds).

I got the IMGDECMP stuff working on CE as well. Basically I managed to get DecompressImageIndirect (using the VOImage class) to hang if I passed bitmaps to it. I wonder who's idea it was to strip the bitmap file header from resources to save memory.

Thanks,
Johan

Um. Regarding the DC. The reason why I asked was from the following rows in image.cpp (from PocketFrog), which are also seen in several other places on the web:

Code:
int width = ::MulDiv( hmWidth, GetDeviceCaps( ::GetDC(0), LOGPIXELSX ), HIMETRIC_INCH );
int height = ::MulDiv( hmHeight, GetDeviceCaps( ::GetDC(0), LOGPIXELSY ), HIMETRIC_INCH );
Nobody decided to strip the header from the BMP to "save memory"...

The memory bitmap format was defined and then they needed some kind of header to save them to files.

Regarding GetDC(0), I find this disturbing =) I will fix this... Of course the DC should be release with "ReleaseDC(0,hdc)".

I guess I (and other people) get away with this because the DC get destroyed when the process exits. Multiple calls to GetDC(0) probably always return the same DC using an internal reference count. It could crash after loading a few thousands images =)
An update: just found out that "class device contexts" don't need to be released. Is it possible that a display DC is a "class DC"?
Hi!

It sounded right, but when I searched for "class device context" on Google I found this:

CDC stands for "Class: Device Context".

Um. I guess they referred to the destructor in CDC...
I am not talking about class CDC... If you read the SDK doc about "Display Device Contexts" they say there is three types of display DC: Class DCs, Common DCs and Private DCs. This has nothing to do with MFC.

Private DC are used when you specify the flag CS_OWNDC in RegisterWindowClass() and Common DCs are used when you do GetDC(hwnd)... My guess is that the last type, the class DCs, are used to access devices (ex: GetDC(0)).
I guess not... Here is what I found:


Class device contexts are supported strictly for compatibility with 16-bit versions of Windows. When writing a Win32-based application, avoid using the class device context; use a private device context instead.

I will simply make sure I release the screen DC... Problem solved.

Thanks for pointing it out!