none
Using WdfCollection to implement a Stack of WDFSTRINGs; Memory management question

    Question

  • Hello,

    I've implemented a stack using a WdfCollection of WDFSTRINGs.  In our kernel mode driver, we dynamically allocate space for a UNICODE_STRING ("US") and dynamically allocate the Buffer within it.  When I call WdfCreateString(), I pass in our US and receive a handle to the new WDFSTRING object, which is added to the end of the collection.  The documentation is rather vague about how it goes about creating the new WDFSTRING...  "The framework copies the string to the new framework string object."  Does this mean that the WdfCollection duplicates my original UNICODE_STRING and I can dispose of the original objects memory (for the US and its Buffer)?  Later, when I use WdfStringGetUnicodeString() to recover the US from the end of the collection (aka, the top of the stack), the documentation says that same thing... 

    So, I am wondering about memory management in this scenario...  When I have added my WDFSTRING object to the WdfCollection, is the original US disposable or does the collection still refer to the original objects memory resources?   When I retrieve the WDFSTRING and extract the US from it, is it using dynamically allocated memory from the WdfCollection? I suspect that the memory that I allocated is what is used throughout this process, but I am hoping that someone can verify/clarify things for me.

    Thank you for your help!

    Mike

    Tuesday, January 04, 2011 8:58 AM

Answers

  • the WDFSTRING makes  a deep copy of the PUNICODE_STRING that is passed into WdfStringCreate. this means that as soon as you have successfully created the WDFSTRING, your UNICODE_STRING can be freed or go out of scope.  the WDFCOLLECTION just adds an object reference to the WDFSTRING, it does not know about it being a string at all (it is treated as a WDF object handle without a type). that reference keeps the WDFSTRING valid as long as it is in the collection.  the PUNICODE_STRING returned by WdfStringGetUnicodeString is the WDFSTRING's internal copy of the UNICODE_STRING. you must treat it as read only and it is valid for as long as the WDFSTRING is valid
    d -- This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Jenstepper Wednesday, January 05, 2011 7:35 PM
    Tuesday, January 04, 2011 7:34 PM

All replies

  • the WDFSTRING makes  a deep copy of the PUNICODE_STRING that is passed into WdfStringCreate. this means that as soon as you have successfully created the WDFSTRING, your UNICODE_STRING can be freed or go out of scope.  the WDFCOLLECTION just adds an object reference to the WDFSTRING, it does not know about it being a string at all (it is treated as a WDF object handle without a type). that reference keeps the WDFSTRING valid as long as it is in the collection.  the PUNICODE_STRING returned by WdfStringGetUnicodeString is the WDFSTRING's internal copy of the UNICODE_STRING. you must treat it as read only and it is valid for as long as the WDFSTRING is valid
    d -- This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Jenstepper Wednesday, January 05, 2011 7:35 PM
    Tuesday, January 04, 2011 7:34 PM
  • Thank you very much for your response and for being a patient teacher! :)  Now, I completely understand what I need to do to ensure that we won't have any memory leaks in our driver.  Thank you again for your kind assistance, Doron!

    Mike

    Tuesday, January 04, 2011 8:54 PM