Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-02-2007, 06:59 AM   #1
LQ Newbie
Registered: Oct 2006
Posts: 9

Rep: Reputation: 0
Question Segmentation Fault


I am getting segmentation fault in my code. The nature of this segmentation fault is highly random. It randomly occurs and the application crashes.

There is a circular doubly linked list. I scroll through the list to find an element. The for loop that goes through the list uses a temporarily pointer. It is suddenly assigned to NULL and crash.

I run the application on red hat 9.0 and using C. The application has multiple threads.

Node definition is as follows:

typedef struct _timer {
tmrList timerList;
INT32 used;
timeValue timeoutTime;
UINT64 timeoutPeriod;
timeoutFunc timerHandlerFunc;
VOID* data;

typedef struct _tmrList {
struct _tmrList *prevElem, *nextElem;
} tmrList;

The loop that scroll through the list looks as follows:

for (tempElem = tmrQueue.nextElem; tempElem != &(tmrQueue); tempElem = tempElem->nextElem) {

if(tempElem == NULL) {
printf("tempElem is NULL\n");
else if(lElem == tempElem)


The logic seems to be fine. I have debugged several time with variable no of nodes, everything seems to be fine.

Any idea to debug this problem.

Thanks for your help.

Old 03-02-2007, 09:04 AM   #2
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
When posting code, please enclose it in [code] tags to improve readability.
  1. Compile the program with debugging symbols
  2. Set the ulimit so the program creates core files on segfaults (use the command "ulimit -c unlimited" in shell before running the program from that shell).
  3. Run the program until it crashes
  4. Load the corefile into gdb
  5. Do a backtrace

This will give you a starting point - probably you're trying to use a structure which is set to NULL (or is otherwise uninitialized) or whose address has been overwritten somehow. You should try to find a procedure for re-producing the error consistently - if you can, it's probably just a matter of breaking on memory access to the offending pointer, or realizing that it is never set in the first place.

This sort of problem is hard to debug. Threads makes it harder. It is why writing your own container code is not such a good idea if you can avoid it. Why not use a pre-written library for this sort of structure, which has already been debugged?
Old 03-02-2007, 09:07 AM   #3
Registered: Feb 2007
Posts: 135

Rep: Reputation: 15
Is the list shared between threads? do you have some sort of protection for it? Can you run your app with just one thread?

Crossposted with Matthew [matthewg42] - the posted suggestions should prove very useful, even if some are a bit lengthy lessons to learn.

Last edited by nmh+linuxquestions.o; 03-02-2007 at 09:10 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
segmentation fault guy_ripper Linux - Software 1 11-21-2006 02:10 AM
Segmentation Fault stefaandk Linux - General 2 09-04-2006 08:10 AM
What does 'segmentation fault' mean? Unforgiven79 Linux - Networking 1 08-29-2006 07:43 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 10:34 AM
Segmentation fault.... lokizen Linux - Newbie 7 09-14-2004 01:40 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:32 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration