File Input Memory Problem

Started by
4 comments, last by shane1985 19 years, 4 months ago
I'm writing code to load my map files, and when I exit the program, I get about 150 2kb memory leaks in the debug window. FILE * map; map = fopen(filename.c_str(), "rb"); fscanf(map, "%d", &numtiles); BYTE * image = new BYTE[32*32*3*numtiles]; // when I comment this out, I get no memory leaks fread(image, sizeof(BYTE), 32*32*3*numtiles, map); // do some other stuff delete [] image; fclose(map); How do I remove these memory leaks? Thanks! -Shane
Advertisement
I don't see a memory leak in that code. Furthermore, I can't imagine how calling fread could possibly cause a memory leak. The problem probably lies somewhere else.

However, there is a small problem. fscanf and fread generally don't mix. fscanf parses the input (skipping spaces and stuff) and fread doesn't. fscanf is used for reading text and fread is used for reading binary data.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Yeah, I don't understand either - I mean, I delete the image data after I'm done too.

I get about 150 of these:
{983} normal block at 0x0548CB48, 2000 bytes long.
Data: < I D 8D B> 00 C7 49 00 00 00 C4 44 00 00 38 44 00 00 00 42
That code does seem sound. As already mentioned, it is possible that the memory leak exists elsewhere--dealing with memory can cause some pretty weirdo problems, so, check other parts of your code. Once I had a memory violation in sprintf (or some similar function). I couldn't figure it out...The code looked fine, but the compiler told me it was there. Well, out of desperation, I decided to check some code around it. The problem was with some pointers that I had setup before the code.
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.
You'd have to post more code.

Cheers
Chris
CheersChris
Alright, I figured it out.
The input area was obfuscating the actual problem, but thanks for your replies anyway!

-Shane

This topic is closed to new replies.

Advertisement