Help tracking down SIGBUS/SIGSEGV errors, please?

Jones
Unregistered
 
Post: #1
I just finished writing up an MD2 model class for OpenGL, applying whatever I could learn from the multiple tutorials on the net, and some c++ class savvy I recently aquired. After going through all 130-some errors, and reducing it to zero-errors & zero warnings, I decided to test it out. Well, naturally it didn't work. No surprises there, *all* the code I write is killed by the system or another framework for some reason or another. Usually the dreaded SIGBUS, which I have groan (hehe, get it?) to loath with a heated passion. Unfortunatly, I also have an uncanny nack at be unable to track down the cause of this error. Anyway, SIGBUS:

Quote:Your code tried to read or write a memory location where no virtual memory is mapped.

Very similar is signal 11, SIGSEGV, "Segmentation Violation". In this case, there's virtual memory mapped to this address, but it's outside the range your application is allowed to read/write.

I'm guessing this is a direct result of the malloc stuff being used in the code. At first it was because I had forgotten to allocate memory to the header, but you don't need too... heh, no easy solutions for me.

So then I put in little cout tags to tell me what just happened. I stuck them throughout the open function. Nothing. So I stuck them in my main file, right before I declared a *static* MD2 (this time within a function) and right after. It told me it was about to declare an MD2, then told me it had done so, then got a SIGBUS error. So I tried *AGAIN* this time with the MD2 as non-static. Same results, but this time with a SIGSEGV (11) error. In both cases it got to the opening function then crashed, so it's in there. Not sure where though, perhaps you guys could help, please?

I stuck the code (Zipped xCode GLUT/OpenGL project) up for download. I had to remove the build folder though, because it was 27 wopping megs, for some reason (Even after a cleaning of all targets.). All GLUT projects do that (grow huge), it's actually quite annoying.

If you need to rebuild the project structure yourself, you can find a tutorial for GLUT projects (if you don't already know how) on OneSadCookie's tutorial page, here.

Download the source.

Thanks for any help you can offer! Smile
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #2
What you want to do is to enable Guard Malloc under the Debug menu in XCode and then debug your app. It will run slow (expect two-three minutes to start the app) but it will break as soon as you make your memory error.
Quote this message in a reply
Jones
Unregistered
 
Post: #3
Fenris Wrote:What you want to do is to enable Guard Malloc under the Debug menu in XCode and then debug your app. It will run slow (expect two-three minutes to start the app) but it will break as soon as you make your memory error.

xCode can be used to actually debug stuff? Wow... that's new, my mind is boggling. Wacko

Thanks for the tip, I'll try that right away.

NOTE for anybody who reads this thread: Yes, I know my class does not load skins, I left that out for more portability. I'll add some functions to give the user the paths they need.

**EDIT: Did as fenris suggested, and got some stuff returned. Here's some stuff from the console:

Quote:About to declare MD2...
Declared static MD2!
Program received signal: "EXC_BAD_ACCESS".
mi_cmd_stack_list_frames: Not enough frames in stack.
mi_cmd_stack_list_frames: Not enough frames in stack.

Do you suppose it means the variable "frames" of which I have one in every class, for holding MD2 frames?

The debugger gave back these 5 (in a C sense Wink ) things that appear to be errors:

Quote:#0 0xffff88e4 in objc_msgSend_rtp
#1 0x9001ada0 in fread
#2 0x00002768 in MD2::Open
#3 0x0000378c in init
#4 0x00003800 in main
#5 (null)

I think that means that in my main loop, where I call init() in which I call the MD2::Open function, in which fread is used, there is a problem. That's what I originally thought. What exactly does objc_msgSend_rtp means, does it help me in any way?
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #4
Gimme a second, I'm halfway through fixing it for you Rasp
Quote this message in a reply
Jones
Unregistered
 
Post: #5
Fenris Wrote:Gimme a second, I'm halfway through fixing it for you Rasp

If it's any help, I noticed in all the tutorials I followed, "header" is not a pointer, like I made it. I tried changing it to a non-pointer, in every case it's used. Should have worked, I figured, but I got more debug stuff. This time complaining about GMmalloc.
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #6
Right, there it was. In the MD2.h file, you're trying to fread the header into the header pointer. Only, that doesn't have any allocated memory to it. You're just reading the header into any random memory address, which can be anything. In my case, it thrashed the stack frames (which aren't related to MD2 frames, by the way. Just a bad name clash. Rasp )

Anyway, to fix it, just go

header = new header_MD2();

on line 144 (just before the fread call) and then add

delete header;

