Original Post
Hi,
I wrote a piece of code that follows the following rules:
The question is: when I destroy the window by calling DestroyWindow(), SomeHandle is left untouched (well - it's still pointing to something), but Unregister(), nevertheless, fails to find a corresponding handle in the std::vector Windows. Is the entry in the vector removed or does it simply mutate into something I can't identify anymore (a memory leak, in this case)? And the same question the other way around: since...
I can't understand (with my knowledge) what the #define precisely does in the above code sample, or why it is written the way it is written, I presume HWND is a pointer (from NEAR*), and therefore by calling Windows.erase() on it, removes any reference to the window object itself, thus creating a hefty memory leak. Am I right? Furthermore, if the HWND is erase()'d (points to NULL?) I can't call DestroyWindow() for it anymore.
The point of the question: do I need to erase() the handle in the vector after I've called DestroyWindow() (which I can't do), or is it removed automatically (can't imagine why)? Or , could someone please straighten out what I just wrote...
Thanks,
Crispy
Edit: readability issues
[edited by - crispy on July 31, 2002 1:23:24 PM]
std::vector<HWND>Windows;
void Register(HWND pHandle) { Windows.push_back(pHandle); }
bool Unregister(HWND pHandle)
{
for(unsigned i = 0; i < Windows.size(); i++)
if(Windows[i] == pHandle)
{
Windows.erase(Windows.begin() + i);
return true;
}
return false;
}
void Func()
{
HWND SomeHandle = CreateWindowEx(...);
Register(SomeHandle);
//blah-blah. blaaaaaah...
//WinAPI call - destroys the window :)
DestroyWindow(SomeHandle);
if(!Unregister(SomeHandle))
cout << "failure" << endl;
else
cout << "success" << endl;
}
//from: compiler docs
#define DECLARE_HANDLE(name) struct name##__ { int unused; };
typedef const struct name##__ NEAR* name
//from: windows.h
DECLARE_HANDLE(HWND)