Yesterday I revisited a nasty problem that occurs on Windows 64bit versions. I ran into that when I upgraded to Windows 7, but it seems it affects older 64bit editions of Windows as well.

After installing D2009 on a clean Win7 machine I was happy to see that Windows 7 really is what Vista should have been. It works snappy and has a lot of productivity features. But then when I started compiling and debugging a couple of my existing Delphi applications I experienced Debugger crashes almost everytime when I terminated an application.

image


Unfortunately no matter if you press “Yes” or “No” on that screen – you have to restart the whole Delphi IDE – which basically makes debugging impossible. In some (actually many) cases you even have to terminate Delphi using Windows TaskManager. I tried all of the compatibility settings Win7 offers, as well as running Delphi as Administrator – but no luck.

Some research on Google, QC and RAID showed that similar exceptions were reported with older Delphi versions, but apparently only on 64bit Windows editions. As the message already suggests, the problem appears to be an invalid call to a SetThreadContext API call. Some tests showed that you can easily reproduce this issue once you have more that just a single thread in your Delphi application. As you might know, I am doing quite a lot of Web application server development – which by nature create many threads…

I created a RAID entry for that (actually an existing one was updated with detailed reproduction steps – iirc) and joined a discussion thread on that topic to see if others are reporting that too. Because of time constraints I did not revisit that issue over the last couple of weeks, but yesterday when I was talking to Daniel Magin, we together found a solution someone had posted on a German Forum a few weeks ago. The original idea is from an MS TechNet posting though (credits go to a guy called Xelax Ax).

He posted a workaround that basically disables the check for a possible error returned by SetThreadContext, i.e. the debugger ignores the error and does not crash because of the Assert that checks fro no error returned. As Andreas Hausladen already correctly mentioned, this is really just kind of a weak workaround, because the debugger might misbehave after that and we just do not know why SetThreadContext fails at all. The point is though that with this fix I can work effectively again with D2009 on Win7/64bit and as this error only happens when the debugger is about to shout down anyway, so the danger of unwanted side effects is not too high imho.

Be careful! No waranties! I am not responsible for lost files etc.! Back up first!

Workaround steps:

  1. Close Delphi
  2. Locate bordbk120N.dll (C:\Program Files (x86)\CodeGear\RAD Studio\6.0\bin)
  3. Make a backup copy
  4. Check step 3
  5. Open bordbk120N.dll in a Hex editor (UltraEdit works fine)
  6. Search for “01 00 48 74 47 80 3D
  7. Make sure this is found once only
  8. Replace “74” with  “EB
  9. Save
  10. Restart Delphi. Error should be gone.

This might work on older Delphi versions as well, but I did not test that yet.

Update 1

I talked to some CodeGear Q&A guy and he tested their current internal Weaver build under the conditions where D2009 fails. The good news is that the error seems to be gone there – so the next Delphi Version will probably be good. For those who want to get started now: please see Update 2 below.

Update 2

LordByte sent me his automatic patch tool for Delphi 2007 and Delphi 2009. Just click “Apply patch” and you are done.

image

Get the patcher here: Delphi_2007_2009_WOW64_Debugger_Fix.zip (169 KB)

Note: Provided as is for free. Use at your own risk.

  • pcunite
    I have CB2007 installed on Win7 x64 and I don't have the bordbk120N.dll file but I do have bordbk105N.dll. Any ideas as I have the same error?
  • Your dll is definitely a different Version. I didn't touch C Builder
    for while, so I don't have a sugestion right now. Sorry

    --
    Grüße / Regards,
    Olaf Monien

    Sent via iPhone

    Am 12.03.2010 um 10:12 schrieb "Disqus" <
    >:
  • pcunite
    Well I can confirm after using it all day that it works. If it help my CB2007 version is:
    CodeGear™ C++Builder® 2007 R2 Version 11.0.2902.10471
    Installed Updates:
    December 2007 Update
    May08 Help Update
  • pcunite
    Solution, I edited it the same as bordbk120N.dll would need and it worked.
  • karlgeppert
    Thanks for that - takes the stress back out of my day. The auto-patcher worked beautifully once I woke up enough to remember to run it as admin on win7.
  • andrei
    Thanks for the patch. it worked.
  • Juanma
    Thank you very much.
    I reinstall my C++Builder 2007 in a W7 x64 an get this error (not always, but frecuently).
    I had edited bordbk120N.dll and now work ok. Great.

    Best regards
    Juan Manuel Marin
  • Mike Potter
    Thanks VERY much for supplying this patch. Worked great for me with Delphi 2007 on Windows 7 to fix this annoying problem.
  • Name
    Hi!

    Just a quick verification: Can someone please verify that this problem does NOT occur anymore in Delphi 2010?

    Thanks!
  • I have not seen that problem on my D2010/Win64 bit installation so far. IIRC it was marked as fixed in RAID.

    Regards / Grüße,
    Olaf Monien
    ------------------------------------------
    EDV-Beratung Monien

    Embarcadero Technology Partner
    Delphi Experts Chairman
    www.delphiexperts.net
    www.monien.net




    Am 12.01.2010 um 09:34 schrieb Disqus:
  • Name
    Thanks alot for this! :) Works for me too. (W7 x64)
  • alfchristophersen
    What about BDS 2006 which I must have running on Windows 7 in a few weeks from now when my boss, University of Oslo is going to abandon completely XP and Vista and all 32-bits computers. Just installing 64 bits Windows 7 in all computers.

    Sounds like a "suicide" to me for those of us needed to support also old 2006 code with Ansi support for a long time still because of amount of code to change in order not to break binary streams.
  • Honestly, I didn't try BDS2006 on Windows 7. Maybe you should setup virtual machines (with XP) to run BDS2006.

    Regards / Grüße,
    Olaf Monien
    ------------------------------------------
    EDV-Beratung Monien

    Embarcadero Technology Partner
    Delphi Experts Chairman
    www.delphiexperts.net
    www.monien.net




    Am 03.01.2010 um 18:28 schrieb Disqus:
  • Thanks guys! This really worked for me, great.
  • piranha
    Thanks dude!
  • philipljackson
    Thanks guys,
    I would never have been able to solve this one myself
    Patch program worked great on D2007.
  • Davit
    THANK YOU VERY MUCH
  • CRM455
    Thanks guys,
    This saved me a lot of time trying to figure out what is going on.
    Patch program worked great on D2007.
    :)
    Chris
  • OnasJ
    thx alot for this solution!
  • Not sure how much this affects older versions of Windows. I've been running Delphi 2009 under Vista 64 for almost a year now and never seen this problem once, even in multi-threaded apps.
  • Carl Clark
    Hoo Haa!
    Thanks a million Olaf and LordByte (Patcher worked on my Delphi 2007).
  • John
    Sweet dude, the automatic patch tool works like a charm, I can finally debug again!
  • zvyagaaa
    Thanks a lot for your solution :) With Love From Siberia
  • claudio68
    Hi Olaf's

    GREAT! GREAT! GREAT!!!

    Thank you very muche for this solution!!

    best Regards
  • Name
    Thanks a million, dude. From your information I have a created a fully automatic patcher. The patcher has been used by 8 colleagues (so far) of mine, and they all report success .. no more assertion errors.
  • Whohoo! Do you mind sharing that patcher? If you don't have a webspace, I can easily host it here on the blog.
  • Name
    I dont mind at all.

    Please instruct me where to upload (FTP?), or give me an email adress I can send the attachment to :-)

    Best regards
  • Please send via e-mail to olaf (at) monien (dot) net

    My mailbox has no size limit.

    Regards,
    Olaf
  • Name
    Mail on its way.
    Hope it will solve the problem for all you guys.

    Best regards
    LordByte
  • Great - thanks! I have posted a link to the patcher under "Update 2" above.
  • Name
    Will even update it for other versions of Delphi,
    if anyone so desire. You must provide the
    native BORDBGxxxN.dll for inspection, and the
    BDS-basekey in the HKEY_CURRENT_USER registry-hive.

    Anyway, I hope it works for all of you.

    Best regards
    Lordbyte
  • Ron Sharp
    Thanks! That did the trick with Delphi 2009 and Windows 7 for me (after I remembered to run it as an admin). There still seems to be a delay between closing the app and control returning to the IDE, but that's preferable to having to restart Delphi.

    It would be nice if there was an official patch for this, short of Delphi 2010, since it wasn't that long ago that I upgraded to 2009.
  • remko
    Interesting issue. If we look into the pseudo code of this function:
    *(_DWORD *)(a1 + 3548) = 65551;
    if ( SetThreadContext(*(HANDLE *)(v1 + 684), (const CONTEXT *)(a1 + 3548)) != 1 )
    {
    if ( byte_455BAC )
    {
    v8 = GetLastError();
    v9 = (*(int (__cdecl **)(int))(*(_DWORD *)v1 + 132))(v1);
    sub_401B08("setContext osTId 0x%x failed %d", v9, v8);
    }
    if ( "SetThreadContext failed" )
    sub_40C6AC("(!\"SetThreadContext failed\")", "..\\win32src\\thread32.cpp", 434);
    }

    We can see that the Context flag is set to 65551 and if we look into the comments in the header file (winnt.h):
    #define CONTEXT_AMD64 0x100000
    #define CONTEXT_i386 0x00010000
    ...
    #define CONTEXT_SEGMENTS (CONTEXT_i386 | 0x00000004L) // DS, ES, FS, GS

    so all other flags are combines with either CONTEXT_AMD64 or CONTEXT_i386. It might be worth trying to patch this flag to the corresponding value for AMD64. If someone wants to try:
    .text:0043C7F5 C7 83 DC 0D 00 00 0F 00 01 00 mov dword ptr [ebx+0DDCh], 1000Fh

    /edit: found this link: http://www.nynaeve.net/?p=129
  • Thanks for your comment - esp. for the highly interesting link about GetThreadConbtext on WOW64
  • remko
    I don't have an x64 os so I can't try but you can also try to make the following change:
    C7 83 DC 0D 00 00 0F 00 01 00 to C7 83 DC 0D 00 00 15 00 01 00

    It changes the Context flag from 65551 (CONTEXT_FULL or CONTEXT_FLOATING_POINT) to 65557 (CONTEXT_ALL)
  • Name
    Have tested it .. Changed 0x0F to 0x15

    Additional assertion errors not seen before occured.
  • Michael
    So who is in charge of this to fix this annoying bug? I don't think it's Microsoft actually - so I guess CG is responsible to hot fix this in their latest flagship D2009 (and hopefully not in an upcoming version)

    Michael
  • CG will certainly say, that D2009 was released before Win7 and Win7 is not even officially released yet.
    I talked to some Q&A guy of CodeGear, he checked that error on their current internal build, and the error does not seem to be reproducible anymore. So chances are that we might get an update somewhen.

    One question here is certainly: what will be released earlier? Delphi next version (Weaver) or Win7?
  • Michael
    Regarding your last question: In this case I would prefer Win 7 to be released earlier. Let's see if the Delphi community has got enough power to 'force' CG to release a hot fix for this issue.

    Michael
blog comments powered by Disqus
CodeGear Technology Partner