Andy’s Blog and Tools

Syndicate content
Delphi, C++Builder and other thoughts
Updated: 34 min 42 sec ago

Castalia’s Clipboard history + TRichEdit = IDE deadlock

Mon, 08/15/2016 - 10:26

Eugene Kotlyarov found a problem with Castalia’s clipboard history feature combined with a TRichEdit in a debugged application. If you copy text from the RichEdit to the clipboard the first time, the IDE and the debugged application stop responding and start reacting after a ~30 seconds timeout. After that, copy to clipboard works until you restart the debugged application. Win32 and Win64 have the same problem.

What happens:

  • The debugged application invokes WM_COPY by Ctrl+C/Ctrl+Insert/Context-menu “Copy”.
  • WM_COPY set the RichEdit as clipboard renderer without putting the actual data into the clipboard.
  • Castalia’s TClipboardHistoryForm receives the WM_CLIPBOARDUPDATE message in the main thread and calls GetClipboardData (via Clipboard.AsText).
  • The debugged application’s RichEdit loads a DLL to provide the actual clipboard data for the GetClipboardData call.
  • The debugger receives a LOAD_DLL_DEBUG_EVENT for the DLL.
  • The debugger posts a message to the debugger window in the main thread and goes to sleep with WaitForSingleObject to wait for the debugger window to process the DLL load event.

Now everything waits. GetClipboardData is blocked because the clipboard owner, the debugged application, is trapped in a debugger event. The debugger is blocked because the debugger window doesn’t get the posted message because the blocked GetClipboardData prevents the main thread from processing messages.

Fortunately a Microsoft developer knew about this possible deadlock and put a timeout into GetClipboardData.


A solution that I’ve put into the IDE Fix Pack development version (not available) moves the WM_CLIPBOARDUPDATE message handling into a thread window, so that the call to GetClipboardData doesn’t block the main thread and the debugger can process the DLL load event.

Categories: News, Blogs, and Tips

What’s wrong with virtual methods called through an interface

Tue, 05/31/2016 - 11:20

Calling a virtual method through an interface always was a lot slower than calling a static method through an interface. But why is that? Sure, the virtual method call costs some time, but comparing it with the difference of a normal static and virtual method call shows that the timings diverge too much.

i7-4790 3.6GHz
10,000,000 calls to empty method Instance call Interface call Static method 12 ms 17 ms Virtual method 17 ms 164 ms

Let’s assume we have this declaration:

IMyInterface = interface
procedure Test(A, B: Integer);
  TTest = class(TInterfacedObject, IMyInterface)
procedure Test(A, B: Integer); virtual;

The compiler will generate a helper function for the interface method “Test”. This helper converts the “MyIntf” interface reference in the call “MyIntf.Test()” to the object reference behind the interface and then jumps to the virtual method.

add eax,-$0C   // convert the interface reference to the object reference
push eax       // save the object reference on the stack
mov eax,[eax]  // access the VMT
mov eax,[eax]  // get the “Test” VMT method address
xchg [esp],eax // swap the object ref on the stack with the method address
ret            // do the jump to the method address

This is very slow as you can see in the table above. If you know the “XCHG mem,reg” instruction, then you also know that it has an implicit “CPU LOCK” that slows down the method call a lot. But why is it using the XCHG instruction in the first place? Well, we are in between a method call. All the parameters are already loaded in to EAX, EDX and ECX. So we can’t use those to do the swap. The only way is to use the stack as temporary variable, and XCHG seemed to be the choice of the compiler engineer at the time interfaces were introduced to Delphi.

Let’s change that code to not use XCHG.

add eax,-$0 C      // convert the interface reference to the object reference
push eax           // reserve space for the method address used by RET
push eax           // save the object reference on the stack
mov eax,[eax]      // access the VMT
mov eax,[eax]      // get the “Test” VMT method address
mov [esp+04],eax   // write the method address to the reserved space
pop eax            // restore the object reference
ret                // do the jump to the method address

i7-4790 3.6GHz
10,000,000 calls to empty method Instance call Interface call Static method 12 ms 17 ms Virtual method 17 ms 99 ms Virtual method (XCHG) 164 ms

This is a lot faster, but still slow compared to the “Instance call”. The helper has a lot of memory accesses, but they shouldn’t slow it that much down, especially not in a tight loop when everything comes from the CPU’s cache.

