Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

100 Neutral

About RichardForgacs

  • Rank
  1. RichardForgacs

    Memory Heap Problem

    I've still problems with my code, but it's working more or less. When I try to use the "buffer[bytesRead] = '\0';" expression, the objects don't appear in the GL window. The current version of my "working" code uses this expression: "buffer[bytesRead-5] = '\0';" . Yes, I know it's not a good code. I loss data with this, but the end of the .ASE files don't contain useful informations(at least for me). I'm working on the movement now, but I'll try to correct the code. I'll thank every helpful replies in the future, you helped a lot! Thanks!
  2. RichardForgacs

    Memory Heap Problem

    This is the catch 22 of trying to write code samples in book form. Space is always limited and you want the sample to be as concise and on topic as possible. The first victim, comments ( which makes sense as the book you are reading should in fact be the comments ), the second... error checking. This is even more true in Java books where error checking can be a bit more... verbose. In many ways, its a problem with no easy solution. Well except I suppose to use websites instead of books. [/quote] I agree with you whole heartedly. This day and age, I think books are (becoming) obsolete; especially when it comes to programming information. But I digress; I haven't read many programming related books to know the general pattern of writing, so I'll get off my high pony. For the OP; I suggest you research and implement a simple logging class for yourself, send me a PM if you'd like some help. Also research your functions/classes and understand their return values. Then if something fails, write it out in english (or your preferred language) to a file you can check. (i.e. "[date-time]: (function-name) (function could not read file [filename])"). This will save you many hours of headache. :-) [/quote] I'm very beginner in programming. I'm just looking for a point where I can start from. I'm really interested in graphics ( not really game development, just for fun) and I found this piece of code in the book. I just wanted an .ASE loader to my code. Now it's working perfectly, thanks for your help!
  3. RichardForgacs

    Memory Heap Problem

    You still suffer from uninitialized data at the end of buffer. As BeerNuts said the strcat function will work its way through the buffer looking for the first zero byte it finds, and start appending at that point. In other words, it assumes that all strings are null terminated. The problem here is that the fread function does not append a zero byte at the end of the buffer the way strcpy and strcat does. This means that when the strcat function is looking for that trailing zero it may continue past the end of the buffer looking for it, and once it happens to find a zero somewhere past the end of buffer, and writes to that memory, the OS will interrupt your program and spit out a error message typically containing words like Access, Violation, Memory and the like. You can fix this either by filling buffer with zeros before using it. memset((void*)buffer, 0, sizeof(buffer)); or you can add a zero at the end manually after reading the file with fread. In the latter case the return value from fread comes in handy, as Fekete mentioned. buffer[bytesRead] = '\0'; strcat(buffer, " EOF"); ... [/quote] Thank you very much! That was the problem! Now it's working perfectly without debugging! Thanks for the other answers too!
  4. RichardForgacs

    Memory Heap Problem

    Thanks for the answers, but the program still crashers.
  5. RichardForgacs

    Memory Heap Problem

    The new code looks liek this: buffer = new char[filesize+5]; fseek(file,0,SEEK_SET); fread(buffer,1,filesize,file); fclose(file); strcat(buffer," EOF"); I think it's correct now, but maybe I'm wrong. Any other ideas?
  6. RichardForgacs

    Memory Heap Problem

    Thanks a lot, that was also a mistake from me in the code. The role of this piece of [color="#1c2837"]code ([font=Consolas,]strcat(buffer," EOF"); ) is to mark the end of the file and i can found it easily in the loop. But the program still craches when I try to run it without debugging. [/font]
  7. RichardForgacs

    Memory Heap Problem

    Hi! I have a strange problem with a part of my code. I wrote an .ASE file loader to my application with the help of a book, but when I try to start my code without Debuging, it says that: "Unhandled exception at 0x77a737b7 in gl5.exe: 0xC0000374: A heap has been corrupted." I started to debug my code and i found stranges errors :S Most of the error messages are: "CXX0030: Error: expression cannot be evaluated". It's about pointers which i was deleted before. I tried everything, but can't find the problem. I need your help! Here's my code: ase.cpp: #include <iostream> #include <stdio.h> #include "ase.h" using namespace std; MODEL::MODEL(){mdl_objects = NULL;max_objects = 0;} MODEL::~MODEL(){} int countStrings (char *buffer, char *szo); bool MODEL::Load(char * name){ FILE * file = NULL; long filesize; char *buffer; char *tmp,token[] = " \\\":\t\r\n"; long cur_object = -1; Release(); file = fopen(name,"rb"); fseek(file,0,SEEK_END); filesize = ftell(file); //fájl mérete buffer = new char[filesize+5]; strcat(buffer," EOF"); fseek(file,0,SEEK_SET); fread(buffer,1,filesize,file); fclose(file); max_objects = countStrings(buffer,"*GEOMOBJECT"); mdl_objects = new MODEL_OBJECT[max_objects+1]; tmp = strtok(strdup(buffer),token); while(strcmp(tmp,"EOF")){ if(strcmp(tmp,"*GEOMOBJECT") == 0){ cur_object++; mdl_objects[cur_object].max_vertices = 0; mdl_objects[cur_object].max_triangles = 0; //mdl_objects[cur_object].max_uv_coord = 0; mdl_objects[cur_object].vertex = NULL; mdl_objects[cur_object].triangle = NULL; //mdl_objects[cur_object].uv_coord = NULL; mdl_objects[cur_object].max_facenormal = 0; mdl_objects[cur_object].face_normal = NULL; } else if(strcmp(tmp,"*MESH_NUMVERTEX") == 0) { mdl_objects[cur_object].max_vertices = strtol(strtok(NULL,token),NULL,10); mdl_objects[cur_object].vertex = new MODEL_VERTEX[mdl_objects[cur_object].max_vertices+1]; } else if(strcmp(tmp,"*MESH_NUMFACES") == 0){ mdl_objects[cur_object].max_triangles = strtol(strtok(NULL,token),NULL,10); mdl_objects[cur_object].max_facenormal = mdl_objects[cur_object].max_triangles; mdl_objects[cur_object].triangle = new MODEL_TRIANGLE[mdl_objects[cur_object].max_triangles+1]; mdl_objects[cur_object].face_normal = new MODEL_FACE_NORMAL[mdl_objects[cur_object].max_facenormal+1]; } else if(strcmp(tmp,"*MESH_VERTEX") == 0){ long cur_vertex = strtol(strtok(NULL,token),NULL,10); sscanf(strtok(NULL,token),"%f",&mdl_objects[cur_object].vertex[cur_vertex].xyz[0]); sscanf(strtok(NULL,token),"%f",&mdl_objects[cur_object].vertex[cur_vertex].xyz[2]); sscanf(strtok(NULL,token),"%f",&mdl_objects[cur_object].vertex[cur_vertex].xyz[1]); } else if(strcmp(tmp,"*MESH_FACE") == 0){ long cur_tri = strtol(strtok(NULL,token),NULL,10); strtok(NULL,token); mdl_objects[cur_object].triangle[cur_tri].point[0] = strtol(strtok(NULL,token),NULL,10); strtok(NULL,token); mdl_objects[cur_object].triangle[cur_tri].point[1] = strtol(strtok(NULL,token),NULL,10); strtok(NULL,token); mdl_objects[cur_object].triangle[cur_tri].point[2] = strtol(strtok(NULL,token),NULL,10); } /*else if(strcmp(tmp,"*MESH_NUMTVERTEX") == 0){ mdl_objects[cur_object].max_uv_coord = strtol(strtok(NULL,token),NULL,10); mdl_objects[cur_object].uv_coord = new MODEL_UV_COORD[mdl_objects[cur_object].max_uv_coord+1]; } else if(strcmp(tmp,"*MESH_TVERT") == 0){ long cur_tver = strtol(strtok(NULL,token),NULL,10); sscanf(strtok(NULL,token),"%f",&mdl_objects[cur_object].uv_coord[cur_tver].uv[0]); sscanf(strtok(NULL,token),"%f",&mdl_objects[cur_object].uv_coord[cur_tver].uv[1]); }*/ else if(strcmp(tmp,"*MESH_FACENORMAL") == 0){ long cur_fnormal = strtol(strtok(NULL,token),NULL,10); sscanf(strtok(NULL,token),"%f",&mdl_objects[cur_object].face_normal[cur_fnormal].xyz[0]); sscanf(strtok(NULL,token),"%f",&mdl_objects[cur_object].face_normal[cur_fnormal].xyz[1]); sscanf(strtok(NULL,token),"%f",&mdl_objects[cur_object].face_normal[cur_fnormal].xyz[2]); } tmp = strtok(NULL,token); } delete [] buffer; buffer = NULL; return true; } int countStrings (char *buffer, char *szo){ char token[] = " \\\":\t\r\n"; char *ptr = strtok(strdup(buffer),token); int i = 0; while (strcmp(ptr,"EOF")){ if(strcmp(ptr,szo) == 0) i++; ptr = strtok(NULL,token); } return i; } void MODEL::Release(){ if( max_objects > 0){ for(long obj = 0; obj < max_objects; obj++){ if(mdl_objects[obj].max_vertices > 0) delete [] mdl_objects[obj].vertex; if(mdl_objects[obj].max_vertices > 0) delete [] mdl_objects[obj].triangle; if(mdl_objects[obj].max_facenormal > 0) delete [] mdl_objects[obj].face_normal; // if(mdl_objects[obj].max_vertices > 0) delete [] mdl_objects[obj].uv_coord; } delete [] mdl_objects; mdl_objects = NULL; max_objects = 0; } } ase.h /*struct MODEL_MATERIAL{ char *file_name; long tex_id; };*/ struct MODEL_VERTEX{ float xyz[3]; }; /*struct MODEL_UV_COORD{ float uv[5]; };*/ struct MODEL_TRIANGLE{ long point[3]; long uv_point[3]; }; struct MODEL_FACE_NORMAL{ float xyz[3]; }; struct MODEL_OBJECT{ long max_triangles; long max_facenormal; long max_vertices; //long max_uv_coord; //long material; MODEL_FACE_NORMAL *face_normal; MODEL_VERTEX *vertex; MODEL_TRIANGLE *triangle; //MODEL_UV_COORD *uv_coord; }; class MODEL{ public: MODEL(); ~MODEL(); MODEL_OBJECT *mdl_objects; long max_objects; bool Load(char *file_name); void Release(); };
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!