Hello.

Faced such a problem. There is an application using a certain functionality from DLL. The load is carried out dynamically, that is, with a bunch of LoadLibrary -> GetProcAdress . The application functions quite normally until the computer goes into sleep mode and then goes out of it. The DLL library is automatically unloaded from the address space, oddly enough, not immediately after exiting from sleep mode, but in minutes, in 30-40 minutes. As you know, hibernation freezes all processes and, accordingly, the flows of processes before resuming the work (getting out of sleep). So, why, after these same 30-40 minutes, the system detects the DLL loaded into the address space of the application process invalid, useless and automatically unloads from memory? How can this be fixed?

Thank.

  • 3
    The system is not so smart. Once loaded, the library remains loaded until the reference count is reset. In addition to FreeLibrary, some thread CloseHandle can be called. PS Miracles do not happen. It is necessary to carefully and critically examine everything that happens with the handle returned from LoadLibrary - ganouver
  • 2
    In fact, minus incomprehensible questions (even if the author is mistaken) is not good . @Asen, what you describe is interesting (plus). In fact, many users point out the oddities in the behavior of different programs in Windows when getting out of sleep (on a notebook). What is the reason - I do not know. I can assume that the observed @Asen "disappearance" dll from the same opera. Places in the comments as always a little - avp
  • one
    - continuation - This is only an assumption, based on speculative constructions. - Let's say that the system occasionally "loses" some pages. Those. they remain in the process address space tables, but disappear from the list of hosted physical pages and do not fall into the list of employees. (List of code names, implementation may be quite different). Then, when the system goes to sleep, their contents will not be uploaded to the disk. Accordingly, it will not be restored upon awakening, and what will really be unpredictable in RAM. - Once again about the question - requires careful study . - avp
  • 2
    @avp It is hard to believe that the implementation of Hibernate in Microsoft allowed the possibility of an unpredictable state of RAM at the time of recovery from this very Hibernate . The non-deterministic state in this case is 204904590 times more critical than, say, a normal memory leak. - Costantino Rupert
  • one
    @ karmadro4, happy for you. The HP Configurator does these tricks regularly. @ Kitty, I readily believe that an explanation for the inoperability after Hibernate m. to others. By the way, "tricks" in Windows also arise through intensive work, obviously causing a "heavy" swap. - avp


2 answers 2

98% sure that the link count is reset, I fully support @ganouver , this is confirmed by the valid work of the library with static linking.

When the counter is reset, the memory into which the library is displayed cannot always be released, so an illusion appears that everything works correctly (the addresses may be correct).

In sleep mode, apparently there is an optimization of the virtual space of processes, after which all illusions are dispelled.

    • I advise you to try replacing the approach with GetProcAdress + LoadLibrary with the approach with generating a static import library for dll'ки and linking it to your application.

    If in this case the problem disappears, then we can talk about the incomprehensible behavior of the OS regarding loaded dll'ок . Assume also the option of incorrect use of LoadLibrary in principle (check with MSDN ).

    • Static import tried. Everything works correctly, oddly enough! But in this case it will not be very convenient. It is necessary to rewrite a lot of code, because the application is not small, and the exported DLL functionality is connected dynamically. Thank. - AseN
    • I am afraid that in this case it is difficult to say anything without a code fragment. You can check the behavior on different systems, if it persist'ится , then I think the error is exactly in the code. - Costantino Rupert
    • And what could be the mistake, since the system unloads only after a sleep mode and NEVER more? - AseN
    • @Asen And how can you prove that it does not happen EVER more? :) - Costantino Rupert
    • During the entire time that I was developing the application (about 3 months), I regularly tested it, there was no download. This is exactly in this direction you can not watch. - AseN