1

I am working on a game. At some places, sometimes I receive a segmentation fault and at all other times the code works properly. If I run the game again (after receiving segmentation fault) it recovers back (without any code changes) and runs fine. But after some time again this happens.

I tried debugging it using GDB. I got the following information:

  1. there is a function call : func(&s.x), where s is a structure and x is its member of type int. The address (&s.x) is 0xb3456721
  2. In function func the value received in the argument is 0xb.
  3. The program crashes saying memory at 0xb cannot be accessed. When I print the variable using GDB, I again receive memory cannot be accessed.

Any ideas Why this would happen?

Jonh
  • 199
  • 4
  • 9
  • 13
    Code or it didn't happen. – nneonneo Sep 28 '12 at 18:01
  • 2
    We'd have a lot more ideas if there is a code. – Lews Therin Sep 28 '12 at 18:02
  • What is the declaration of `func`? Do you have the prototype included every place you use it? – Paul Tomblin Sep 28 '12 at 18:02
  • 1
    You're going to have to share some code where you think the failure is. – Mike Sep 28 '12 at 18:02
  • 1
    `The program crashes saying memory at 0xb cannot be accessed.` That is precisely the reason for `SegFault`. Your code tries to write outside its allowed segment. It may happen when you try to dereference an uninitialized pointer or a dangling pointer. – Hindol Sep 28 '12 at 18:08
  • 1
    I'll bet that `offsetof(S, x)` is `0xb`, just like the address seen in `func`. That means that somewhere a null pointer is being dereferenced. – Pete Becker Sep 28 '12 at 18:46

3 Answers3

6

A program that crashes sometimes but not other times when given identical input has a non-deterministic data source in it. Usually the source is an uninitialized variable or block of memory, but it could be dependence on a timestamp, process ID, or other source of input from the system that varies.

Kyle Jones
  • 5,492
  • 1
  • 21
  • 30
1

I have used valgrind successfully before on Linux to debug non-deterministic behavior caused by uninitialized variables.

Here's the valgrind command I use, your program and options should follow

valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes --track-origins=yes -v 

If you are running on Windows, take a look at this answer for valgrind substitutes

Is there a good Valgrind substitute for Windows?

Is your program multi-threaded? One source of non-determinism is a race condition.

Community
  • 1
  • 1
amdn
  • 11,314
  • 33
  • 45
0

Since the C++ tag is present, but you are saying you are calling then function with: func(&s.x), then given that func says it received 0xb in its argument parameter leads me to believe s is actually an improperly initialized reference. For example, consider:

struct foo {
    char a[12];
    int x;
};

void func (int *x) {
    *x = 0;
}

int main () {
    foo *sp = (foo *)-1;
    foo &s = *sp;
    func(&s.x);
}

It is impossible to diagnose the precise nature of your problem because you have not provided enough information.

jxh
  • 69,070
  • 8
  • 110
  • 193