Sign in to follow this  
andrew_h

my program keeps crashing and i've spent many hours trying to fix it w/o any luck.

Recommended Posts

ok, so i've been working on my program to better optimize the model loader, and model collection. Well, when i built a new function to load a model collection, the entire thing crashes. I spent roughly 5 hours without any luck to find the problem. I basically put error checkers around every function, but anytime i thought i found the error, it wasn't truely the error, just something that failed because of the error. So could anyone help me out here? I also included the compiled file which doesn't like to work. some other things i found was that when i ran it as a debug, it worked fine, but when i ran it as a release it didn't. I think i might have made an error with a pointer or variable initialization, but i am unsure. [Edited by - andrew_h on January 4, 2008 11:55:19 PM]

Share this post


Link to post
Share on other sites
Quote:
i found was that when i ran it as a debug, it worked fine, but when i ran it as a release it didn't.


A very large majority of these cases is that you forgot to initialize a variable somewhere and then started messing with it. Check every one of your variables and make sure they are initialized.

Share this post


Link to post
Share on other sites
Ya, i read that when i searched google, but i spent about 2 hours checking variables before i gave up.

Also a note to everyone:
I just realized that for some reason, my program will 'forget' to delete something, so it will keep info in your RAM even after you exit.

Share this post


Link to post
Share on other sites
I did a quick test run of your code...

First, make sure you're checking the return values of all fopen() calls - if fopen() fails (usually because you've given it an incorrect file name), then it will return 0 (NULL).
Second, fgets() includes the newline character in the buffer that it returns, so if you want to use the line of text directly as a file name, you'll have to strip that off.

And finally, consider learning to use the standard C++ libraries instead of C libraries (std::string makes string handling much easier to use, and the iostream library can make it much easier to write safe file reading code). I won't say you must switch to these C++ libraries, because trying to learn too many things at once can be a problem in itself, but do consider it.

John B

Share this post


Link to post
Share on other sites
The problem I see is you are trying to load a file that does not exist. You read the file map.map, which gives you filenames and what not to other filenames. However, the first line is "test map" and then you call the LoadModel function which tries to load "test map" as a file and it does not exist. Anyway, if you add the following it will prevent these kind of errors:


void LoadModel(char *fname)
{
...variable decl...
//
FILE *filein;
char oneline[255];
filein = fopen(fname, "rt"); // File To Load World Data From
//
//--Add this---------------------------
if(!filein)
{
return;
}
//--Add this---------------------------
...
}




Anyway, I assume this is the name of the map and shouldn't be read in. Try this out:

void LoadMap()
{
//int numtriangles; <--NOT USED
int numobjects;
char oneline[255];
//
FILE *filein = fopen("data/map.map", "r+"); // File To Load World Data From
//
//--Add this---------------------------
readstr(filein, oneline); // <--The name of the map file (ignore it)
//--Add this---------------------------
//
readstr(filein, oneline); // Reads the entire map model
sectorcollection.sectors[0].LoadModel(oneline); // Loads the entire map model
//
readstr(filein,oneline); // Reads the number of objects
sscanf(oneline, "%f", &numobjects); // Sets the number of objects used in the map
for(int i=0; i<numobjects; i++) // Loads each model into the structure
{
sectorcollection.sectors[i].LoadModel(oneline);
}
fclose(filein);
}






Also, this will fix your current problem but there is another. Once it goes into loading the models it appears to get stuck in a infinite loop.

Share this post


Link to post
Share on other sites
Yes! i found the problem! when my program loads the number of triangles, it loads a really wierd (and long (like 12 characters long)) number for the number of triangles, and eventually crashes on memory error.

EDIT: w00t, finally fixed it.

[Edited by - andrew_h on January 5, 2008 1:52:58 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by andrew_h
Ya, i read that when i searched google, but i spent about 2 hours checking variables before i gave up.

Also a note to everyone:
I just realized that for some reason, my program will 'forget' to delete something, so it will keep info in your RAM even after you exit.


It's called a memory leak, and even if your program leaks memory, that really doesn't matter. The OS has allocated a section of memory to your program and then your program allocates it's own memory from those OS allocated sections, so the OS always knows what memory is allocated to your program, so when the program exits, the OS just releases all that memory.

The reason we look for memory leaks is that they could cause problems during the running of the program, which would end up with the program trying to have too much memory allocated to it (which would cause it to crash).

But memory allocated to your program does not stay allocated or "missing" after your program exits. The OS takes care of it.

Share this post


Link to post
Share on other sites
Quote:
Original post by andrew_h
hmmm, try saying that when the program 'forgot' to delete 700mb of memory when it exited, and then my computer ran insanely slow until i restarted it.


Sure. Here's my suggestion of why that might be: you're allocating 700MB of memory. That's a decent chunk, even with 2GB of RAM. Depending on your usage patterns, this might force quite a lot of stuff to page to disk. It might then take a while for the operating system's heuristics to decide to page that stuff back in (varies depending on operating system, obviously), so switching back to tasks that you minimized before starting your program might be very sluggish at first.

Share this post


Link to post
Share on other sites
Quote:
Original post by andrew_h
hmmm, try saying that when the program 'forgot' to delete 700mb of memory when it exited, and then my computer ran insanely slow until i restarted it.


Use an OS written in the last few decades.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this