win32 >> SetWindowLong with GWL_USERDATA

by dotNeter » Thu, 03 Aug 2006 14:31:49 GMT

My question is, if the user data is a pointer to a block of memory,
when to free it?

Like,
char* ptr = (char*)malloc(sizeof(char) * 10);
// ...
ret = SetWindowLong(aWndHwnd, GWL_USERDATA, ptr); // when to free ptr?

You know, after destroy the window, the window handler isn't valid any
more. So it looks like I must manually free it before destroy the
window, right?

But if I keep the ptr somewhere, and I just free it till an intentional
point. I think it's still okay, right?

thx.



win32 >> SetWindowLong with GWL_USERDATA

by Jakob Bieling » Thu, 03 Aug 2006 15:36:59 GMT






Why not free the memory in WM_DESTROY?


Yes, as long as you free the memory somewhere, it is ok, as far as
memory leaks are concernced. But it will make your code less easier to
understand. Allocating the memory in WM_CREATE and freeing it in
WM_DESTROY is pretty intuitive.

hth
--
jb

(reply address in rot13, unscramble first)





win32 >> SetWindowLong with GWL_USERDATA

by dotNeter » Thu, 03 Aug 2006 15:53:59 GMT

Thx for your hints.

Due to my specific case, I have to do so, I mean, centralize the
"freeing" operation.
Still, your reply inspired me some additional idea.

thx.






Similar Threads

1. FindWindow versus GWL_USERDATA for identifying applications

2. SetWindowLong in windows mobile 6.

hi all,
I am trying to use SetWindowLong to get other windows messages with
GWL_WNDPROC index as an input.
the problem is the following .

when i try to replace my own (in the current process) wndproc
function
- all works fine - all messages pass throught the "hook" function
first.
But, when i try to do so on another process/window - when i pass the
function that is implemented in my own EXE instead of the original
function - the "hooked" window does not respond and i don't recieve
any messages.


what could the problem be?

Thanks,
paul.

3. NewBie: SetWindowLong - Windows CE

4. Using SetWindowLong in windows mobile 6.

hi all,
I am trying to use SetWindowLong to get other windows messages with
GWL_WNDPROC index as an input.
the problem is the following .

when i try to replace my own (in the current process) wndproc function
- all works fine - all messages pass throught the "hook" function
first.
But, when i try to do so on another process/window - when i pass the
function that is implemented in my own EXE instead of the original
function - the "hooked" window does not respond and i don't recieve
any messages.

what could the problem be?

5. Why will SetWindowLong(hWnd,GWL_STYLE, xxx) trigger WM_SIZE ev

6. warnings with SetWindowLong

I am using SetWindowLong to do some subclassing and I have a few warnings 
which I would like to get rid of.  Here is the lines of code with the 
warnings:

WNDPROC mOldWndProc = (WNDPROC)SetWindowLong(mHwnd, GWL_WNDPROC, 
(LONG)SkinWndProc);
LONG OurWnd = (WNDPROC)SetWindowLong(mHwnd, GWL_WNDPROC, (LONG)mOldWndProc);

and here are the warnings:

Skinner.cpp(335) : warning C4311: 'type cast' : pointer truncation from 
'LRESULT (__stdcall *)(HWND,UINT,WPARAM,LPARAM)' to 'LONG'
Skinner.cpp(335) : warning C4312: 'type cast' : conversion from 'LONG' to 
'WNDPROC' of greater size
Skinner.cpp(369) : warning C4311: 'type cast' : pointer truncation from 
'WNDPROC' to 'LONG'

Can anybody tell me how I can get rid of these?
Thanks,
Allan 


7. Using SetWindowLong()

8. SetWindowLong / GetWindowLong

I've just discovered those functions and I have no problem to make them work
but, at compile time I get warnings :

warning C4312: 'type cast' : conversion from 'LONG' to 'TOCtrl *' of greater
size
warning C4311: 'type cast' : pointer truncation from 'TOCtrl *' to 'LONG'

My application runs fine but I don't like to have warnings standing there
all the time, I like clean compiling =)

I checked and sizeof (LONG) = sizeof ( TOCtrl*) = 4

So I don't know why it's telling me that.

I use C cast btw.