Hi. I am developing a 3d application in SuSE Linux 9.0 in C++. I ported the application in WinXP using VStudio2003. The fun stuff is that the application executes in Linux but it crashes immediatelly in WinXP. I think i found what is causing the segfault in windows. I have the following classes (which are bad-coded by the way ;) : class Vertex3d { public: double x,y,z; long num; Vertex3d() { x = 0; y = 0; z = 0; num = 0; } /* Creates a new instance of Vertex3d */ Vertex3d(double x_,double y_,double z_,long num) { x = x_; y = y_; z = z_; this->num = num; } int draw() { glBegin(GL_POINTS); glVertex3d(x, y, z); glEnd(); return 0; } int draw(double px,double py,double pz) { glBegin(GL_POINT) glVertex3d(px, py, pz); glEnd(); return 0; } double distance(Vertex3d t) { return sqrt(pow(((Vertex3d)t).x - x , 2) + pow(((Vertex3d)t).y - y , 2) + pow(((Vertex3d)t).z - z , 2)); } }; #include "Vertex3d.h" class Cone { public: GLuint selectionName; bool isSelected; Vertex3d bottomCentre; double bottomRadius; Vertex3d topCentre; double topRadius; float* vertices1; float* vertices2; float* Upper; float* Lower; float *table; Cone() { ; } Cone(Vertex3d bottomCentre, double bottomRadius, Vertex3d topCentre, double topRadius) { this->bottomCentre = bottomCentre; this->bottomRadius = bottomRadius; this->topCentre = topCentre; this->topRadius = topRadius; selectionName = 0; isSelected = false; vertices1 = (float *)malloc( sizeof(float) * 2 * (30 + 1) ); vertices2 = (float *)malloc( sizeof(float) * 2 * (30 + 1) ); Upper = (float *)malloc( sizeof(float) * 3 * (30 + 1) ); Lower = (float *)malloc( sizeof(float) * 3 * (30 + 1) );; } }; In the begining of the main.cpp i do the following: float x=2.0, y=6.0f, z=1.5; Cone cone1(Vertex3d(10*x, 0, 10*z, 100), 1, Vertex3d(10*x, 0, 10*z+5, 2), 0.00); Cone cone2(Vertex3d(9*x, 0, 9*z, 100), 0.7, Vertex3d(9*x, 0, 9*z+3, 2), 0.00); Cone cone3(Vertex3d(9*x, 0, 11*z, 100), 0.7, Vertex3d(9*x, 0, 11*z+3, 2), 0.00); Cone cone4(Vertex3d(11*x, 0, 9*z, 100), 0.7, Vertex3d(11*x, 0, 9*z+3, 2), 0.00); Cone cone5(Vertex3d(11*x, 0, 11*z, 100), 0.7, Vertex3d(11*x, 0, 11*z+3, 2), 0.00); I think that the previous declarations cause my app to crash in WinXP. Why does this happens in WinXP although the app runs in Linux? thanks.
On Mon, 28 Jun 2004, Filippos Papadopoulos wrote:
Hi. I am developing a 3d application in SuSE Linux 9.0 in C++. I ported the application in WinXP using VStudio2003. The fun stuff is that the application executes in Linux but it crashes immediatelly in WinXP. I think i found what is causing the segfault in windows. I have the following classes (which are bad-coded by the way ;) :
......
Sorry, this is the full Cone class: #include "Vertex3d.h" class Cone { public: GLuint selectionName; bool isSelected; Vertex3d bottomCentre; //to kentro ths bashs double bottomRadius; Vertex3d topCentre; //to kentro ths koryfhs double topRadius; float* vertices1; float* vertices2; float* Upper; float* Lower; float *table; Cone() { ; } Cone(Vertex3d bottomCentre, double bottomRadius, Vertex3d topCentre, double topRadius) { this->bottomCentre = bottomCentre; this->bottomRadius = bottomRadius; this->topCentre = topCentre; this->topRadius = topRadius; selectionName = 0; isSelected = false; vertices1 = (float *)malloc( sizeof(float) * 2 * (30 + 1) ); vertices2 = (float *)malloc( sizeof(float) * 2 * (30 + 1) ); Upper = (float *)malloc( sizeof(float) * 3 * (30 + 1) ); Lower = (float *)malloc( sizeof(float) * 3 * (30 + 1) );; } void draw() { //dhmiourgia baseon drawCircle(1, vertices1, bottomRadius, 8, bottomCentre.x, bottomCentre.y, bottomCentre.z); drawCircle(2, vertices2, topRadius, 8, topCentre.x, topCentre.y, topCentre.z); //dhmiourgia pleyron for(GLuint k=0; k < 8 ;k++) { glColor3d(0.51,0.91,1.0); glBegin(GL_QUADS);glTexCoord2f(1.0f,1.0f); glVertex3f( Upper[(k+0)*3+0 ] , Upper[ (k+0)*3+1 ] , Upper[ (k+0)*3+2 ] ); glTexCoord2f(0.0f,1.0f); glVertex3f( Lower[(k+0)*3+0 ] , Lower[ (k+0)*3+1 ] , Lower[ (k+0)*3+2 ] ); glTexCoord2f(1.0f,0.0f); glVertex3f( Lower[(k+1)*3+0 ] , Lower[ (k+1)*3+1 ] , Lower[ (k+1)*3+2 ] ); glTexCoord2f(0.0f,0.0f); glVertex3f( Upper[(k+1)*3+0 ] , Upper[ (k+1)*3+1 ] , Upper[ (k+1)*3+2 ] ); glEnd(); } } void drawCircle(int l, float *vertices, GLdouble radius, GLint slices, double cx, double cy, double cz) { float angle = (float) M_PI / (float) slices * 2.0f; GLint i; if(l==1) table=Upper; else if(l==2) table=Lower; /* Ari8mos shmeion = 'slices' . ayta briskontai pano se enan kyklo */ for( i=0; i<slices+1; i++ ) { vertices[ i*2 + 0 ] = cos( angle * i ); vertices[ i*2 + 1 ] = sin( angle * i ); } /* Bash tou konou */ for( i=0; i<slices; i++ ) { glColor3d(0.3 + 0.007*i, 0.3 + 0.007*i , 0.3 + 0.007*i); glBegin( GL_POLYGON); glNormal3f(cx * vertices[ (i+0)*2+0 ] * radius, cy +vertices[ (i+0)*2+1 ] * radius, 2*cz ); glVertex3f(cx + vertices[ (i+0)*2+0 ] * radius, cy +vertices[ (i+0)*2+1 ] * radius, cz ); table[(i+0)*3+0] = cx + vertices[ (i+0)*2+0 ] * radius; table[(i+0)*3+1] = cy + vertices[ (i+0)*2+1 ] * radius; table[(i+0)*3+2] = cz; glNormal3f(cx * vertices[ (i+1)*2+0 ] * radius, cy *vertices[ (i+1)*2+1 ] * radius, 2*cz ); glVertex3f(cx + vertices[ (i+1)*2+0 ] * radius, cy +vertices[ (i+1)*2+1 ] * radius, cz ); table[(i+1)*3+0] = cx + vertices[ (i+1)*2+0 ] * radius; table[(i+1)*3+1] = cy + vertices[ (i+1)*2+1 ] * radius; table[(i+1)*3+2] = cz; glNormal3f( cx, cy, cz ); glVertex3f( cx, cy, cz ); table[(i+2)*3+0] = cx; table[(i+2)*3+1] = cy; table[(i+2)*3+2] = cz; glNormal3f( 1, 1, 1 ); glEnd(); } } };
On Mon, 28 Jun 2004 05:55:16 -0500 (EST) Filippos Papadopoulos <filip@cs.uoi.gr> wrote:
Hi. I am developing a 3d application in SuSE Linux 9.0 in C++. I ported the application in WinXP using VStudio2003. The fun stuff is that the application executes in Linux but it crashes immediatelly in WinXP. I think i found what is causing the segfault in windows. I have the following classes (which are bad-coded by the way ;) :
I don't see anything off the top of my head, but have you tried debugging it under the VC Debugger except that in the Cone class you are using Malloc() in the constructor: vertices1 = (float *)malloc( sizeof(float) * 2 * (30 + 1) ); vertices2 = (float *)malloc( sizeof(float) * 2 * (30 + 1) ); Upper = (float *)malloc( sizeof(float) * 3 * (30 + 1) ); Lower = (float *)malloc( sizeof(float) * 3 * (30 + 1) );; You should use the new operator. Here is an example: vertices1 = new float[2 * (30 + 1)]; I don't think this is your problem, but malloc(3) should not be used in C++ unless you have no other choice. Remember that malloc(3) is a C runtime library function (and you should include <cstdlib>). new is an intrinsic C++ operator. Also, your public class variables are not initialized in the case where the empty argument Cone constructor is called, but that would possibly cause you trouble in Linux as well as Windows. -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
On Mon, 28 Jun 2004, Jerry Feldman wrote:
On Mon, 28 Jun 2004 05:55:16 -0500 (EST) Filippos Papadopoulos <filip@cs.uoi.gr> wrote:
Hi. I am developing a 3d application in SuSE Linux 9.0 in C++. I ported the application in WinXP using VStudio2003. The fun stuff is that the application executes in Linux but it crashes immediatelly in WinXP. I think i found what is causing the segfault in windows. I have the following classes (which are bad-coded by the way ;) :
I don't see anything off the top of my head, but have you tried debugging it under the VC Debugger except that in the Cone class you are using Malloc() in the constructor: vertices1 = (float *)malloc( sizeof(float) * 2 * (30 + 1) ); vertices2 = (float *)malloc( sizeof(float) * 2 * (30 + 1) ); Upper = (float *)malloc( sizeof(float) * 3 * (30 + 1) ); Lower = (float *)malloc( sizeof(float) * 3 * (30 + 1) );;
You should use the new operator. Here is an example: vertices1 = new float[2 * (30 + 1)];
I don't think this is your problem, but malloc(3) should not be used in C++ unless you have no other choice. Remember that malloc(3) is a C runtime library function (and you should include <cstdlib>). new is an intrinsic C++ operator.
Also, your public class variables are not initialized in the case where the empty argument Cone constructor is called, but that would possibly cause you trouble in Linux as well as Windows. --
Well i found out the solution. In the begining of the main.cpp i do the following in Linux: float x=2.0, y=6.0f, z=1.5; Cone cone1(Vertex3d(10*x, 0, 10*z, 100), 1, Vertex3d(10*x, 0, 10*z+5, 2),0.00); Cone cone2(Vertex3d(9*x, 0, 9*z, 100), 0.7, Vertex3d(9*x, 0, 9*z+3, 2),0.00); ... In VC++ i did: float x=2.0, y=6.0f, z=1.5; Cone cone1(*(new Vertex3d(10*x, 0, 10*z, 100)), 1, *(new Vertex3d(10*x, 0, 10*z+5, 2)),0.00); Cone cone2(*(new Vertex3d(9*x, 0, 9*z, 100)), 0.7, *(new Vertex3d(9*x, 0, 9*z+3, 2)),0.00); ... and it worked... Maybe gcc is a bit "smarter" (?) with the initializations...
Well i found out the solution. In the begining of the main.cpp i do the following in Linux: float x=2.0, y=6.0f, z=1.5; Cone cone1(Vertex3d(10*x, 0, 10*z, 100), 1, Vertex3d(10*x, 0, 10*z+5, 2),0.00); Cone cone2(Vertex3d(9*x, 0, 9*z, 100), 0.7, Vertex3d(9*x, 0, 9*z+3, 2),0.00); ...
In VC++ i did: float x=2.0, y=6.0f, z=1.5; Cone cone1(*(new Vertex3d(10*x, 0, 10*z, 100)), 1, *(new Vertex3d(10*x, 0, 10*z+5, 2)),0.00); Cone cone2(*(new Vertex3d(9*x, 0, 9*z, 100)), 0.7, *(new Vertex3d(9*x, 0, 9*z+3, 2)),0.00); ... and it worked...
Maybe gcc is a bit "smarter" (?) with the initializations...
I don't think so. You probably has a problem in the Vertex3d copy constructor. The code above (VC++) is creating a memory leak, and is wrong. Everything you create with new must be destroyed with delete. Since you are using dereference operator (*) you are losing the pointer to the object, and you cannot delete anymore (at least not with some unusual tricks). Try doing a Vertex3d a; Vertex3d b = a and see if it works. []s Davi de Castro Reis
On Tue, 29 Jun 2004 09:07:28 -0300 Davi de Castro Reis <davicastro@terra.com.br> wrote:
Maybe gcc is a bit "smarter" (?) with the initializations...
I don't think so. You probably has a problem in the Vertex3d copy constructor.
I think I agree with Davi here, but another comment on automatic and allocated variables : Automatic and malloc'd variables are not initialized. it is very possible that in the Linux environment, the garbage you picked up might have been benign where under VC, the garbage might have been bad. Example: #include <iostream> using std::cout; int main() { double x; cout << x << '\n'; } In the above case, x could contain anything from 0.0 through any valid value, and possibly some invalid value, and cause a floating point exception. It depends solely on what the C run time library does before main is called. Same with Visual C. Class variables are likewise not initialized unless you explicitly do so in a constructor. One issue that Davi picked up was your coding style. Cone cone1(*(new Vertex3d(10*x, 0, 10*z, 100)), 1, *(new Vertex3d(10*x, 0, 10*z+5, 2)),0.00); In addition to the memory leak issue, you are making the code hard to read. One of the oldest rules I try to use is to maintain the readability of my code. Maybe it comes from writing an old program in PDP-8 assembler, putting it down for finals, and after finals, picking it up and finding it was impossible for me to read. Additionally, your expressions, such as 10 * x are going to be evaluated at run time unless the compiler can optimize them. It might be better: const float x=2.0, y=6.0f, z=1.5; // Now these are constants and therefore, 10 * x is a constant expression evaluated at compile time. Some programmers might also prefer to use 10.0 * x, but todays compilers will generate the right code. -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
participants (3)
-
Davi de Castro Reis
-
Filippos Papadopoulos
-
Jerry Feldman