Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results

Before Posting...
Please check out the Community Guidelines in the Announcements and Administration Category.

WdfCollectionAdd and memory objects

MakMak Member Posts: 50
  1. We have a lot of collections in our driver. Is it possible to get information about undeleted WdfMemory Objects and WdfStrings?
  2. If I call WdfStringCreate and then WdfCollectionAdd with this string, is it necessary to delete now the String Object? Has it a reference count of 2?

Comments

  • Tim_RobertsTim_Roberts Member - All Emails Posts: 12,914
    via Email
    On Nov 2, 2018, at 11:22 PM, Mak wrote:
    >
    > * We have a lot of collections in our driver. Is it possible to get information about undeleted WdfMemory Objects and WdfStrings?
    > * If I call WdfStringCreate and then WdfCollectionAdd with this string, is it necessary to delete now the String Object? Has it a reference count of 2?

    WDF objects are not reference-counted. It is simple ownership -- each object is owned by some other object. When the parent is deleted, its child objects are deleted. By default, strings are owned by the device. If you no longer need the string after you remove it from the collection, then yes, you need to delete it.

    Tim Roberts, [email protected]
    Providenza & Boekelheide, Inc.

    Tim Roberts, [email protected]
    Providenza & Boekelheide, Inc.

  • MakMak Member Posts: 50

    If I add a WDFMemory object which I have created in two collections, is wdfRemoveItem in both collections enough two delete them?

  • MakMak Member Posts: 50

    I found this on the web and asked myself if this is correct?

       WDFSTRING tmp_wdf_string;
      WdfStringCreate(&wdf_load_group_name, WDF_NO_OBJECT_ATTRIBUTES, &tmp_wdf_string);
      WdfCollectionAdd(new_load_order, tmp_wdf_string);
      WdfObjectDelete(tmp_wdf_string);
    
  • Peter_Viscarola_(OSR)Peter_Viscarola_(OSR) Administrator Posts: 7,138

    WDF objects are not reference-counted

    Yes, they absolutely are. See, for example, WdfObjectReference.

    I found this on the web and asked myself if this is correct

    It’s NOT correct... and, worse, it’s hiddenly stupid. The object is placed in the Collection, where the ref count is incremented. He then calls WdfObjectDelete.. which decks the ref count and marks the object for deletion as soon as the ref count goes to zero. So.... as soon as the object is removed from the collection, it gets deleted.

    The kernel mode driver samples in the web almost universally suck. Beware!

    Peter

    Peter Viscarola
    OSR
    @OSRDrivers

  • MakMak Member Posts: 50

    Thanks, this is also what I read in the wdfCollectionAdd remarks:
    ... When WdfCollectionAdd adds an object to a collection, it increments the object's reference count.
    If I have two wdfcollections and add one object to both of them and then I only call wdfCollectionRemove on both collections, Is the object correctly dereferenced and deleted now?

  • Doron_HolanDoron_Holan Member - All Emails Posts: 10,428
    via Email
    By default if you don
    d
  • Peter_Viscarola_(OSR)Peter_Viscarola_(OSR) Administrator Posts: 7,138

    Is the object correctly dereferenced and deleted now?

    Well... it's properly dereferenced -- And given that the object is owned by some OTHER object, it will never leak, because when the Parent Object goes away, the objects it owns will go away. But if you're constantly creating, inserting, and removing (and not reusing) these objects, you should hasten their deletion when you're done with them by calling WdfObjectDelete... again, only after you have no more use of the object

    Peter

    Peter Viscarola
    OSR
    @OSRDrivers

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Upcoming OSR Seminars
Writing WDF Drivers 25 Feb 2019 OSR Seminar Space
Developing Minifilters 8 April 2019 OSR Seminar Space