Segfault (Segmentation fault) for no reason

Pages: 1234
Whenever I add a new variable to a specific external header my program crashes.
I debugged using GBD/CBD debugger from Code::Blocks , and it says: SIGSEGV(Segmentation Fault)

Code that works:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef ITEM_H_INCLUDED
#define ITEM_H_INCLUDED
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <stdarg.h>

#define ANY -1

#define BUFF_NONE 0
#define BUFF_HEALTH 1
#define BUFF_IMMORTAL 2
#define BUFF_WATERWALKING 3
#define BUFF_DEFENCE 4

class Item
{
private:
    float X,Y;
    int W,H;
    int OX,OY;
    const char* imgAddr;
    const char* Name;
    int TYPE;
    int damage,attackSpeed,armor,buffId,buffStr;
    SDL_Surface* surface;

public:
    Item();
    ~Item();
    Item(int x,int y,int w,int h,const char* name,const char* img_addr);
    Item(int x,int y,int w,int h,const char* name,const char* img_addr,int flags);

    void draw(SDL_Surface* window);
    void drawName(SDL_Surface* window,int x,int y);
    void move_to(int,int);
    void flag(int flag);
    void Move(char axis,float speed);
    void setWeapon(int Damage,int AttackSpeed);
    void setHelmet(int Armor);
    void setChestplate(int Armor);
    void setLeggins(int Armor);
    void setBoots(int Armor);
    void setAccessory(int buff_id,int buff_strength);
    void setConsumable(int buff_id,int buff_strength);
    void setArmor(int Armor);
    void setDamage(int Damage);
    void randomBuff();
    void use();
    int getX();
    int getY();
    int getH();
    int getW();
    int getOX();
    int getOY();
    int getItemType();
    int getDamage();
    int getAttackSpeed();
    int getArmor();
    int getBuffID();
    int getBuffStr();
    bool contains(int ,int);
    const char* getImgAddr();
    const char* getName();
};

#endif // ITEM_H_INCLUDED 


Code that doesn't work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef ITEM_H_INCLUDED
#define ITEM_H_INCLUDED
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <stdarg.h>

#define ANY -1

#define BUFF_NONE 0
#define BUFF_HEALTH 1
#define BUFF_IMMORTAL 2
#define BUFF_WATERWALKING 3
#define BUFF_DEFENCE 4

class Item
{
private:
    float X,Y;
    int W,H;
    int OX,OY;
    int some_int_name; ///NEW VARIABLE
    const char* imgAddr;
    const char* Name;
    int TYPE;
    int damage,attackSpeed,armor,buffId,buffStr;
    SDL_Surface* surface;

public:
    Item();
    ~Item();
    Item(int x,int y,int w,int h,const char* name,const char* img_addr);
    Item(int x,int y,int w,int h,const char* name,const char* img_addr,int flags);

    void draw(SDL_Surface* window);
    void drawName(SDL_Surface* window,int x,int y);
    void move_to(int,int);
    void flag(int flag);
    void Move(char axis,float speed);
    void setWeapon(int Damage,int AttackSpeed);
    void setHelmet(int Armor);
    void setChestplate(int Armor);
    void setLeggins(int Armor);
    void setBoots(int Armor);
    void setAccessory(int buff_id,int buff_strength);
    void setConsumable(int buff_id,int buff_strength);
    void setArmor(int Armor);
    void setDamage(int Damage);
    void randomBuff();
    void use();
    int getX();
    int getY();
    int getH();
    int getW();
    int getOX();
    int getOY();
    int getItemType();
    int getDamage();
    int getAttackSpeed();
    int getArmor();
    int getBuffID();
    int getBuffStr();
    bool contains(int ,int);
    const char* getImgAddr();
    const char* getName();
};

#endif // ITEM_H_INCLUDED 


If you have any idea what might be causing the problem please tell me.
Last edited on
I almost forgot.
Here's the debug log:

