CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2006
    Location
    England
    Posts
    72

    Program runs ONLY if debugger is attached

    I have an interesting (and incredibly frustrating) problem where my application runs fine, but ONLY when a debugger is attached to it.

    I can build in both Debug or Release and double clicking the execuable causes it to crash before the window is drawn. However if I launch from the IDE (VS2010), again in both Debug or Release mode, the application runs perfectly fine.

    I'm at a loss and have no idea how to debug in this situation.

    All suggestions welcome.

    I am using VS2010 in Windows 7, C++ with MFC. This is an application which has been migrated from VC++ 6 to VS2010. Note, it works perfectly when built from VC++ 6.

    Thanks,
    AnotherMuggle.

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Program runs ONLY if debugger is attached

    A possible cause is that you are running your VS in elevated (UAC) mode, and this is how your app is expecting things. When ran outside of VS, your app runs un-elevated which may be causing the issues.


    First thing would be to identify if your app really does need to run elevated.
    If it does, then provide a UAC manifest to request elevation.
    If it doesn't, then change the code that depends on UAC elevation to no longer require this.


    Typically speaking, other than typical "admin mode" type programs and installations, your program should not require UAC elevation.

  3. #3
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: Program runs ONLY if debugger is attached

    Did you get any hint at what the problem could be when you pressed Retry in the crash dialog?
    Nobody cares how it works as long as it works

  4. #4
    Join Date
    May 2006
    Location
    England
    Posts
    72

    Re: Program runs ONLY if debugger is attached

    Thanks both.

    I've finally managed to get to the bottom of the crash but still no idea why it happens only when no debugger is attached.

    The problem is caused when loading a particular dll, which I believe was built using VC++ 6, and hense there is a compatability issue between my build and theirs!?

    I'm still not sure why having the debugger attached stops the crash though!?

    I tried running the application with "Run as admin" which I believe is the same as "UAC Elevated Mode"?

    Additionally I added the following early in the source code, which allowed me to run the executable with double-click then attaching a debugger:
    Code:
    while(!IsDebuggerPresent());
    When doing the above the application still didn't crash.

    Weird

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Program runs ONLY if debugger is attached

    You may have a memory buffer overflow which is affecting the code. Putting in the above line may mask the problem which is still present. Just because it 'worked' in vc6 doesn't mean that there isn't a problem - only that it wasn't seen then.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Program runs ONLY if debugger is attached

    Quote Originally Posted by AnotherMuggle View Post
    The problem is caused when loading a particular dll, which I believe was built using VC++ 6, and hense there is a compatability issue between my build and theirs!?
    Did you try to rebuild this DLL with VS2010?
    Victor Nijegorodov

  7. #7
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Program runs ONLY if debugger is attached

    Another possible cause for different behaviour when run by double-clicking it in Explorer vs. running it from inside the IDE is the current working directory. When run from Explorer, that is the directory withe the .exe file in it, while, with default project settings, it's the project directory (the one containing the source files) when run from the IDE. This may, obviously, decide about whether some external file is found or not, or, in case there's two versions of the file, which one gets accessed.

    If the above is the cause of the issue, the program should run fine when started from the IDE with "Run without debugging", just like running it in the debugger.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Program runs ONLY if debugger is attached

    Quote Originally Posted by AnotherMuggle View Post
    The problem is caused when loading a particular dll, which I believe was built using VC++ 6, and hense there is a compatability issue between my build and theirs!?
    It doesn't matter what tool built the DLL or how old the DLL happens to be. You can run DLL's created 15+ years ago with the latest Visual Studio if the DLL's follow certain guidelines.

    If the issue is the load of the DLL, then on the startup of the DLL, it may have bugs in DllMain(). If you're calling a function from the DLL, maybe the parameters do not match up, where they don't match in type or functionality (for example, the DLL takes a std::string argument, and you pass it a std::string created with Visual Studio 2013).
    I'm still not sure why having the debugger attached stops the crash though!?
    Any program that has memory corruption or overwrite bugs cannot be guaranteed to run reliably in any environment. It is as simple as that. Maybe your program has these issues.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    May 2006
    Location
    England
    Posts
    72

    Re: Program runs ONLY if debugger is attached

    Thanks for all the kind responses.

    I am fairly confident this is the explanation for my problem: http://siomsystems.com/mixing-visual-studio-versions/

    Paul - am think in this case the rules have been broken! Out of interest I tested breaking some of those rules, as detailed in the above link, and sure enough it exhibits very similar symptoms.

    Regarding rebuilding the DLL in VS2010, which I am sure will solve the problem, this is possible but I need to arrange a rebuild with the owner of the source.

    Thanks again,
    AnotherMuggle

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured