473,395 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

program crashes on exit

2
I program I'm writing crashes when it exits with a segfault. I'm runnning under linux. Using gdb, i see that the crash happens after the last line of main(), which is just a return(EXIT_SUCCESS); If I put an exit(EXIT_SUCCESS); right before that line, it doesn't crash though!

In gdb, when the crash occurs, a backtrace gives the following:
#0 0x0804e090 in ?? ()
#1 0xb7ea2ff4 in ?? () from /usr/lib/debug/libc.so.6
#2 0xb7ea4140 in ?? () from /usr/lib/debug/libc.so.6
#3 0x0804e008 in ?? ()
#4 0xbfac1928 in ?? ()
#5 0xb7ddade9 in *__GI___libc_free (mem=0x0) at malloc.c:3622
#6 0x08048c51 in _start ()

I'm using a debug version of libc, as you can see, but it doesn't help much.

http://c-faq.com/strangeprob/crashatexit.html describes the problem, but the solutions don't seem to apply to my program.

The really strange thing is that the problem is erratic - usually it happens the way but occasionally (maybe every 10th time or so) there's no problem at all, or else it just freezes without a segfault. When frozen, I've attached gdb, and it reports that it's just sitting on the last line of the program - just the final curly bracket - over and over again with nothing changing.

Valgrind shows the following when it crashes:
==24604== Use of uninitialised value of size 4
==24604== at 0x8049AAB: main (ttuner.c:234)
==24604==
==24604== Invalid read of size 1
==24604== at 0x41E5FF6: (within /usr/lib/debug/libc-2.6.1.so)
==24604== by 0x8048C50: (within /home/jono/src/ttuner/ttuner)
==24604== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==24604==
==24604== Process terminating with default action of signal 11 (SIGSEGV)
==24604== Access not within mapped region at address 0x0
==24604== at 0x41E5FF6: (within /usr/lib/debug/libc-2.6.1.so)
==24604== by 0x8048C50: (within /home/jono/src/ttuner/ttuner)
==24604==
==24604== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 19 from 1)
==24604== malloc/free: in use at exit: 0 bytes in 0 blocks.
==24604== malloc/free: 10 allocs, 10 frees, 1,381,121 bytes allocated.
==24604== For counts of detected errors, rerun with: -v
==24604== All heap blocks were freed -- no leaks are possible.


Is there anything else that might be helpful in debugging this?
Feb 25 '08 #1
2 6386
gpraghuram
1,275 Expert 1GB
If you get a crash at the exit and when it happens inconsistently then it means there is some memory corruption in the code.
Try to use some memory corruption identification tool like Purify to solve the issue or do a code walk through

Raghuram
Feb 26 '08 #2
jaddle
2
Problem has been solved! Thanks for the suggestion.

It turns out that I had an array that wasn't quite long enough for the data going into it - a pretty standard mistake!

The interesting bit, and what I really learned from the whole thing, is why it was breaking in such an interesting way, instead of just crashing when the invalid memory got accessed.

I had an array declared as char notename[3]; in main(). Unfortunately, this string could be any of A, B, C#, or Ebb (double-flat), etc. (it's a program to generate sounds to use for tuning instruments). The double flats take an extra character!

What happened then was, because this was stack memory (i.e. not malloc'ed), one byte at the end turned out to be part of something like the return address for main - i.e. where it goes when it finished. Once I screwed that up, it couldn't return properly and crashed on exit.

Changing the declaration to char *notename = malloc(sizeof(char) * 3); eliminated the ugly crashes, but still left the bug that the array was being overrun, so other data corruption (at the least) could have ensued. However, valgrind was able to see the overrun easily and the problem was fixed with no further problems.

In addition to 'purify', suggested here (I'll have a look at that soon, thanks!) someone mentioned that compiling with -fmudflap (and possibly -lmudflap) could also detect things like this. Haven't tried it yet, but it's good to know for next time.
Feb 26 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ken | last post by:
When running a program in the debugger, what would cause it to crash without any error messages? I get "The program has exited with code 0 (0x0)". The program is a MDI app with threading for...
8
by: mike2036 | last post by:
I have an application (that has unmanaged code) and when I launch it without 'FullTrust' permissions (LocalIntranet_Zone), it crashes. When I set 'FullTrust' permissions, it launches fine. Is...
11
by: seattleboatguy | last post by:
I am trying to send a message from a visual c++ user-thread to the main window, so the main window can update text on the window to reflect what the user-thread is doing. I have tried 2 different...
22
by: walterbyrd | last post by:
I like to develop on my desktop, then when I get stuff working, I copy to my web-site. I set up a new version Xampp on my windows-2k desktop. And downloaded the stuff from the website to edit....
1
by: billsahiker | last post by:
Why is my application crashing on a windows 98 machine? It runs fine on the development machine with xp pro.It was developed with VS2005 in VB. I installed framework 2 and windows installer 2 on...
41
by: z | last post by:
I use Visual C 2005 to develop my programs. One in particular is crashing in very specific and hard to replicate situations, made worse by the fact it only crashes when run -outside- the dev - as...
13
by: Thomas Neubauer | last post by:
Hello, i am learning c# and have created now a simple project that just creates 6 random numbers. My form includes a button and 6 labels for the random numbers. The program seems to work...
3
by: plbaldri | last post by:
I have written a very simple and basic program for an intro Comp Sci class and am having trouble with it crashing. The program needs to accept a filename on the command line at the prompt and has to...
6
by: =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Usually someone writes a program and guarantees its behaviour so long as people don't deliberately go and try to make it malfunction. For instance, let's say we have a "Proceed" button on the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.