Building to ensure sources are up-to-date
Selecting target:
Debug
Adding source dir: C:\Documents and Settings\0hidden0\Desktop\PN01\
Adding source dir: C:\Documents and Settings\0hidden0\Desktop\PN01\
Adding file: C:\Documents and Settings\0hidden0\Desktop\PN01\bin\Debug\PN01.exe
Changing directory to: C:/DOCUME~1/0hidden0/Desktop/PN01/.
Set variable: PATH=.;C:\SDL SDK\lib;C:\Program Files\CodeBlocks\MinGW\bin;C:\Program Files\CodeBlocks\MinGW;C:\Program Files\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\wbem;C:\Program Files\ATI Technologies\ATI.ACE\Core-Static

[debug]Command-line: C:\Program Files\CodeBlocks\MINGW\bin\gdb.exe -nx -fullname -quiet -args C:/DOCUME~1/0hidden0/Desktop/PN01/bin/Debug/PN01.exe
[debug]Working dir : C:\DOCUME~1\0hidden0\Desktop\PN01

Starting debugger: C:\Program Files\CodeBlocks\MINGW\bin\gdb.exe -nx -fullname -quiet -args C:/DOCUME~1/0hidden0/Desktop/PN01/bin/Debug/PN01.exe
done

[debug]> set prompt >>>>>>cb_gdb:

Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints

[debug]Reading symbols from C:\DOCUME~1\0hidden0\Desktop\PN01\bin\Debug\PN01.exe...done.
[debug](gdb) >>>>>>cb_gdb:
[debug]> show version
[debug]GNU gdb (GDB) 7.5
[debug]Copyright (C) 2012 Free Software Foundation, Inc.
[debug]License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
[debug]This is free software: you are free to change and redistribute it.
[debug]There is NO WARRANTY, to the extent permitted by law. Type "show copying"
[debug]and "show warranty" for details.
[debug]This GDB was configured as "i686-pc-mingw32".
[debug]For bug reporting instructions, please see:
[debug]<http://www.gnu.org/software/gdb/bugs/>.
[debug]>>>>>>cb_gdb:
[debug]> set confirm off

Debugger name and version: GNU gdb (GDB) 7.5

[debug]>>>>>>cb_gdb:
[debug]> set width 0
[debug]>>>>>>cb_gdb:
[debug]> set height 0
[debug]>>>>>>cb_gdb:
[debug]> set breakpoint pending on
[debug]>>>>>>cb_gdb:
[debug]> set print asm-demangle on
[debug]>>>>>>cb_gdb:
[debug]> set unwindonsignal on
[debug]>>>>>>cb_gdb:
[debug]> set print elements 0
[debug]>>>>>>cb_gdb:
[debug]> set disassembly-flavor att
[debug]>>>>>>cb_gdb:
[debug]> catch throw
[debug]Catchpoint 1 (throw)
[debug]>>>>>>cb_gdb:
[debug]> source C:\Program Files\CodeBlocks\share\codeblocks/scripts/stl-views-1.0.3.gdb
[debug]>>>>>>cb_gdb:
[debug]> directory C:/DOCUME~1/0hidden0/Desktop/PN01/
[debug]Source directories searched: C:/DOCUME~1/0hidden0/Desktop/PN01;$cdir;$cwd
[debug]>>>>>>cb_gdb:
[debug]> run
[debug]Starting program: C:\DOCUME~1\0hidden0\Desktop\PN01\bin\Debug\PN01.exe

Child process PID: 324

[debug][New Thread 324.0xa0]
[debug][New Thread 324.0x924]
[debug][New Thread 324.0xf7c]
[debug][New Thread 324.0xc3c]
[debug][New Thread 324.0x778]
[debug][New Thread 324.0x758]
[debug][New Thread 324.0xff8]
[debug][New Thread 324.0xcac]
[debug]Program received signal SIGSEGV, Segmentation fault.
[debug]0x7c90e8e5 in strchr () from C:\WINDOWS\system32\ntdll.dll
[debug]>>>>>>cb_gdb:

