glutMouseFunc and Segmentation Faults

Thread Solved

Join Date: Feb 2008
Posts: 14
Reputation: Bushido Hacks is an unknown quantity at this point 
Solved Threads: 0
Bushido Hacks's Avatar
Bushido Hacks Bushido Hacks is offline Offline
Newbie Poster

glutMouseFunc and Segmentation Faults

 
0
  #1
Mar 13th, 2008
Hello, everyone.
I'm having some trouble getting
  1. glutMouseFunc
to work.

  1. #include <GL/glut.h> //GL/glut.h includes GL/gl.h and GL/glu.h so there is no need to declare them
  2. #include <ctime> // std::time
  3. #include <cstdlib> // std::srand
  4. #include "./colorgl.h" // contains my ColorGL class that handles colors.
  5. #include "./pathgl.h" // contains my NodeGL, PathGL, BezierGL, and HermiteGL classes
  6.  
  7. float winDX, winDY;
  8. int winWidth = 600;
  9. int winHeight = 400;
  10. int clxct = 0; // number of clicks
  11.  
  12. ColorGL bgl, pen; // background color (default is set to black), and foreground color.
  13. BezierGL bzc, bzcr, bzru; // Three BezierGL objects. BezierGL is a subclass of PathGL
  14.  
  15. void reshapeFcn(int dx, int dy){
  16. float aspectRatio;
  17. if(dy == 0){dy = 1;} // prevent divbyzero
  18. /* void glViewport(int x, int y, int width, int height); */
  19. glViewport(0,0,dx,dy); // set viewport to window dimensions
  20. // reset coordinate system
  21. glMatrixMode(GL_PROJECTION);
  22. glLoadIdentity();
  23. // establish clipping volumne (left,right,bottom,top,near,far)
  24. aspectRatio = static_cast<float>(dx)/static_cast<float>(dy);
  25. if(dy <= dx)
  26. {
  27. winDX = 200;
  28. winDY = 200 / aspectRatio;
  29. }
  30. else
  31. {
  32. winDX = 200 * aspectRatio;
  33. winDY = 200;
  34. };
  35. glOrtho(-winDX,winDX,-winDY,winDY,1.0,-1.0);
  36. glMatrixMode(GL_MODELVIEW);
  37. glLoadIdentity();
  38. };
  39.  
  40. void displayFcn()
  41. {
  42. glClear(GL_COLOR_BUFFER_BIT);
  43. pen.pick(bgl).use(); // pick a color except the background color and use it as the foreground color.
  44. glPointSize(3.0); // Set the point size to 3.0 pixels
  45. glutSwapBuffers(); // Flush and execute
  46. }
  47.  
  48. /*
  49. void timerFunc(int val)
  50. {
  51.  // Animation would go here, but it is also good for getting the keyboard functions to work.
  52.  glutPostRedisplay();
  53.  glutTimerFunc(33,timerFunc,1);
  54. }
  55. */
  56.  
  57. /* This is working. */
  58. void keyFcn( unsigned char key, int x, int y )
  59.  
  60. {
  61.  
  62. switch(key)
  63.  
  64. {
  65. case 'c': pen.pick(bgl).use(); break; // pick a new color
  66. case 'q':
  67.  
  68. case 27: std::exit(0); // ESCape to quit
  69.  
  70. default: break;
  71.  
  72. };
  73.  
  74. };
  75.  
  76. /* This is correct but it is not working. */
  77. void mouseFcn(int button, int action, int x, int y)
  78. {
  79. if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
  80. {
  81. if(clxct < 4) // If less than four points are ploted, plot a point.
  82. {
  83. bzc.set(clxct,x,y); // y = winHeight - y in windows
  84. glBegin(GL_POINTS);
  85. bzc.plot(clxct);
  86. glEnd();
  87. clxct++;
  88. }
  89. else{ // Otherewise plot a path.
  90. pen.pick(bgl).use(); // the color to draw the line.
  91. glPointSize(1.0);
  92. glBegin(GL_LINE_STRIP);
  93. bzc.plot(clxct);
  94. glEnd();
  95. pen.pick(bgl).use(); // the color to draw the next set of points
  96. glPointSize(3.0);
  97. clxct = 0;
  98. }
  99. }
  100. };
  101.  
  102. void init()
  103. {
  104. bgl.useClear(); // glClearColor(0.0,0.0,0.0,1.0);
  105. };
  106.  
  107. int main(int argc, char** argv)
  108. {
  109. std::srand(std::time(NULL)); // Random number generator (RNG)
  110. glutInit(&argc,argv);
  111. glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
  112. glutInitWindowPosition(100,100); // left, top
  113. glutInitWindowSize(winWidth,winHeight);
  114. glutCreateWindow(argv[0]); // Create a new window with the name of the program as the title.
  115. glutDisplayFunc(displayFcn);
  116. glutReshapeFunc(reshapeFcn);
  117. glutKeyboardFunc(keyFcn);
  118. glutMouseFunc(mouseFcn);
  119. //glutTimerFunc(33,timerFunc,1);
  120. init();
  121. glutMainLoop();
  122. return 0;
  123. };

