Click here to Skip to main content
15,887,596 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Memory leak Pin
Stephen Hewitt25-Feb-13 16:43
Stephen Hewitt25-Feb-13 16:43 
GeneralRe: Memory leak Pin
Richard Andrew x6426-Feb-13 9:11
professionalRichard Andrew x6426-Feb-13 9:11 
AnswerRe: Memory leak Pin
David Crow25-Feb-13 13:24
David Crow25-Feb-13 13:24 
AnswerRe: Memory leak Pin
Stefan_Lang26-Feb-13 4:04
Stefan_Lang26-Feb-13 4:04 
GeneralRe: Memory leak Pin
a_matseevsky26-Feb-13 10:59
a_matseevsky26-Feb-13 10:59 
GeneralRe: Memory leak Pin
Stefan_Lang26-Feb-13 22:44
Stefan_Lang26-Feb-13 22:44 
GeneralRe: Memory leak Pin
a_matseevsky27-Feb-13 2:09
a_matseevsky27-Feb-13 2:09 
GeneralRe: Memory leak Pin
Stefan_Lang27-Feb-13 2:52
Stefan_Lang27-Feb-13 2:52 
Sorry, apparently I've skipped part of your message in reading. So what you're seeing is a fragmentation of the virtual address space of your process, and that prevents the allocation of a block that doesn't fit into that space.

My next suggestion then would be to allocate a big block of memory before calling CFileDialog, like you did. You don't even need VirtualAlloc for that, you could just call new, and then create your buffer using in-place construction.

This of course requires that you know in advance the size of your file. But then, if the size would vary, there would be no point in even trying to load it into memory as a whole: if you don't know the size, you have to assume that you will eventually encounter files that don't fit into memory, no matter what!

That said, even if the file is always the same size, where is it that this aplication will run? Is it on your PC? If not, a client running the program may have less memory available than you do!


One last idea is to split the file buffer into multiple parts, allowing you to use up most of the available memory in spite of fragmentation. That way you could keep all of it in memory, just not within one contiguous block.

You could even write a wrapper class that does behave like a contiguous block of memory, but internally translates an address into a location within one of those buffers.

P.S.: here's some code to illustrate my suggestion:
C++
template <unsigned int total_size, unsigned int chunk_size>
class DistributedBuffer {
   unsigned char* blocks[1+(total_size-1)/chunk_size];
public:
   DistributedBuffer() {
      for (unsigned int i = 0; i < 1+(total_size-1)/chunk_size; ++i)
         blocks[i] = new unsigned char[chunk_size];
   }
   // read from file address
   unsigned char operator[](unsigned int offset) const {
      return blocks[offset/chunk_size][offset%chunk_size];
   }
   // write to file address
   unsigned char& operator[](unsigned int offset) {
      return blocks[offset/chunk_size][offset%chunk_size];
   }
};
int foo() {
   // create 1 GB buffer by allocating 1024 blocks of 1 MB each
   DistributedBuffer<1024*1024*1024, 1024*1024> myFileBuffer;
   // now do some work:
   myFileBuffer[12345678] = 17; // calling unsigned char& operator[](unsigned int offset)
   unsigned char c = myFileBuffer[12345678]; // calling unsigned char operator[](unsigned int offset) const
   return (int)c; // returns 17
}


modified 27-Feb-13 9:16am.

GeneralRe: Memory leak Pin
a_matseevsky27-Feb-13 11:44
a_matseevsky27-Feb-13 11:44 
Question[Solved] Memory leak in producer-consumer program Pin
noislude25-Feb-13 7:30
noislude25-Feb-13 7:30 
AnswerRe: Memory leak in producer-consumer program Pin
Richard Andrew x6425-Feb-13 10:08
professionalRichard Andrew x6425-Feb-13 10:08 
GeneralRe: Memory leak in producer-consumer program Pin
noislude25-Feb-13 10:23
noislude25-Feb-13 10:23 
GeneralRe: Memory leak in producer-consumer program Pin
Richard Andrew x6425-Feb-13 10:33
professionalRichard Andrew x6425-Feb-13 10:33 
GeneralRe: Memory leak in producer-consumer program Pin
noislude25-Feb-13 10:42
noislude25-Feb-13 10:42 
QuestionRe: Memory leak in producer-consumer program Pin
David Crow25-Feb-13 13:28
David Crow25-Feb-13 13:28 
AnswerRe: Memory leak in producer-consumer program Pin
noislude25-Feb-13 13:33
noislude25-Feb-13 13:33 
GeneralRe: Memory leak in producer-consumer program Pin
Stefan_Lang26-Feb-13 3:29
Stefan_Lang26-Feb-13 3:29 
GeneralRe: Memory leak in producer-consumer program Pin
noislude26-Feb-13 8:07
noislude26-Feb-13 8:07 
GeneralRe: Memory leak in producer-consumer program Pin
Stefan_Lang26-Feb-13 22:21
Stefan_Lang26-Feb-13 22:21 
GeneralRe: Memory leak in producer-consumer program Pin
noislude26-Feb-13 22:27
noislude26-Feb-13 22:27 
GeneralRe: Memory leak in producer-consumer program Pin
Stefan_Lang26-Feb-13 23:01
Stefan_Lang26-Feb-13 23:01 
SuggestionRe: Memory leak in producer-consumer program Pin
noislude27-Feb-13 9:47
noislude27-Feb-13 9:47 
AnswerRe: Memory leak in producer-consumer program Pin
Vaclav_25-Feb-13 17:45
Vaclav_25-Feb-13 17:45 
QuestionWhat is the MFC future Pin
Arris7425-Feb-13 7:06
Arris7425-Feb-13 7:06 
AnswerRe: What is the MFC future Pin
Vaclav_25-Feb-13 9:46
Vaclav_25-Feb-13 9:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.