How can I diagnose why my FreeLibrary isn’t freeing the library?


A customer had some code that called Free­Library, and they expected the library to be freed, but it wasn't happening. They suspect that there was an unmatched call to Load­Library and was wondering if there were any diagnostic tools that would help them find it.

Application Verifier to the rescue.

Application Verifier maintains a history of module loads and unloads, and you can use the !avrf -dlls debugger extension to view that log. For each module that is loaded and unloaded, it records a stack trace of the Load­Library and Free­Library.

The customer reported back that this debugger extension found the problem. Their code used the HKEY_PERFORMANCE_DATA registry key to read some performance counters, and opening the performance counter was triggering an additional DLL load.

Comments (7)
  1. Brian_EE says:

    Two stories in a row where the customer came back and followed up the case with a “we found the problem”. Most times, Raymond, you will say “we never heard back from the customer” in response to a comment asking about it.

    Is it more typical to hear back from the customer or not?

    1. Usually we don’t hear back. I’m giving the benefit of the doubt and assuming that the follow-up was filtered out by the customer liaison (don’t need to bother the product team) rather than because the customer never followed up.

      1. Joshua says:

        “opening the performance counter was triggering an additional DLL load” sounds like an OS bug but I don’t think that’s Raymond’s intention.

        1. Um, no, that’s the design. You open a performance counter. The system needs to load the DLL that provides the performance counter. Otherwise where will it get the performance counter from? Just make up values at random?

          1. kantos says:

            Joking: Just load the MissingPerfCounterShim that always returns -1 (e.g. off-scale low or counter missing)

  2. Neil says:

    But that doesn’t explain why they particularly needed the library to be freed at that time in the first place…

    1. They didn’t need it to be freed, but they were expecting it to be freed since they couldn’t think of a reason why it should still be loaded. But now they understand that their expectations were incorrect.

Comments are closed.

Skip to main content