I have the following setup:
- A Delphi command line application written in Delphi XE5 and built in Debug 64 bit.
- A C dll written in Microsoft Visual Studio 2013 and built in Release 64 bit.
- The Delphi command line application calls a function in the C dll.
What's unexpected:
- When debugging the Delphi command line application within the Delphi XE5 IDE the C dll function call takes 15 s.
- When launching the same Delphi command line application directly (without IDE, without Debugger) then the C dll function call takes 0.16 s.
Delphi command line application source code:
program DelphiCpplibraryCall;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
Windows;
type
TWork = function(Count : Integer) : Integer; cdecl;
var
Handle : THandle;
Work : TWork;
Result : Integer;
Freq : Int64;
Start : Int64;
Stop : Int64;
begin
try
Handle := LoadLibraryEx('worker.dll', 0, LOAD_WITH_ALTERED_SEARCH_PATH);
Work := GetProcAddress(Handle, 'work');
QueryPerformanceFrequency(Freq);
QueryPerformanceCounter(Start);
Result := Work(500000);
QueryPerformanceCounter(Stop);
Writeln(Format('Result: %d Time: %.6f s', [Result, (Stop-Start) / Freq]));
FreeLibrary(Handle);
Readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
C dll source code:
#include "worker.h"
#include <unordered_map>
class Item
{
public:
Item(const int value = 0) : _value(value) {}
virtual ~Item(void) {}
private:
int _value;
};
int work(int count)
{
typedef std::unordered_map<int, Item> Values;
Values* values = new Values;
int k = 0;
for (size_t i = 0; i < count; i++)
{
(*values)[i] = Item(i);
k++;
}
delete values;
return k;
}
Delphi + C dll soure code: DelphiCpplibraryCall.zip
Runtime comparison:
- First console: When debugging in IDE
- Second console: When launching without IDE
For some reason the the Delphi Debugger seems to slow down the C dll function call a lot which makes debugging nearly impossible.
Has anyone a clue what could case this issue or how to avoid it? Many thanks.
Edit: I can confirm now that the described behavior is not restricted to the Delphi IDE and Debugger at all. This problem also happens if I:
- I build the C dll in Microsoft Visual Studio 2013 in Release.
- And build and debug the command line executable that calls the C dll in Visual Studio 2013.
This means that the C dll release build function execution time changes depending on whether there is a debugger attached or not.
I can also confirm that it is the deletion of the unordered_map (delete values;
) that takes that long as soon as there is a debugger present.