Destroying an RTL Generic Table and enumerating with RestartKey ?

I have a driver that uses RTL Generic Table functions to store data,
everything works fine, but I’m concerned about resource clean up for
when I get called to remove my device.

Whilst there is a table initialisation function, there is no
equivalent table destroy function, such as you see with LookAsideLists.

I already have an Ioctl call which cleans out all the elements in the
table and seems to be fine.

while (TRUE)
{
pHit = RtlGetElementGenericTable (&pDevExt->Table, 0);

if (pHit == NULL)
break;

RtlDeleteElementGenericTable (&pDevExt->Table, pHit);
}

Is it enough in my RemoveDevice function to call the above code to be
sure that all the table resources have been freed ? I’d feel much
happier if there was a Generic Table delete function.

On a related note, I’ve had some struggles with
RtlEnumerateGenericTableWithoutSplaying() and the RestartKey
parameter. What I was trying to do was to restart the table
enumeration at a specific point which was not necessarily the same
place as where the previous enumeration finished.

I tried various ways of trying to create a RestartKey by using the
successful result of RtlLookupElementGenericTable() as the
RestartKey, but no matter the variations I tried it would
crash. Does anyone have experience of generating their own RestartKey ?

regards,

Mark.

>

I have a driver that uses RTL Generic Table functions to store data,
everything works fine, but I’m concerned about resource clean up for
when I get called to remove my device.

You should do something like this:
I a while loop:
while (NULL != (pHit = RtlGetElementGenericTable(&pDevExt->Table, 0)))

Free all memory that was allocated at init using ExFreePool () or
ExFreePoolWithTag() - depends on the way of initialization - including:

  • memory allocated for structures of which pointers are stored in the table
    element structure
  • memory allocated for the table element structure itself

If the function RtlNumberGenericTableElements(&pDevExt->Table) does not
return 0 after the while loop there is/was something and you will have a
memory leak.

Urban