The program shuts down when I click and returns a
Segmentation fault
message.
What is causing this to happen?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: glutMouseFunc and Segmentation Faults

 
0
  #2
Mar 13th, 2008
There is probably something wrong in your not-shown classes ColorGL &| BezierGL, since the code runs fine with all reference to those classes commented.

Have you got a debugger? I'm guessing if your PC is saying 'Segmentation fault' that you're using some breed of Linux? If so, run your application from the command like like this...
gdb ./a.out ...and when it segfaults, do...
backtrace ...and you'll have a much better idea where your error is ( since gdb is an interactive debugger, and 'backtrace' gives you the callstack before the error ).

If you're not on Linux.. er.. dno, what are you on? I've never seen Windows say 'Segmentation fault'... Visual Studio has an integrated debugger, but you'll have to work out how to use it yourself.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 14
Reputation: Bushido Hacks is an unknown quantity at this point 
Solved Threads: 0
Bushido Hacks's Avatar
Bushido Hacks Bushido Hacks is offline Offline
Newbie Poster

Re: glutMouseFunc and Segmentation Faults

 
0
  #3
Mar 13th, 2008
The program name is hex3
$ g++ -c hex3.cpp -o hex3.o
$ g++ -Wl--start-group hex3.o colorgl.o pathgl.o -lglut -Wl--end-group -o hex3
$ ./hex3

It is at this point that I click something and...

Segmentation fault
$ gdb ./hex3
GNU gdb Red Hat Linux (6.6-16.fc7rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu"...
(no debugging symbols found)
Using host libthread_db library "/lib64/libthread_db.so.1".
(gdb) backtrace
No stack.
(gdb) quit
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: glutMouseFunc and Segmentation Faults

 
0
  #4
Mar 13th, 2008
>_< oops, sorry;

$ gdb ./hex3
(gdb) run
..wait for error..
(gdb) backtrace
Last edited by MattEvans; Mar 13th, 2008 at 3:14 pm.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 14
Reputation: Bushido Hacks is an unknown quantity at this point 
Solved Threads: 0
Bushido Hacks's Avatar
Bushido Hacks Bushido Hacks is offline Offline
Newbie Poster

Re: glutMouseFunc and Segmentation Faults

 
0
  #5
Mar 13th, 2008
That helps. Turns out BezierGL::plot() is a fault.

I guess I can show some of my code.
  1. /* pathgl.h */
  2. //...
  3. class NodeGL
  4. {
  5. public:
  6. //...
  7. void plot() const;
  8. //...
  9. private:
  10. double *p;
  11. };
  12. //..
  13. class PathGL
  14. {
  15. public:
  16. //...
  17. virtual void plot() const;
  18. virtual void plot(int) const;
  19. //...
  20. private:
  21. NodeGL *p;
  22. int size;
  23. };
  24. //...
  25. class BezierGL : public PathGL
  26. {
  27. public:
  28. //...
  29. virtual void plot() const;
  30. virtual void plot(int) const;
  31. //...
  32. };

  1. /* pathgl.cpp */
  2. //...
  3. void NodeGL::plot() const
  4. {
  5. glVertex3d(p[0],p[1],p[2]);
  6. };
  7. //...
  8. void PathGL::plot() const
  9. {
  10. for(int i = 0; i < size; i++){p[i].plot();};
  11. };
  12. //...
  13. void PathGL::plot(int n) const {p[n].plot();};
  14. //...
  15. void BezierGL::plot() const {
  16. //..
  17. NodeGL tmp;
  18. //...
  19. tmp.plot();
  20. //...
  21. };
  22. //...
  23. void BezierGL::plot(int n) const {this->plot(n);};

Perhaps I should use this code instead for BezierGL::plot(int)

  1. void BezierGL::plot(int n) const {PathGL::plot(n);};
Last edited by Bushido Hacks; Mar 13th, 2008 at 3:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 14
Reputation: Bushido Hacks is an unknown quantity at this point 
Solved Threads: 0
Bushido Hacks's Avatar
Bushido Hacks Bushido Hacks is offline Offline
Newbie Poster

Re: glutMouseFunc and Segmentation Faults

 
0
  #6
Mar 13th, 2008
Modifying that function work. I managed to make my four points before Segmentation fault occurs. :-)

