SegFault Error, Need Help

This is a discussion on SegFault Error, Need Help within the C Programming forums, part of the General Programming Boards category; The program below is supposed to first, open the port, then send a command and wait for the port to ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    12

    SegFault Error, Need Help

    The program below is supposed to first, open the port, then send a command and wait for the port to say it is done. Then after all the commands are sent, it closes the port and ends the program. After sending four commands to the port, the program errors out and says it is a segfault error. If anyone can figure out what is going on, please let me know.

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int configure(void);  // function prototype
    int run(void); // function prototype
    int a; // initializing of variable a
    int b; // initializing of variable b
    
    
    
    int main()
    {
        printf("Running Program.\n");
        typedef long(*importFunction1)(long, long); //OpenPort signature
        typedef long(*importFunction2)(char*); //DriverSendToPort signature
        typedef long(*importFunction3)(char*, long); //WaitForChar signature
        typedef long(*importFunction4)(void); //ClosePort signature
        
            //Loading all functions at one time. 
        importFunction1 OpenPort;
        importFunction2 DriverSendToPort;
        importFunction3 WaitForChar;
        importFunction4 ClosePort;
        //load DLL file
        HINSTANCE hinstLib = LoadLibrary("VxmDriver.dll");
        if (hinstLib == NULL) {
                     printf("ERROR: unable to load DLL\n");
                     getch();
                     return 1;
                     }
        // Get OpenPort pointer
        OpenPort = (importFunction1)GetProcAddress (hinstLib, "OpenPort");
        if (OpenPort == NULL) {
                               printf("ERROR: unalbe to find OpenPort\n");
                               getch();
                               FreeLibrary(hinstLib);   
                               return 1;   
                               }
        // Get DriverSendToPort pointer
        DriverSendToPort = (importFunction2)GetProcAddress (hinstLib, "DriverSendToPort");
        if (DriverSendToPort == NULL) {
                               printf("ERROR: unalbe to find DriverSendToPort\n");
                               getch();
                               FreeLibrary(hinstLib);   
                               return 1;   
                               }
        // Get WaitForChar pointer
        WaitForChar = (importFunction3)GetProcAddress (hinstLib, "WaitForChar");
        if (WaitForChar == NULL) {
                               printf("ERROR: unalbe to find WaitForChar\n");
                               getch();
                               FreeLibrary(hinstLib);  
                               return 1;   
                               }        
        // Get ClosePort pointer
        ClosePort = (importFunction4)GetProcAddress (hinstLib, "ClosePort");
        if (ClosePort == NULL) {
                               printf("ERROR: unalbe to find ClosePort\n");
                               getch();
                               FreeLibrary(hinstLib);   
                               return 1;  
                               }
        printf("All files have loaded successfully.\nHit any key to enter program.\n");
        getch(); 
        //Run all functions and free library/print statement on success...
        OpenPort(2,9600);
        for ( a = 0 ; a < 10 ; a++ ) {
            if ( a > 0 ) {
                 DriverSendToPort("C,I1M400,R,");
                  WaitForChar("^",0);
                 }
            for ( b = 0; b < 10; b++ ) {
                DriverSendToPort("F,C,I2M400,R,");
                 WaitForChar("^",0);
                }
            DriverSendToPort("C,I2M-4000,R,");
             WaitForChar("^",0);
            }
        DriverSendToPort("C,I1M-4000,R,");
        WaitForChar("^",0);
        DriverSendToPort("Q,");
        ClosePort();
        FreeLibrary(hinstLib); 
        printf("successfully completed");
        getch();
        return 0;
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    1. your function signatures are char* and not const char* - Are you sure that your dll functions do not try to modify incoming buffers?
    2. You do not wait till the last string sent to the port is actually sent before closing the port - are you sure that the function sending buffer is working synchroneously? Try to sleep for 2000 ms before Closing the port, for example...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    12
    I changed it to const char* and that didn't help. It still has a segfault error. Also, i already have tried adding a "pause" into the program. I had it at 10 sec and it still errors out after four commands.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    All those functions return a value (apparently), which you're ignoring.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You don't free some of your resources when an error occurs while loading resources.
    Code:
        HINSTANCE hinstLib = LoadLibrary("VxmDriver.dll");
        if (hinstLib == NULL) {
                     printf("ERROR: unable to load DLL\n");
                     getch();
                     return 1;
                     }
        // Get OpenPort pointer
        OpenPort = (importFunction1)GetProcAddress (hinstLib, "OpenPort");
        if (OpenPort == NULL) {
            /* shouldn't you close hinstLib here? */
                               printf("ERROR: unalbe to find OpenPort\n");
                               getch();
                               FreeLibrary(hinstLib);   
                               return 1;   
                               }
        // Get DriverSendToPort pointer
        DriverSendToPort = (importFunction2)GetProcAddress (hinstLib, "DriverSendToPort");
        if (DriverSendToPort == NULL) {
            /* shouldn't you close hinstLib here? */
            /* shouldn't you close OpenPort here? */
                               printf("ERROR: unalbe to find DriverSendToPort\n");
                               getch();
                               FreeLibrary(hinstLib);   
                               return 1;   
                               }
            /* etc */
    At the very least you should call ClosePort() when the resource loadings after the port has been opened fail.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Segfault with additional variable?
    By misterFry in forum C++ Programming
    Replies: 11
    Last Post: 11-12-2008, 09:55 AM
  3. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 11:24 AM
  4. use of printf prevents segfault!
    By MK27 in forum C Programming
    Replies: 31
    Last Post: 08-27-2008, 12:38 PM
  5. Segfault and Warning help
    By Uncle Rico in forum C Programming
    Replies: 1
    Last Post: 03-25-2005, 01:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21