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

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

question about reference count on WdfCollection

MakMak Member Posts: 50

If for example I create a list in the following way:

stringAttributes.ParentObject = list_1;
status = WdfRegistryQueryMultiString(ServiceRegKey, ValueName, &stringAttributes, list_1);

and then put the item in a second list too, the reference count of every wdfstring in the list is 2.
In the the second list e.g. list_2 the items has now the parents of list_1.
How do I correctly free the items from list_2?
Is it correct if I do nothing or must I call wdfCollectionRemove on each item on list_2 to clean up correctly?
What happens if I call WdfObjectDelete(list_2) internally?
I know that I could give the items in list_1 no parent object but it is nice if the list delete the items automatically.
What happens if I call WdfObjectDelete(list_1) before WdfObjectDelete(list_2)?

Comments

  • Peter_Viscarola_(OSR)Peter_Viscarola_(OSR) Administrator Posts: 7,284

    Here’s an idea: Why not make your code as easy to read, understand, and maintain as possible? If you have these questions, won’t the next person who maintains your code also have these questions?

    So... don’t be “cute” by having the list parent objects that are on the list and try to get them auto-deleted. Just remove the items, delete them, then delete the lists.

    Strive for code clarity. Ease of understanding. That is simplicity, which is the essence of good engineering.

    Peter

    Peter Viscarola
    OSR
    @OSRDrivers

  • Tim_RobertsTim_Roberts Member - All Emails Posts: 13,007
    via Email
    Mak wrote:
    > If for example I create a list in the following way:
    >
    > stringAttributes.ParentObject = list_1;
    > status = WdfRegistryQueryMultiString(ServiceRegKey, ValueName, &stringAttributes, list_1);
    >
    > and then put the item in a second list too, the reference count of every wdfstring in the list is 2.
    >
    > In the the second list e.g. list_2 the items has now the parents of list_1.
    >
    > How do I correctly free the items from list_2?

    It's possible that I have missed something, but when I look through the
    source code, I don't see that a WDFCOLLECTION cares at all about object
    parentage.  When an object is added, its reference count is bumped. 
    When an object is removed, its reference count is reduced, and if it
    goes to 0 it is deleted. When a collection is deleted, all of its
    objects get their reference counts reduced.

    That's certainly the most intuitive implementation.

    > Is it correct if I do nothing or must I call wdfCollectionRemove on each item on list_2 to clean up correctly?

    You must remove it from the collection.

    > What happens if I call WdfObjectDelete(list_2) internally?

    Then all those strings will have their reference counts set back to 1.

    > I know that I could give the items in list_1 no parent object but it is nice if the list delete the items automatically.
    >
    > What happens if I call WdfObjectDelete(list_1) before WdfObjectDelete(list_2)?

    I believe it all works the way you would expect.

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

  • MakMak Member Posts: 50

    Peter: thanks for your suggestion. I think about it.
    Tim: thank you too.

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
Developing Minifilters 29 July 2019 OSR Seminar Space
Writing WDF Drivers 23 Sept 2019 OSR Seminar Space
Kernel Debugging 21 Oct 2019 OSR Seminar Space
Internals & Software Drivers 18 Nov 2019 Dulles, VA