KEVENT storage question

Hi,
I have a KEVENT which may or may not have another thread waiting on it. Can I free the storage memory of KEVENT after I invoke KeSetEvent? Is there a risk of memory corruption?
If there will be a waiting thread - it will not touch the event after the wait was satisfied. Intuition suggests me that it is a terrible idea. But I have this other thought that maybe KeSetEvent just wakes the waiting thread by pulling some levers in the thread dispatcher.

Background:
Currently the KEVENT is part of the WDFOBJECT’s context, and the event itself is triggered during WdfDestroyEvt. I can allocate the event somewhere outside the context but that would be a bit “unwieldy” for the design I have.

Okay, I figured it out myself: Nope.

Generally when you want to dispose of an object that contains let’s say
several pointers like:

_MY_STRUCT
{
PKEVENT sync;
PTHREAD someReferencedThread;
PCONTEXT someCountedContext
};

When your component does not need this STRUCT anymore, because it needs to
unload/detach/destroy etc… your code should work in a manner that all of
the pointers inside the structure should be free-able/dereference-able
during the struct release/destroy. So in your case the EvtDestroy should
either be smart enough to know that in that scenario there are no threads
waiting ( or make it so that there can be no threads that wait ) on the
event or wait until there are no threads waiting on the event in all other
cases you are asking for trouble.

Gabriel.

On Tue, Oct 6, 2015 at 5:17 PM, wrote:

> Okay, I figured it out myself: Nope.
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>


Bercea. G.

Why not free the memory from the waiter instead?

wrote in message news:xxxxx@ntdev…
> Hi,
> I have a KEVENT which may or may not have another thread waiting on it. Can I free the storage memory of KEVENT after I invoke KeSetEvent? Is there a risk of memory corruption?
> If there will be a waiting thread - it will not touch the event after the wait was satisfied. Intuition suggests me that it is a terrible idea. But I have this other thought that maybe KeSetEvent just wakes the waiting thread by pulling some levers in the thread dispatcher.
>
> Background:
> Currently the KEVENT is part of the WDFOBJECT’s context, and the event itself is triggered during WdfDestroyEvt. I can allocate the event somewhere outside the context but that would be a bit “unwieldy” for the design I have.
>
>

> Why not free the memory from the waiter instead?
Because it is uncertain whether the waiter exists or doesn’t.

So in your case the EvtDestroy should either be smart enough to know that in that scenario there are > no threads waiting ( or make it so that there can be no threads that wait ) on the
event or wait until there are no threads waiting on the event in all other
cases you are asking for trouble.
That’s what I’ll settle for.