CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Programm crashes due to the presence of code even if it is not executed

  1. #1
    Join Date
    Oct 2020
    Posts
    2

    Exclamation Programm crashes due to the presence of code even if it is not executed

    Dear all,

    I have written the following program using Visual Studio Express 2013 to figure out my problem:


    #pragma comment (lib, "ws2_32.lib")

    #include <stdio.h>
    #include <winsock2.h>

    int main (int argc, char *argv[])
    {
    WSADATA wsa;
    if (1==2) WSAStartup(MAKEWORD(2,0),&wsa);
    } // main



    The programm crashes after start "Unhandled exception at 0x5DF1E71A in 3D_20.exe: 0xC0000005: Access violation writing location 0x000A0FF8."

    The problem is the code WSAStartup(MAKEWORD(2,0),&wsa);
    You see that the (1==2) never will be true so WSAStartup will not be excuted, but the presence of this piece of code makes the program crash.
    Useless to say that compiling give no errors.
    If I omit the critical line, the program runs withpout problems.

    If I insert printf... or whatever after main and before WSAStartup this will not executed! The problem is the pure existence of WSAStartup in the code!

    This is very strange to me. Any ideas?

    Best Regards,
    Andreas

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,844

    Re: Programm crashes due to the presence of code even if it is not executed

    Quote Originally Posted by anhu View Post
    Dear all,

    I have written the following program using Visual Studio Express 2013 to figure out my problem:


    #pragma comment (lib, "ws2_32.lib")

    #include <stdio.h>
    #include <winsock2.h>

    int main (int argc, char *argv[])
    {
    WSADATA wsa;
    if (1==2) WSAStartup(MAKEWORD(2,0),&wsa);
    } // main



    The programm crashes after start "Unhandled exception at 0x5DF1E71A in 3D_20.exe: 0xC0000005: Access violation writing location 0x000A0FF8."

    The problem is the code WSAStartup(MAKEWORD(2,0),&wsa);
    You see that the (1==2) never will be true so WSAStartup will not be excuted, but the presence of this piece of code makes the program crash.
    Useless to say that compiling give no errors.
    If I omit the critical line, the program runs withpout problems.

    If I insert printf... or whatever after main and before WSAStartup this will not executed! The problem is the pure existence of WSAStartup in the code!

    This is very strange to me. Any ideas?

    Best Regards,
    Andreas
    Rewrite your code to be convenient to debug:
    Code:
    int main (int argc, char *argv[])
    {
    	WSADATA wsa;
    	if (1==2) 
    	{
    		WSAStartup(MAKEWORD(2,0),&wsa);
    	}
    } // main
    set a breakpoint in the begin of main(...), start debugging and see what happens.
    Victor Nijegorodov

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,602

    Re: Programm crashes due to the presence of code even if it is not executed

    Is that your actual code? Sounds like you may have if(1 = 2)

  4. #4
    Join Date
    Oct 2020
    Posts
    2

    Re: Programm crashes due to the presence of code even if it is not executed

    Hi all,

    I have changed the code to

    #pragma comment (lib, "ws2_32.lib")

    #include <stdio.h>
    #include <winsock2.h>

    int main (int argc, char *argv[])
    {
    WSADATA wsa;
    int i;
    for (i=10;i<5;i++)
    {
    WSAStartup(MAKEWORD(2, 0), &wsa);
    }
    }


    and the same happens than before.
    For those who complained about "if (1==2)":
    You see, that the for loop will never be executed and still the program crashes.

    I set a breakpoint at main ... and the output is

    Attachment 35912

    what can I learn from this? I am not familiar with debugging as in C-tutorials usually there is not chapter about "working with debuggers".
    Any help is appreciated.

    Best regards,
    Andreas

  5. #5
    Join Date
    Feb 2017
    Posts
    579

    Re: Programm crashes due to the presence of code even if it is not executed

    Quote Originally Posted by anhu View Post
    The problem is the pure existence of WSAStartup in the code!
    Just because a section of code cannot be reached due to the program logic doesn't mean it's not present in the executable. It may still influence what else gets included that can cause problems.

    I would suggest you leave this for a while and instead concentrate on getting WSAStartup to work properly. When it does work you most likely will realize what's wrong now (even though it doesn't matter anymore ). Why no start with the example code here,

    https://docs.microsoft.com/en-us/win...ock-wsastartup

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

    Re: Programm crashes due to the presence of code even if it is not executed

    Visual Studio Express 2013
    I strongly recommend that you upgrade to Visual Studio 2019 Community (free). Express 13 is now 7 years old........

    The provided code compiles and runs OK on my computer using VS2019. What OS are you using? Are you compiling as 32/64 bit? Express had some limitations - but I can't remember what they were apart from no MFC.
    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++17 Compiler: Microsoft VS2019 (16.8.2)

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,844

    Re: Programm crashes due to the presence of code even if it is not executed

    Quote Originally Posted by anhu View Post
    Hi all,

    I have changed the code to

    #pragma comment (lib, "ws2_32.lib")

    #include <stdio.h>
    #include <winsock2.h>

    int main (int argc, char *argv[])
    {
    WSADATA wsa;
    int i;
    for (i=10;i<5;i++)
    {
    WSAStartup(MAKEWORD(2, 0), &wsa);
    }
    }


    and the same happens than before.
    For those who complained about "if (1==2)":
    You see, that the for loop will never be executed and still the program crashes.

    I set a breakpoint at main ... and the output is

    Attachment 35912

    what can I learn from this? I am not familiar with debugging as in C-tutorials usually there is not chapter about "working with debuggers".
    Any help is appreciated.

    Best regards,
    Andreas
    Your "attachment is invalid:
    Invalid Attachment specified. If you followed a valid link, please notify the administrator
    Where exactly did you set a breakpoint?
    Where exactly "the program crashes"? Please, copy the output and post it here.
    Victor Nijegorodov

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)