LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Segmentation Fault (https://www.linuxquestions.org/questions/programming-9/segmentation-fault-533860/)

arabindav 03-02-2007 05:59 AM

Segmentation Fault
 
Hello,

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;
}timer;

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)
return(SUCCESS);

loopCount++;
}

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.

devel.

matthewg42 03-02-2007 08:04 AM

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?

nmh+linuxquestions.o 03-02-2007 08:07 AM

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.


All times are GMT -5. The time now is 09:05 PM.