So where does the code spend the time? Well, modern CPUs (after P1) have a feature called “return stack buffer”. The CPU puts the return address on the “return stack buffer” for every CALL instruction so it can predict where the RET instruction will jump to. This requires that every CALL is matched by a RET. But wait, the helper uses a RET for an indirect jump. We have the CALL from the interface method call, the RET in the helper and the RET in the actual method. That doesn’t match up. In other words, this helper renders the “return stack buffer” invalid what comes with a performance hit because the CPU can’t predict where to jump.

Let’s see what happens if we replace the RET with a JMP.

add eax,-$0C        // convert the interface reference to the object reference
push eax            // save the object reference on the stack
mov eax,[eax]       // access the VMT
push DWORD PTR [eax]// save the “Test” VMT entry method address on the stack
add esp,$04         // skip the method address stack entry
pop eax             // restore the object reference
jmp [esp-$08]       // jump to the method address

i7-4790 3.6GHz
10,000,000 calls to empty method Instance call Interface call Static method 12 ms 17 ms Virtual method 17 ms 24 ms Virtual method (RET) 99 ms Virtual method (XCHG) 164 ms

UPDATE: As fast as this implementation may be, it has a problem. As Allen and Mark pointed out, it accesses memory on the stack that is treated as free memory from the system. So if a hardware interrupt is triggered between the “add esp,$04” and the “jmp [esp-$08]”, the data on the stack is overwritten and the jump will end somewhere but not where it should be.

UPDATE 2: Thorsten Engler sent me an e-mail that invalidates the “hardware interrupt problem”. All interrupts are handled in kernel mode and kernel mode code doesn’t touch the user stack. The CPU itself switches the SS:ESP before invoking the interrupt handler.

Based on AMD64 Architecture Programmer’s Manual Volume 2 – System Programming Rev.3.22 Section 8.7.3 Interrupt To Higher Privilege:

When a control transfer to an exception or interrupt handler running at a higher privilege occurs (numerically lower CPL value), the processor performs a stack switch using the following steps:

  1. The target CPL is read by the processor from the target code-segment DPL and used as an index into the TSS for selecting the new stack pointer (SS:ESP). For example, if the target CPL is 1, the processor selects the SS:ESP for privilege-level 1 from the TSS.
  2. Pushes the return stack pointer (old SS:ESP) onto the new stack. The SS value is padded with two bytes to form a doubleword.
Categories: News, Blogs, and Tips

System.ByteStrings for 10.1 Berlin

Tue, 05/31/2016 - 03:30

Delphi 10.1 Berlin reintroduces UTF8String and RawByteString for the NextGen compilers (Android, iOS). But ShortString and AnsiString are still missing. The compiler has full support for them but you can’t use them because they are declared with a leading underscore in the System.pas unit what makes them inaccessible because “_” is compiled to “@” what you can’t use for an identifier.

By patching DCU files it is possible to make those hidden types accessible.

The unit System.ByteStrings for 10.1 Berlin reintroduces

  • ShortString
  • AnsiString
  • AnsiChar
  • PAnsiChar
  • PPAnsiChar
  • UTF8String (XE5-10 Seattle)
  • PUTF8String (XE5-10 Seattle)
  • RawByteString (XE5-10 Seattle)
  • PRawByteString (XE5-10 Seattle)

Add the System.ByteStrings.dcu’s path to the compiler’s search path and add the unit to your uses clauses.

There is no *.PAS file because the DCU is patched with a hex editor to get access to the hidden types.

Name IDE Version File Size Downloads Added System.ByteStrings XE5 RTM/UP1 only XE5ByteStrings.7z 2.45 KB 1013 times 2013-10-23 System.ByteStrings XE5 UP2 only XE5Up2ByteStrings.7z 2.85 KB 926 times 2013-12-20 System.ByteStrings XE6 XE6ByteStrings.7z 2.89 KB 806 times 2014-04-16 System.ByteStrings XE7 XE7ByteStrings.7z 2.89 KB 976 times 2015-01-20 System.ByteStrings XE8 XE8ByteStrings.7z 3.69 KB 1022 times 2015-04-16 System.ByteStrings 10 Seattle D10ByteStrings.7z 3.67 KB 1028 times 2015-09-01 System.ByteStrings 10.1 Berlin D101ByteStrings.7z 3.72 KB 406 times 2016-05-31
Categories: News, Blogs, and Tips

IDE Fix Pack 5.95 for Delphi 10.1 Berlin

Sun, 05/29/2016 - 16:01

IDE Fix Pack 5.95 supports RAD Studio 10.1 Berlin.

When Windows Defender (or any other anti-virus tool) sees the compiler creating a DCU file and the compiler calls CloseHandle on the file handle, the virus/malware scanner blocks the thread and takes its time to have a look at the file. This causes CloseHandle to take more than 2 milliseconds per file on my system. If you have 2500 units this sums up to 5 seconds. With Windows Defender disabled those 5 seconds go back to under 100 milliseconds.
Because the compiler can’t work in those 5 seconds, the IDE Fix Pack now delegates the CloseHandle calls to a background thread. This means Windows Defender can scan the file while the compiler works on the next units without being blocked by the scan.
On my Win32 test project this parallel execution made the rebuild 5 seconds faster. IDE Fix Pack guarantees that all written DCU files are closed before the binary executable is created. So if you have only some units you won’t see much of a speed improvement because the main thread may wait for the background thread to close all remaining files.


  • Added: RAD Studio 10.1 Berlin support
  • Added: CloseHandle for created DCU files is delegated to a background thread. Windows Defender “workaround”
  • Added: Fix for RSP-14557: DynArraySetLength – resizing an array of managed type is causing entire copy instead of realloc (D10.1, only the IDE)
  • Added: Fix for RSP-13116: TCustomImageList.BeginUpdate/EndUpdate (D10.0)


Name IDE Version File Size Downloads Added IDE Fix Pack 5.95 2009 (UP4) IDEFixPack2009Reg595.7z 180.99 KB 847 times 2016-05-30 IDE Fix Pack 5.95 (unsupported) 2010 (UP5) IDEFixPack2010Reg595.7z 177.23 KB 1054 times 2016-05-30 IDE Fix Pack 5.95 XE (UP1) IDEFixPackXEReg595.7z 163.4 KB 401 times 2016-05-30 IDE Fix Pack 5.95 (unsupported) XE2 (UP4+HF1) IDEFixPackXE2Reg595.7z 237.48 KB 393 times 2016-05-30 IDE Fix Pack 5.95 (unsupported) XE3 (UP2) IDEFixPackXE3Reg595.7z 191.69 KB 278 times 2016-05-30 IDE Fix Pack 5.95 (unsupported) XE4 (UP1) IDEFixPackXE4Reg595.7z 195.43 KB 206 times 2016-05-30 IDE Fix Pack 5.95 (unsupported) XE5 (UP2) IDEFixPackXE5Reg595.7z 194.6 KB 325 times 2016-05-30 IDE Fix Pack 5.95 (unsupported) XE6 (UP1) IDEFixPackXE6Reg595.7z 323.56 KB 315 times 2016-05-30 IDE Fix Pack 5.95 XE7 (UP1) IDEFixPackXE7Reg595.7z 339.73 KB 734 times 2016-05-30 IDE Fix Pack 5.95 XE8 (UP1) IDEFixPackXE8Reg595.7z 337.01 KB 454 times 2016-05-30 IDE Fix Pack 5.95 10 Seattle (RTM/UP1) IDEFixPackD10Reg595.7z 344.75 KB 1343 times 2016-05-30 IDE Fix Pack 5.95 10.1 Berlin IDEFixPackD101Reg595.7z 341.28 KB 3031 times 2016-05-30

Download (fastdcc):

Name IDE Version File Size Downloads Added fastdcc 5.95 2009 (UP4) fastdcc2009v595.7z 78.21 KB 119 times 2016-05-30 fastdcc 5.95 (unsupported) 2010 (UP5) fastdcc2010v595.7z 85.26 KB 160 times 2016-05-30 fastdcc 5.95 XE (UP1) fastdccXEv595.7z 87.18 KB 127 times 2016-05-30 fastdcc 5.95 (unsupported) XE2 (UP4+HF1) fastdccXE2v595.7z 109.87 KB 120 times 2016-05-30 fastdcc 5.95 (unsupported) XE3 (UP2) fastdccXE3v595.7z 124.02 KB 90 times 2016-05-30 fastdcc 5.95 (unsupported) XE4 (UP1) fastdccXE4v595.7z 124.17 KB 76 times 2016-05-30 fastdcc 5.95 (unsupported) XE5 (UP2) fastdccXE5v595.7z 125.57 KB 116 times 2016-05-30 fastdcc 5.95 (unsupported) XE6 (UP1) fastdccXE6v595.7z 153.72 KB 103 times 2016-05-30 fastdcc 5.95 XE7 (UP1) fastdccXE7v595.7z 165.99 KB 212 times 2016-05-30 fastdcc 5.95 XE8 (UP1) fastdccXE8v595.7z 166.1 KB 135 times 2016-05-30 fastdcc 5.95 10 Seattle (RTM/UP1) fastdccD10v595.7z 166.25 KB 304 times 2016-05-30 fastdcc 5.95 10.1 Berlin fastdccD101v595.7z 163.74 KB 751 times 2016-05-30