Program received signal SIGSEGV, Segmentation fault.
In strchr () (C:\WINDOWS\system32\ntdll.dll)

[debug]> bt 30
[debug]#0 0x7c90e8e5 in strchr () from C:\WINDOWS\system32\ntdll.dll
[debug]#1 0x00000000 in ?? ()
[debug]>>>>>>cb_gdb:
Whenever I add a new variable to a specific external header my program crashes.


External... as in part of another library?

What header are you changing and how are you changing it?
Never mind.Fixed it by removing:

 
~Item();


Apperently it was trying to put the new variable as default operator for the class..for some reason.
Nevermind my last post now it's doing the same thing again!

And by external file I mean the code I posted.That's the external header that I created to hold the Item class.

I have no idea what's going on.It seems to randomly fail when building the projesct.I'm using Code::Blocks with the gnu gcc compiler.

Also , if I add only one variable it simply crashes , if I add two it gives me a don't send error.
Last edited on
Are you rebuilding your entire project, or just some files? It is likely that some files are not being rebuilt and thus are out of sync.
And by external file I mean the code I posted.That's the external header that I created to hold the Item class.


Whoops! Yeah I see it now. Sorry, the word "external" threw me off a bit.

Anyway the crash seems to be happening inside strchr(). Are you calling that function anywhere in your code?

Usually debuggers will snap on the exact line causing the crash. If you could narrow it down and post the actual code that is running during the crash that would be helpful.


Actually -- now that I look closer at the log it looks like it might be crashing at startup before it even gets to your code. Is that the case?

Assuming you don't have any global objects, you can set a breakpoint on the first line of main() and see if it's getting there. If not, then LB's suggestion seems the most likely.

If you do have global objects some other potential problems come to mind.
I tried rebuilding the entire project serveral times.
And it only crashes if I add variable to that spacefic class.Any other class from any other header works fine.
So..... do you have global objects?


Is your code getting to main or is it crashing before main?
I have news.I debugged for about an hour an found out that the error was created whenever I added a chest to the map (forgot to mention the code is from a rogue like dungeon game I'm working on).
That's great, but it still doesn't answer my questions. =P
ok so I hove no idea HOW but it solved itself.
I think Code::Blocks bugged out and wasn't building the whole project even tho it was saying it was.

I kept debugging and now it's fixed.

Thank you so much for you help and patience.
Dang it!
I said it to fast.
I left the chest adding method commented out...
It works without chests but .. I dont know how that makes any sense..
Disch , if you have some time to help me lemme know and i'll private message to you the whole code , of all of the files and maybe you can help me out.
Last edited on
I can't help if you don't answer my questions.

EDIT:

If you want to send me the code I'll take a look. Though you could just as easily answer my questions ;P
Last edited on
I have some global objects and it does get to main.
The whole code works fine if I take out the function that adds chests to the dungeon.Hmm.. i think I know where the error is.. ill have a look i'll tell you if I find something out.
Last edited on
Does the order in which your objects are constructed matter? Like does any global object use another global object?

EDIT: What I'm getting at is known as the "static initialization order fiasco", and it basically happens when one global object is constructed and accesses a 2nd global object before that 2nd object was constructed (which has very bad results).

It would explain the randomness of it varying from build to build since the order of objects can change. So sometimes they'd be constructed in the "proper" order, and sometimes they wouldn't be (which would make it crash)

EDIT 2: It's also (yet another) reason to avoid using globals.
Last edited on
Yes it does.
Since modifying Item.h makes it crash , it must have to do with that.
And Item.h is included in an InventoryTile.h which is included in an Inventory.h , which is included in Chest.h which is finally included in Map.h
Well Item.h as you posted it doesn't appear to have any global objects in it -- it just is a class.

Maybe it would be easier to see the entire code.
Pages: 1234