[wdmaudiodev] Re: AVStream driver & globally allocated memory

  • From: Tim Roberts <timr@xxxxxxxxx>
  • To: "wdmaudiodev@xxxxxxxxxxxxx" <wdmaudiodev@xxxxxxxxxxxxx>
  • Date: Mon, 20 Jun 2016 10:47:30 -0700

Alexander Ivash wrote:

What are the right places to allocate/de-allocate memory for global
variables in AVStream drivers?

You shouldn't have any global variables, in the sense of variables
declared at global scope.  That's just a general programming rule.


At the moment I'm allocating memory inside DriverEntry right after
KsInitializeDriver call and de-allocate inside the hook for
'DriverUnload', which I installed also after KsInitializeDriver call.

Is it correct way of working with global variables? I'm asking because
from time to time I'm getting BSODs with the following error:

In an AVStream driver, there are three layers in the hierarchy:
    - Device
    - Filter
    - Pin

Each instance of your driver has a device.  Each device can have
multiple filter instances.  Each filter can have multiple pin
instances.  Each and every one of those things has a context structure. 
If you follow the AVStream driver samples, you have classes like
CCaptureDevice and CVideoPin.

Your "global" data should be contained in the appropriate context
structure.  Data that applies to all filters for the current device
instance should be member variables in CCaptureDevice.  That way, YOU
don't have to worry about allocation at all.  The memory gets managed by
AVStream and added to the memory "bag", so it can get automatically
deallocated when the device is destroyed.

Now, like all rules, there are exceptions.  It's not impossible that you
might need to have a piece of data that is global across all devices,
although it's quite rare.  However, unless you're handing that memory to
someone else, no one should be deleting your data. There is no automatic
cleanup for a kernel driver, unlike a user-mode process.  DriverEntry
gets called once, and DriverUnload gets called once.  That's a
contract.  You'd added debug prints to make sure that statement is
holding true?

-- 
Tim Roberts, timr@xxxxxxxxx
Providenza & Boekelheide, Inc.

******************

WDMAUDIODEV addresses:
Post message: mailto:wdmaudiodev@xxxxxxxxxxxxx
Subscribe:    mailto:wdmaudiodev-request@xxxxxxxxxxxxx?subject=subscribe
Unsubscribe:  mailto:wdmaudiodev-request@xxxxxxxxxxxxx?subject=unsubscribe
Moderator:    mailto:wdmaudiodev-moderators@xxxxxxxxxxxxx

URL to WDMAUDIODEV page:
http://www.wdmaudiodev.com/

Other related posts: