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.

View Comments “Delphi 2009 / Windows 7 / 64 bit Debugger Crash Workaround”
  1. Michael says:

    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

  2. Olaf Monien says:

    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 chance are that we might get an update somewhen.

    One question here is certainly: what will be released earlier? Delphi next version (Weaver) or Win7?

  3. [...] the blog site from my friend Olaf Monien you can find now a automatic path tool. it makes the changes for foru with one click. Possibly [...]

  4. remko says:

    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 0×100000
    #define CONTEXT_i386 0×00010000

    #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

  5. Name says:

    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.

  6. Olaf Monien says:

    Whohoo! Do you mind sharing that patcher? If you don't have a webspace, I can easily host it here on the blog.

  7. Olaf Monien says:

    Thanks for your comment – esp. for the highly interesting link about GetThreadConbtext on WOW64

  8. claudio68 says:

    Hi Olaf's

    GREAT! GREAT! GREAT!!!

    Thank you very muche for this solution!!

    best Regards

  9. Name says:

    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

  10. Olaf Monien says:

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

    My mailbox has no size limit.

    Regards,
    Olaf

  11. Michael says:

    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

  12. Name says:

    Mail on its way.
    Hope it will solve the problem for all you guys.

    Best regards
    LordByte

  13. Olaf Monien says:

    Great – thanks! I have posted a link to the patcher under “Update 2″ above.

  14. Name says:

    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

  15. remko says:

    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)

  16. Name says:

    Have tested it .. Changed 0x0F to 0×15

    Additional assertion errors not seen before occured.

  17. zvyagaaa says:

    Thanks a lot for your solution :) With Love From Siberia

  18. John says:

    Sweet dude, the automatic patch tool works like a charm, I can finally debug again!

  19. Carl Clark says:

    Hoo Haa!
    Thanks a million Olaf and LordByte (Patcher worked on my Delphi 2007).

  20. 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.

  21. OnasJ says:

    thx alot for this solution!

  22. nokfarang002 says:

    This is new information for working job.

    งาน
    งาน
    งาน part time
    งานราชการ

  23. Ron Sharp says:

    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.

  24. CRM455 says:

    Thanks guys,
    This saved me a lot of time trying to figure out what is going on.
    Patch program worked great on D2007.
    :)
    Chris

  25. Davit says:

    THANK YOU VERY MUCH

  26. philipljackson says:

    Thanks guys,
    I would never have been able to solve this one myself
    Patch program worked great on D2007.

  27. piranha says:

    Thanks dude!

  28. manishfusion says:

    Hi, very interesting blog. Nice work!

    Hot deals

  29. martin123 says:

    Hi, it’s a so nice and good site ever. It’s a really great and fantastic post here in this site. Thank you for the sharing of your ideas and information to all of us.mortgages.Thanks Regard.

  30. Torsten says:

    Thanks guys! This really worked for me, great.

  31. alfchristophersen says:

    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.

  32. Olaf Monien says:

    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
    http://www.delphiexperts.net
    http://www.monien.net

    Am 03.01.2010 um 18:28 schrieb Disqus:

  33. Name says:

    Thanks alot for this! :) Works for me too. (W7 x64)

  34. Name says:

    Hi!

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

    Thanks!

  35. Olaf Monien says:

    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
    http://www.delphiexperts.net
    http://www.monien.net

    Am 12.01.2010 um 09:34 schrieb Disqus:

  36. Mike Potter says:

    Thanks VERY much for supplying this patch. Worked great for me with Delphi 2007 on Windows 7 to fix this annoying problem.

  37. gisnap says:

    Thanks for sharing, i love this post

    http://currnews.com/
    currnews.com
    Very much live news

  38. Juanma says:

    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

  39. andrei says:

    Thanks for the patch. it worked.

  40. karlgeppert says:

    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.

  41. pcunite says:

    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?

  42. pcunite says:

    Solution, I edited it the same as bordbk120N.dll would need and it worked.

  43. Olaf Monien says:

    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” <
    >:

  44. pcunite says:

    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

  45. Marcello Grec says:

    Hi Olaf.
    Thank you!

  46. Michael says:

    Thank you for the interesting article and LordByte for the patcher.
    This bug was really annoying…

  47. AliFerster says:

    Great THANKS!!!!!!!!!!!++++++++

  48. Ryan says:

    Thank you for the link to the patcher, and thanks to LordByte for the patch itself! I've been dealing with the issue for the past few weeks and finally thought I'd search for a fix. Great patcher, thanks!

  49. ricardosartorato says:

    Man you rocks.

    The “update 2″ works perfectly.

    Thanks very much your attention to share this.

    Best Regards…

  50. donsergio says:

    Great, thanks man.

  51.  
Leave a Reply


blog comments powered by Disqus
CodeGear Technology Partner