Ok, im kinda in a hurry, so im posting the question, as well as currently searching the MSDN...

Q. Say one window allocates some memory (any type: gloabal, virtual, Heap) for an array of data points. Then it passes a pointer and its size to this to another window to use.

In the second window, is there a way of checking the Validity of the pointer before its used by the second window? I mean, since they are separate windows, its possible for the first window to "free" the memory, and the second window would have no clue (and possibly cause a GPF).

Thanx in advance for this!
:stupid:
NaN
Posted on 2002-05-29 14:31:22 by NaN
mm I don't know if this may help or not.

while coding in C . and I have pointer.
if I don't use this pointer anymore I assign it NULL. then delete the object.

so why not the first windows assign any pointer NULL after it delete the object ( free the memory ) ?

and the second window just check if the pointer == NULL .

Hope this help.
Posted on 2002-05-29 14:44:00 by Sa6ry
Because the copy of the pointer, sent before hand will still point to what is then invalid memory. This is why i need to check, cause they are in separate threads...

However, i think i found the solution, still need to test it tho. If anyone agrees or disagrees, i would like to hear your thoughts still ;)
The IsBadReadPtr function verifies that the calling process has read access to the specified range of memory.

BOOL IsBadReadPtr(

CONST VOID * lp, // address of memory block
UINT ucb // size of block
);
Parameters

lp

Points to the first byte of the memory block.

ucb

Specifies the size, in bytes, of the memory block. If this parameter is zero, the return value is FALSE.

Return Value

If the calling process has read access to all bytes in the specified memory range, the return value is FALSE; otherwise, it is TRUE. To get extended error information, call GetLastError.


:alright:
NaN
Posted on 2002-05-29 14:56:40 by NaN
You could do what Sa6ry suggests, but it's a little more complicated than that - you'd need an in-use flag, and use the CMPXCHG instruction to change that flag - that way it would work in a multi-processor environment. That way you don't free the memory if it's in use, and don't use it if it's NULL.

If you use IsBadReadPtr, how do you know it is part of the respective memory buffer? You could free the memory and then allocate some more - making that address a good read pointer again?

I'm sure there is a windows proper way - I don't know what it is.
Posted on 2002-05-29 15:02:13 by bitRAKE
I think it also can be done this way .

you may use pointer to pointer.

First Window
A -> B -> ( Object )

Second Window
C-> B -> ( Object )

First Window or Second Window
Delete Object
B == NULL

Second Window or First Window
IF B == NULL invalid pointer.
Posted on 2002-05-29 15:32:12 by Sa6ry
IsBadReadPtr is probably not the best solution, as the memory might
be freed, but the region re-used for other purposes...
using a in-use flag (I'd use a CRITICAL_SECTION) is a good idea,
but where would this flag be stored? Global mem would make it
hard to do truely dynamic stuff. Dynamically allocated mem? Well,
this would also have to be freed... and would again give trouble.
I guess there's a good reason why you normally send messages
around and have to copy any data, instead of passing pointers around :).
Posted on 2002-05-29 15:33:40 by f0dder
The validity of the data presented is not an issue to me... just that the "data" of any sort can be read. This is only to protect against GPF's... If the same location is renewed.. so be it, it wont affect the operating performance.. just the outcomes... with is tollerable ;)

Im still thinking the API will work best at this point, but thanx for your thougts... your free to still give me a convincing argument why its still bad tho ;)

Thanx again everyone!
:alright:
Posted on 2002-05-29 16:25:48 by NaN
By using IsBadReadPtr you don't protect yourself from GPFs. What if the buffer gets freed and the memory is allocated for other things and the reallocated buffer is too small for your operations, then you may read outside the buffer and cause a GPF anyway. The pointer-to-pointer method seems more reliable to me.
Posted on 2002-05-30 00:45:38 by gliptic
about pointer to pointer method
i think it can be modified if we are speacking about two windows.

First Window
A -> ( Object )

Second Window
B -> A -> ( Object )

First Window Delete
Delete Object
A == NULL

Second Window Delete.
Delete Object
A == NULL
B == NULL

First Window Retrieve
A . Object

Second Window Retrieve.
B -> A . Object .
Posted on 2002-05-30 02:20:14 by Sa6ry
Thanx for your thoughts, but even this wont work. I've posted in another thead that the passed pointer 'expires' automatically after the SendMessage API returns. So even if i past a pointer to a pointer, it too will expire...

I've never tried such things, but im guess im experiencing the 'cant share data between processes' problem that i read in Ic's tuts years ago ;)

I will program the work around. The bummer is it wont be real time and require another message send when the data is updated by the first window....

Thanx again.
:alright:
NaN
Posted on 2002-05-30 16:45:42 by NaN