943,816 Members | Top Members by Rank

Ad:
Mar 13th, 2008
0

glutMouseFunc and Segmentation Faults

Expand Post »
Hello, everyone.
I'm having some trouble getting
c++ Syntax (Toggle Plain Text)
  1. glutMouseFunc
to work.

c++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bushido Hacks is offline Offline
14 posts
since Feb 2008
Mar 13th, 2008
0

Re: glutMouseFunc and Segmentation Faults

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.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Mar 13th, 2008
0

Re: glutMouseFunc and Segmentation Faults

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bushido Hacks is offline Offline
14 posts
since Feb 2008
Mar 13th, 2008
0

Re: glutMouseFunc and Segmentation Faults

>_< oops, sorry;

$ gdb ./hex3
(gdb) run
..wait for error..
(gdb) backtrace
Last edited by MattEvans; Mar 13th, 2008 at 4:14 pm.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Mar 13th, 2008
0

Re: glutMouseFunc and Segmentation Faults

That helps. Turns out BezierGL::plot() is a fault.

I guess I can show some of my code.
c++ Syntax (Toggle Plain Text)
  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. };

c++ Syntax (Toggle Plain Text)
  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)

c++ Syntax (Toggle Plain Text)
  1. void BezierGL::plot(int n) const {PathGL::plot(n);};
Last edited by Bushido Hacks; Mar 13th, 2008 at 4:39 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bushido Hacks is offline Offline
14 posts
since Feb 2008
Mar 13th, 2008
0

Re: glutMouseFunc and Segmentation Faults

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 4:38 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bushido Hacks is offline Offline
14 posts
since Feb 2008
Mar 13th, 2008
0

Re: glutMouseFunc and Segmentation Faults

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bushido Hacks is offline Offline
14 posts
since Feb 2008
Mar 13th, 2008
0

Re: glutMouseFunc and Segmentation Faults

Fixed it. Turns Out I was using the wrong function in my mouse function.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bushido Hacks is offline Offline
14 posts
since Feb 2008
Mar 13th, 2008
0

Re: glutMouseFunc and Segmentation Faults

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.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Mar 13th, 2008
0

Re: glutMouseFunc and Segmentation Faults

Quote ...
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 ).
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Game Development Forum Timeline: Web-based RPG game advice needed
Next Thread in Game Development Forum Timeline: Game server hosting ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC