Thread: Difficult to catch bug

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    147

    Difficult to catch bug

    Hello,

    I have a problem with a C program I have developed and I need same advice. First I describe the environment: program has around 700 lines of code, windows 7, MINGW. I have a "clean" compilation, I mean, no warnings by the linker (compiled with -W).

    The program runs in the production environment without problem 99% times: 1 time each 3/4 minutes aprox., zero to four hangs in a day. Each runs has different input data (a different file). I have created a log to see where it crashs. When it crashs, if I try to reproduce the problem running the program with the input file which crash with, but I have no luck as when I run it, it does not crash.

    What I see, is the program does not crash always in the same place, altought it does many times in a fclose() function.

    I have reviewed my code lot of times because I suspect it is maybe a pointer curruption or similar, but have not find anything rare. I can not imagine where is the problem.

    So, my first question is: is it possible in windows to save a 'coredump', as in linux, in order to load in the debbuger and investigate?

    And, what do you suggest to find the problem? It is very annoyed and can not find the solution.

    Thank you very much

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Try compiling with the options "-Wall -Wextra -pedantic". That may increase the chances of the compiler picking up something suspicious.

    I wouldn't dismiss a pointer molestation. Many ways of achieving that.

    It is also possible your code has a memory leak. That can cause an intermittent failure, depending on the order in which memory is allocated and deallocated.

    Microsoft does (or at least, used to - I'm not sure if they still do) provide a free utility called Userdump.exe which creates a dump file for a program that terminates abnormally. The (now Microsoft) SysInternals suite includes a utility named ProcDump that may be useful in some circumstances.

    Given that you are referring to "runs", I assume your program has a loop that performs multiple "runs". It would be a good idea to check that ALL variables are being initialised correctly on the first "run", and reinitialised correctly on subsequent runs. A common cause of intermittent failures is a one or two variables that are not being initialised correctly - and could explain your observation that re-running the program after a crash with the same input file does not cause a crash. Look for code that assumes a auto variable is initialised to zero (which is a programming error, but is likely to go undetected as program memory is more likely to be initialised to zero on program startup than in subsequent "runs", for various reasons).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    MIGW has valgrind, I believe. If you run it under valgrind, that reports most of the problems to do with illegal memory accesses, uninitialised variables, unfreed allocations, and the like. The only thing it can't warn you about is when you tell the program to do the wrong thing, in a perfectly well-define, legal manner.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Malcolm McLean View Post
    MIGW has valgrind, I believe. If you run it under valgrind, that reports most of the problems to do with illegal memory accesses, uninitialised variables, unfreed allocations, and the like. The only thing it can't warn you about is when you tell the program to do the wrong thing, in a perfectly well-define, legal manner.
    Where can you get valgrind that works under windows?

    Edit: You might try using DrMingw DrMingw - jrfonseca - Dr. Mingw - José Fonseca's utilitities - Google Project Hosting
    The Code::Blocks project uses a variation of EXCHNDL.DLL to help trace bugs like this.

    Tim S.
    Last edited by stahta01; 04-08-2013 at 07:16 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    Quote Originally Posted by grumpy View Post
    Try compiling with the options "-Wall -Wextra -pedantic". That may increase the chances of the compiler picking up something suspicious.

    I wouldn't dismiss a pointer molestation. Many ways of achieving that.

    It is also possible your code has a memory leak. That can cause an intermittent failure, depending on the order in which memory is allocated and deallocated.

    Microsoft does (or at least, used to - I'm not sure if they still do) provide a free utility called Userdump.exe which creates a dump file for a program that terminates abnormally. The (now Microsoft) SysInternals suite includes a utility named ProcDump that may be useful in some circumstances.

    Given that you are referring to "runs", I assume your program has a loop that performs multiple "runs". It would be a good idea to check that ALL variables are being initialised correctly on the first "run", and reinitialised correctly on subsequent runs. A common cause of intermittent failures is a one or two variables that are not being initialised correctly - and could explain your observation that re-running the program after a crash with the same input file does not cause a crash. Look for code that assumes a auto variable is initialised to zero (which is a programming error, but is likely to go undetected as program memory is more likely to be initialised to zero on program startup than in subsequent "runs", for various reasons).
    I am trying to catch signals with:

    signal(SIGINT, SIG_IGN);
    signal(SIGABRT, GestionSenales);
    signal(SIGFPE, GestionSenales);
    signal(SIGILL, GestionSenales);
    signal(SIGSEGV, GestionSenales);
    signal(SIGTERM, GestionSenales);

    But my program crash without calling GestionSenales function. Is that normal?

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    But my program crash without calling GestionSenales function. Is that normal?
    You will need to show some code to answer that
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    Quote Originally Posted by rogster001 View Post
    You will need to show some code to answer that
    int main(int argc, char *argv[]) {
    /* ---- Ponemos la consola en modo unbuffered ---- */
    setbuf(stdout, NULL);
    setbuf(stdin, NULL);
    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stdin, NULL, _IONBF, 0);

    /* ---- Ignoramos las señales de interrupción ---- */
    signal(SIGINT, SIG_IGN);
    signal(SIGABRT, GestionSenales);
    signal(SIGFPE, GestionSenales);
    signal(SIGILL, GestionSenales);
    signal(SIGSEGV, GestionSenales);
    signal(SIGTERM, GestionSenales);
    .....


    void GestionSenales(int id) {
    printf("SIGNAL %d RECEIVED *****\n", id);
    .....


    I dont get printf. I only get a windows by OS telling me myprogram.exe hash crashed.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You cannot use printf() in a signal handler.
    Google for a list of allowed functions in signal handlers
    Kurt

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    following nice answer from ZuK here is a good link as regards GNU
    Last edited by rogster001; 04-11-2013 at 01:39 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    Hello,

    I think I have detected where the problem is and ........ is it not in my code, but the way my application work. I explain looking for help: I have four .bat files (in windows 7) that copy four different file to the path where my app is. This copy process is doing around 1 time each minute, more or less. The problem lies in that my app run at specifics intervals, open the four files and process certain matematical operations. What I see is that sometimes my app open one of the files while the OS is still copying files, so two process (my app and my .bat file) are accessing at the same time to the file.
    Is there any way to lock file in order to access it, knowing no other process can write the file? I have noted and it is my main suspiction I am processing the file while os is finishing copy, so I dont get the data I want. This is the reason is happening from time to time, where the scheduled task match.
    or any way to tell my program to wait until copy file has finished????
    Any help will be wellcome?
    thanks
    Last edited by Kempelen; 04-15-2013 at 10:54 AM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    fopen, _wfopen
    You could try opening the files in "r+" mode, which should fail if another process already has the file open for writing.
    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.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by ZuK View Post
    You cannot use printf() in a signal handler.
    Google for a list of allowed functions in signal handlers
    Kurt
    On Windows you can. This just looks like POSIX. It isn't actually POSIX. This is Windows World.

    EDIT: Double-checked, and it turns out printf() is unsafe on Windows as well. Point still stands that the rules are not the same as POSIX, however.
    Last edited by brewbuck; 04-15-2013 at 12:06 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very difficult task
    By kantagara in forum C Programming
    Replies: 13
    Last Post: 10-04-2012, 03:25 PM
  2. How Difficult is This?
    By MAtkins in forum C Programming
    Replies: 43
    Last Post: 02-11-2011, 02:21 PM
  3. Is there a catch to return in a try/catch box?
    By meili100 in forum C++ Programming
    Replies: 25
    Last Post: 11-21-2007, 01:33 PM
  4. A difficult Problem.
    By Kam in forum C Programming
    Replies: 4
    Last Post: 11-13-2002, 05:06 AM
  5. why must everything be so difficult!
    By ... in forum C++ Programming
    Replies: 14
    Last Post: 03-22-2002, 04:21 PM

Tags for this Thread