Jump to content
  • Advertisement
Sign in to follow this  
ontheheap

Strange runtime error

This topic is 5464 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a tile map editor using WinAPI and I've ran into a nasty run time bug that I'm having trouble with. Everything has been working fine until I added the following code, which loads all bitmaps in a directory into a dynamic HBITMAP array. It works fine at first, but once I call it 5 or 6 times I get a memory address error. I have functions for saving/opening maps which work fine, but if I call them after calling the following function the app crashes with the same error. I know this is the function that is causing the problem, because I commented out the draw function and it still occurs. Right now I have both the Load_Tiles function and Draw_Tiles function commented out, and everything is working perfect.
int Load_Tiles(char * dir)
{
    WIN32_FIND_DATA Count;
    HANDLE hCount;
    
    WIN32_FIND_DATA FindFileData;
    HANDLE hData;
    
    static int totalBmpsInFile;
    totalBmpsInFile = 0;
    
    delete [] hbmp;
   
   // this figures out how many bitmaps are in the directory
   if(SetCurrentDirectory(dir))
   {
       hCount = FindFirstFile("*.bmp",&Count);
       totalBmpsInFile = 1;
       do {
           if(hCount == INVALID_HANDLE_VALUE)    
               break;
           totalBmpsInFile += 1;
       } while(FindNextFile(hCount, &Count));
       FindClose(hCount);
   }   
   
   hbmp = new HBITMAP[totalBmpsInFile-2]; // yay =)
   int i = 0;
   
   hData = FindFirstFile("*.bmp",&FindFileData);
   hbmp = (HBITMAP)LoadImage(hinst,FindFileData.cFileName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
       do {
           i+=1;
           FindNextFile(hData, &FindFileData);
           hbmp = (HBITMAP)LoadImage(hinst,FindFileData.cFileName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
       } while(i < totalBmpsInFile-2);
       FindClose(hData);
       
SetCurrentDirectory("..\\..\\"); // return to the main directory
RedrawWindow(hwndtile,NULL,NULL,RDW_UPDATENOW);
return totalBmpsInFile - 2;
}




It's strange because the tiles are loaded and displayed fine the first 5 or 6 times I call the function. Any help would be appreciated. - heap

Share this post


Link to post
Share on other sites
Advertisement
Hrm... well I thought I had it fixed but it seems to be very touchy. I've been playing with it a little bit and this is what it's doing right now:

As soon as I open the program, I create a new map and then save it to a file. I then create another new map, don't save it, and then re-open the other map to make sure it's working. Works fine.

- OR -

As soon as I open the program I go through the Tiles menu and open each tile directory to make sure that is working. I can open the tile's over and over again and the code handles it fine for a while and then crashes.

If I try to do both browsing through the tiles and saving/opening the program just closes.

Here is a zip file containing the source, dev-cpp project, tiles, and executable.

clicky

Perhaps someone can take a glance at it and see if anything jumps out at them. It would be greatly appreciated =)

Share this post


Link to post
Share on other sites
You must go through and call DeleteObject() for each bitmap in the array before delete[]ing it. The win32 API is basically aimed towards C, so there's no nice convenient destructor to tidy up for you.

Windows is a lot friendlier about excessive use of GDI resources than it used to be, but you'll still hit a limit at some point.

Share this post


Link to post
Share on other sites
Thanks for the reply.

I added the folling:


for(int f = 0; f < totalBmpsInFile;f++)
{
DeleteObject(hbmp[f]);
}


I'm still getting the same run time errors. I'm starting to think I would be better off rewriting the entire function to see if I can come up with a better solution. I still don't see why this one won't work though.

Share this post


Link to post
Share on other sites
That won't work since you're setting totalBmpsInFile to 0 each time around. You only want to initialize it once - static int totalBmpsInFile = 0;, then reset it after you've cleaned up the old stuff

Share this post


Link to post
Share on other sites
Thanks uavfun, but I gave that a shot too. I walked away from the entire thing for the last few hours and realized using vector would make it much easier. My app was using up about 7,000K of memory before using vector, now it's using about 1,200, and not crashing. It's still having a slight problem with loading tiles after calling open or save as, but this is a huge improvement =). Thanks again.

- heap

Share this post


Link to post
Share on other sites
I thought I'd update this thread since I found a better way to do what I was trying to do. First though, I definitely appreciate every bit of assistance I get on this site.

Originally, I was loading the bitmaps into the app each time I selected them from the menu. I realized how much of a mistake this was when I started writing the code to draw the tiles onto the map. Every time the tiles were changed the bitmaps were lost. So now I'm loading all of the tiles into vectors as soon as the app starts and everything is working perfectly!!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!