4

I have added the following functionality to an existing native C++ application:

  1. A 3rd party DLL is loaded via LoadLibraryA(...)
  2. A function pointer is obtained from the DLL via GetProcAddress(...)
  3. The function is called

While this basically works (the function is called alight and produces the expected output) there is one major problem: the function call (in 3.) is extremely slow. It takes over 300ms (!) to execute, while it should be less than 1 ms.

So I created a new, blank C++ application and copied the code over. So steps 1 to 3 are executed exactly the same way as in the other application. The function does the same thing and produces the same results. But the execution is much faster: less than 1 ms, as expected.

Some more observations I made:

  • Both results are 100% reproducible.
  • Both applications seem to have the same compiler/linker settings.
  • Threading/performance issues don't seem to be the problem, since in both applications the code is executed on the main thread during very low CPU usage (not much else is going on). Also the results are reproducibly, which rules out these kind of timing issues.
  • Every call into the DLL takes at least 300ms in my first application, while it's fast in the other. Thus the behavior is not limited to a single function.
  • It makes no difference if I use a debug or release build.
  • Both applications and the 3rd party DLL are 32bit.

So now I'm looking for an idea what could be causing both applications to behave so differently when doing exactly the same thing.

Boris
  • 8,551
  • 25
  • 67
  • 120
  • Maybe stupid: Is there a FreeLibrary ? –  Apr 01 '16 at 09:25
  • Yes, after calling the function I use FreeLibrary to unload the DLL. – Boris Apr 01 '16 at 09:29
  • Are there any static initialisations or variables in either the function been called or globally in the dll? What dependencies does the dll have and how long are they taking to load/initialise? – Niall Apr 01 '16 at 09:35
  • Since the DLL is 3rd party, I cannot tell what's going on inside. But should that really matter? Since both my applications use the DLL in the same way I would expect the same behavior. – Boris Apr 01 '16 at 09:37
  • Maybe, it is possible that there is some form of initialisation that is required globally (on another thread) and is run as soon as the dll loads; if the method is dependent on the result, it may have to wait for it. If the library is loaded when the exe is loaded, you don't notice this since the result is immediately available to the function being called. – Niall Apr 01 '16 at 09:47
  • I note you say "every call into the dll" - is that on the first time each function is executed or is it on subsequent calls to the methods as well? – Niall Apr 01 '16 at 10:07
  • The delay is the same for the first call of any function as well as subsequent calls. – Boris Apr 01 '16 at 10:42

0 Answers0