I think I can handle it from here. Thanks for the help.
Last edited by Bushido Hacks; Mar 13th, 2008 at 3:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 14
Reputation: Bushido Hacks is an unknown quantity at this point 
Solved Threads: 0
Bushido Hacks's Avatar
Bushido Hacks Bushido Hacks is offline Offline
Newbie Poster

Re: glutMouseFunc and Segmentation Faults

 
0
  #7
Mar 13th, 2008
OK, after replacing most instances of this->someMethod() in my BezierGL class, a new challenge appears.

$ gdb ./hex3
GNU gdb Red Hat Linux (6.6-16.fc7rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu"...
(no debugging symbols found)
Using host libthread_db library "/lib64/libthread_db.so.1".
(gdb) run
Starting program: /home/bushidohacks/newexamples/hex3 
(no debugging symbols found)
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7fff549fd000
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
---Type <return> to continue, or q <return> to quit---
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
---Type <return> to continue, or q <return> to quit---

Program received signal SIGSEGV, Segmentation fault.
0x00000000004050bb in NodeGL::plot ()
(gdb) backtrace
#0  0x00000000004050bb in NodeGL::plot ()
#1  0x0000000000405563 in PathGL::plot ()
#2  0x000000000040559f in BezierGL::plot ()
#3  0x0000000000401711 in mouseFcn ()
#4  0x0000003609c1f928 in glutMainLoopEvent () from /usr/lib64/libglut.so.3
#5  0x0000003609c1ffaa in glutMainLoop () from /usr/lib64/libglut.so.3
#6  0x0000000000401647 in main ()
(gdb) quit
The program is running.  Exit anyway? (y or n) y
$

What could it be now?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 14
Reputation: Bushido Hacks is an unknown quantity at this point 
Solved Threads: 0
Bushido Hacks's Avatar
Bushido Hacks Bushido Hacks is offline Offline
Newbie Poster

Re: glutMouseFunc and Segmentation Faults

 
0
  #8
Mar 13th, 2008
Fixed it. Turns Out I was using the wrong function in my mouse function.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: glutMouseFunc and Segmentation Faults

 
0
  #9
Mar 13th, 2008
Ok, the method NodeGL:plot is very small, the only thing that could be wrong in that method itself is the array not being instantiated ( or being too short ), however, there could be something wrong higher up ( the pointer 'this' can be 0 sometimes! ).

To get more useful info from the debugger, you need to add an option when compiling: recompile all of your files with the option -g3 to emit debugging information ( I hope that works on your G++ version, I'm using G++4 ).. after recompiling -- you need to recompile ALL of the files with this option, so not just the hex3.cpp file, but the file(s) for bezier, node, etc aswell -- after doing that, run the application with GDB again, and post the output of backtrace if you dont mind.. that will tell me ( and I can tell you ) whether or not it's an error within the code in the method NodeGL:plot, or an error higher in the call chain.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: glutMouseFunc and Segmentation Faults

 
0
  #10
Mar 13th, 2008
Fixed it. Turns Out I was using the wrong function in my mouse function.
Cool; good to know it's working

Disregard my last post; although, the -g3 flag is quite useful to remember ( with full debug info you can see all the values passed into methods aswell as the method names ).
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Game Development Forum


Views: 2822 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Game Development
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC