PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

CreateFile() and the stack

:?: Does anyone know if CreateFile() allocates memory on the stack?

The application I am writing opens a file (of variable size, but possibly large) at the start. It needs to read from this file often during execution, so I basically opened the file with CreateFile() and then left it. However, I seem to be running out of stack space later on in the program (at least I think that's the problem).

To get around the problem, I now use CloseHandle() as soon as I am done with the file, and then call CreateFile() again every time I need to re-access the data, and this seems to cause no problems.

Example (old method):

1. CFileHandler class opened file in constructor
2. CFileHandler class called CloseHandle() in destructor

The CFileHandler class is instantiated once at the beginning of my application (CFileHandler *handler = new CFileHandler()), and not destroyed until the program finishes.

Example (new method):
1. CFileHandler.ReadData calls CreateFile() at start
2. CFileHandler.ReadData calls CloseHandle() at end
3. No file handling is done in the constructor/destructor


Is this inefficient? Or is this how I should be doing things? Are there any other suggestions?

I am using eVC++ 3.0 and the PPC2002 emulator that comes with it.

Thanks![/code]
Peter Balogh · 07 May 2004, 06:24 UTC
Try compiling for desktop windows and using a leak checker, like purify, boundschecker or the debug heap (crtdbg.h) to detect memory leaks. I suspect the leak is in your code somewhere.
As a general comment, you shouldn't read files too much unless it's absolutely necessary. Closing them after every read sounds like a bad idea too. File handling on CE is awfully slow when it comes to opening or randomly seeking files. You should consider keeping more data in memory if it's feasible (e.g. your application is running in foreground and we're talking about a couple hundred kilobytes, not tens of megabytes).

Cheers,
Peter
Thank you Peter!

You were right, I did have a (rather large!) memory leak elsewhere in my code. It just didn't seem to cause any problems until I tried opening a file (I'm still not sure why).

Anyway, I fixed the leak and everything is working fine now. Thanks again for the advice! :D