There is also a new IDE Fix Pack 6.0 Beta 3 that contains all the above and the experimental 64 bit compiler performance optimizations.


Name IDE Version File Size Downloads Added IDE Fix Pack 6.0beta3 (unsupported) XE2 (UP4+HF1) IDEFixPackXE2Reg60beta3.7z 245.08 KB 61 times 2016-05-30 IDE Fix Pack 6.0beta3 (unsupported) XE3 (UP2) IDEFixPackXE3Reg60beta3.7z 199.63 KB 49 times 2016-05-30 IDE Fix Pack 6.0beta3 (unsupported) XE4 (UP1) IDEFixPackXE4Reg60beta3.7z 202.2 KB 41 times 2016-05-30 IDE Fix Pack 6.0beta3 (unsupported) XE5 (UP2) IDEFixPackXE5Reg60beta3.7z 201.25 KB 65 times 2016-05-30 IDE Fix Pack 6.0beta3 (unsupported) XE6 (UP1) IDEFixPackXE6Reg60beta3.7z 343.08 KB 58 times 2016-05-30 IDE Fix Pack 6.0beta3 XE7 (UP1) IDEFixPackXE7Reg60beta3.7z 359.5 KB 81 times 2016-05-30 IDE Fix Pack 6.0beta3 XE8 (UP1) IDEFixPackXE8Reg60beta3.7z 356.88 KB 62 times 2016-05-30 IDE Fix Pack 6.0beta3 10 Seattle (RTM/UP1) IDEFixPackD10Reg60beta3.7z 364.08 KB 182 times 2016-05-30 IDE Fix Pack 6.0beta3 10.1 Berlin IDEFixPackD101Reg60beta3.7z 361.14 KB 346 times 2016-05-30 fastdcc 6.0beta3 (unsupported) XE2 (UP4+HF1) fastdccXE2v60beta3.7z 123.57 KB 47 times 2016-05-30 fastdcc 6.0beta3 (unsupported) XE3 (UP2) fastdccXE3v60beta3.7z 131.54 KB 40 times 2016-05-30 fastdcc 6.0beta3 (unsupported) XE4 (UP1) fastdccXE4v60beta3.7z 134.18 KB 38 times 2016-05-30 fastdcc 6.0beta3 (unsupported) XE5 (UP2) fastdccXE5v60beta3.7z 133.49 KB 35 times 2016-05-30 fastdcc 6.0beta3 (unsupported) XE6 (UP1) fastdccXE6v60beta3.7z 170.29 KB 37 times 2016-05-30 fastdcc 6.0beta3 XE7 (UP1) fastdccXE7v60beta3.7z 183.56 KB 68 times 2016-05-30 fastdcc 6.0beta3 XE8 (UP1) fastdccXE8v60beta3.7z 183.09 KB 57 times 2016-05-30 fastdcc 6.0beta3 10 Seattle (RTM/UP1) fastdccD10v60beta3.7z 183.25 KB 108 times 2016-05-30 fastdcc 6.0beta3 10.1 Berlin fastdccD101v60beta3.7z 182.31 KB 258 times 2016-05-30
Categories: News, Blogs, and Tips

DDevExtensions and DFMCheck for 10.1 Berlin

Sun, 05/29/2016 - 07:11

The DDevExtensions and the DFMCheck IDE plugins are now available for 10.1 Berlin.


Name IDE Version File Size Downloads Added DDevExtensions 1.61 5-2007 734.07 KB 16706 times 2009-01-10 DDevExtensions 2.8 Features PDF DDevExtensionsFeatures.pdf 602.92 KB 8085 times 2014-12-27 DDevExtensions 2.4 7, 2007 535.41 KB 8747 times 2011-07-25 DDevExtensions 2.6 (legacy) XE2+UP3 800.68 KB 1114 times 2013-11-28 DDevExtensions 2.84 2009-10.1 Berlin DDevExtensions284.7z 1.1 MB 2093 times 2016-05-28 Name IDE Version File Size Downloads Added DFMCheck 1.6 5-10.1 Berlin DfmCheckSetup16.7z 701.2 KB 592 times 2016-05-28

DDevExtensions Changelog:

  • Version 2.84 (2016-05-28)
    • Added: TAB key works like ENTER in the CodeInsight window.
    • Added: 10.1 Berlin support
Categories: News, Blogs, and Tips