WdfCollectionAdd and memory objects

  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?

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, timr@probo.com
Providenza & Boekelheide, Inc.

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

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);

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

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?

By default if you don

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