to the cleanse method, and you're good to go. Smile
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #7
Anyway, if you hate SIGBUS/SIGSEGV with a passion, just turn on guard malloc, hit Cmd-Y and wait for the program to die. In 99% of the cases, it has broken on the selected line, and the problem is that you have either:
• Written to unallocated memory
• Overwritten a buffer
• Freed memory twice

Hope this helps. Smile
Quote this message in a reply
Jones
Unregistered
 
Post: #8
Hurray it's working!

Er... wait...

Almost! Rasp

It get's to "Read Skins." and then crashes now.

Actually, I would guess that it wouldn't be able to do that, but I have no skins in my test model, so I think that's why it thinks it's able to load them. LOL

This is strange... the skins loader works, and it's basically no different that the texture coordinates fetching process. *scratches head* Huh

EDIT: I tried it with another model in which I placed skin data, and still claims it could load them. The error is now located in my malloc stuff, according to the debugger and not fread. Hmm.

Nothing looks wrong here, right?
Quote: texcoords = (texCoord_MD2 *)malloc(sizeof(texCoord_MD2) * header->num_TexCoords);
fseek(md2_open_fs, header->ofs_TexCoords, SEEK_SET);
fread(texcoords, sizeof(texCoord_MD2), header->num_TexCoords, md2_open_fs);

EDIT: Pasting it here caused me to see it... it should be a * not a comma. I'll try that...

EDIT: No, it was right.
Quote this message in a reply
Jones
Unregistered
 
Post: #9
Perhaps if I tried some sort of alternative, like calloc, or somthing... Annoyed

Oh fenris, I realized I forgot to thank you for your help, it was much appreciated. If I ever release/make anything with this, I'll stick your name in!
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #10
No problem, happy to help. Smile I'm in a hurry to catch a plane at the moment, but if your problem still persists when I get back, I'll give it another shot. Wink
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #11
Quote:Perhaps if I tried some sort of alternative, like calloc, or somthing...
For C++ you should use the keywords new and delete rather than malloc(), calloc() and free(). This is vital for classes if you want the constructor functions to run.
Quote this message in a reply
Jones
Unregistered
 
Post: #12
backslash Wrote:For C++ you should use the keywords new and delete rather than malloc(), calloc() and free(). This is vital for classes if you want the constructor functions to run.

Well, no memory allocation goes on in my constructor functions. And can new declare multiple instances, like malloc can do, for example:

Code:
bob = (int)malloc(sizeof(int) * 10)

//   Produces
bob[10];

What could my malloc stuff be doing wrong? (To the class, I mean.)

EDIT:

I've realised that the error is not in the memory allocation, it's in the reading. If I write this:

Code:
texcoords = (texCoord_MD2 *)malloc(sizeof(texCoord_MD2) * header->num_TexCoords);    
    cout << "Malloc tag: texcoords." << endl;
    fseek(md2_open_fs, header->ofs_TexCoords, SEEK_SET);
    cout << "Fseek tag: texcoords."    << endl;
    fread(texcoords, sizeof(texCoord_MD2), header->num_TexCoords, md2_open_fs);
    cout << "Read Texture coords."    << endl;

I'll get this back:

Quote:// Stuff from before the texcoords...
About to declare MD2...
Declared static MD2!
Read Header.
Read Skins.
// Texcoords...
Malloc tag: texcoords.
Fseek tag: texcoords.
// Failure!
OpenGL has exited due to signal 10 (SIGBUS).

Does the code I posted above seem wrong in any way?

EDIT 2: Forgot, the MD2 is not actually static, btw.

EDIT 3:

THEORY:

What needs to happen here, is for all this malloc junk to dissapear. Honestly, having to manage your own memory... what are we programming with here, fortran? Rasp No, I'll have to try something simpler... like declaring arrays with new, that are sized to the "num_" variables. That'll make things easier... I think.
Quote this message in a reply
Jones
Unregistered
 
Post: #13
Well, I stuck in some "new" commands, like this:

Code:
texcoords = new texCoord_MD2[header->num_TexCoords];

But I get std::bad_alloc errors.

Well, I have no idea what the problem is here. Annoyed

Maybe I'm trying to allocate too much memory, and it doesn't allocate all of it, then tries to read too much into not-enough memory? Huh
Quote this message in a reply
Jones
Unregistered
 
Post: #14
SOLVED!

It was my endian-convertor functions. I changed all my ints to uints, and now it works!

Now... to see if it will actually draw anything...
Quote this message in a reply
zhivaja
Unregistered
 
Post: #15
I have the same problem
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Sigsegv in AGL, but not GLUT Wheatie 9 4,699 Aug 8, 2002 08:21 PM
Last Post: Wheatie