From virtual memory address space (Borland C ++ Builder 6 program)

I have a problem with some application written in C ++ Builder 6. After running for some time (week, month) the application crashes and exits without any error message. In my application log shortly before the crash, I am getting a lot of "Out of memory" exceptions. I looked at a process where it was throwing exceptions from memory (screenshot below) and it has a lot of unallocated private memory space. What could be the reason for this behavior?

I had this problem once, a couple of years ago. The reason for this was the "use dynamic link libraries" option not set in the linker options. When I checked it the problem went away and vice versa. The application under test I did just call "new char [1000000]" and then delete. The memory was freed every time (there was no memory increase in the Windows Task Manager), but after a while I lost memory, VMMap showed exactly the same thing. A lot of reserved private memory, but most of it is incomplete.

Now the problem is back, but I cannot fix it the same way. I don't know if that was the reason, but I had Builder 6 and 2010 on the same machine. Now I only have Builder 6 and it seems that I cannot reproduce the error with the test app as I used to. There seems to be some sort of memory manager bug or something on the air. CodeGuard does not detect memory leaks. When I create a block of memory with "new" it instantly shows up in "memory commit size" and decreases as the memory usage decreases, so I guess the memory leaks are wrong, the task manager doesn't show much "memory commit size".

Is there anything I can do? Is there a way to free uncommitted memory? What is the best way to diagnose the problem?

Screenshot: http://i.stack.imgur.com/UKuTZ.jpg

+3


source to share


2 answers


I found a way to simulate this problem and solution.

for(int i=0; i<100; i++)
{
    char * b = new char[100000000];
    new char;
    delete b;
}

      

Borland's memory manager reserves a block of memory that is a multiple of one page, which is 4KB. When allocating a memory size other than a multiple of 4kB, there is some free space that borland can use to allocate some other chunk of memory. When the first chunk is freed, the second still retains the reserved block of memory.



First see that the code should only leak 100B of memory, but in reality it will result in a memory allocation exception after less than 16 iterations.

I found two solutions for this problem. One of them is FastMM, it works but also causes some problems with it. The second solution is to swap borlndmm.dll with the one from Embarcadero Rad Studio 2010. I haven't tested it fully, but it seems to work without issue.

I have to move the holes project to RAD 2010, but for some reason I am stuck with Borland 6.

+3


source


Prologue

Hmm interesting behavior ... I have to add something I learned. I reject BCB6 right after a few tries because I had too many errors for my taste (compared to BCB5 , especially AnsiString

handling). So I stayed with BCB5 for a long time without any problem. I have used it even for very large projects like CAD / CAM .



After a few years I had to move to BDS2006 due to my employer and problems start ( some may be similar to yours ). Apart from minor IDEs and trace / interrupt / encoding errors, there are more important things like:

  • memory manager

    • delete/delete[]

      corrupts the memory manager if called twice for the same pointer without throwing any exceptions to be notified ...
    • the wrong default constructor / destructor for struct

      the compiler error was the biggest problem I encountered (combined with delete

      )
    • incorrect or missing member functions in classes can cause multiple calls delete

      !!! due to error t in the compiler or in the C ++ engine.

    but I was lucky enough to solve it here: bds 2006 C conflicts with hidden memory manager (class new / delete [] vs AnsiString)

  • wrong compilation

    Sometimes the application is not compiled correctly, no error occurs, but some lines of code are missing from the exe and / or are in a different order and then in the source code. I've seen this sometimes also in BCB 5.6 . To solve this:

    • delete all temporary files like ~,obj,tds,map,exe,...

    • close the IDE and reopen it just to be sure (sometimes looking at local variables (mostly large arrays) corrupts the IDE's memory)
    • compile again
  • Beware of breakpoint / trace / codeguard in different ways and then raw app

    especially with a multi-threaded application, the behavior behaves differently, and is traced, but not yet. Also, codeguard makes a big difference (and I don't mean slowdowns that distort sensitive sync). For example, codeguard has the nasty habit of sometimes throwing exceptions out of memory for no reason, so some parts of the code have to be checked over and over until they pass sometimes, even though mem usage is still the same and far out of memory.

    Operators
  • AnsiString

    There are two types in the VCL standard and component properties AnsiString

    . Therefore, it is reasonable to take this into account, since a AnsiString

    statement operation is performed on the component property . Try for example something like

    Edit1->Text+="xxx";
    
          

    there are also AnsiString

    operator errors like this:

    AnsiString version="aaa"+AnsiString("aaa")+"aaa";       // codeguard: array access violation
    
          

  • Importing old BCB projects

    Avoid direct imports if possible, it often generates some unknown allocation errors and memleaks. I'm not sure why, but I suspect imported window classes are handled differently and memleaks are related to bullet # 1 . Your best bet is to create a new application and create / copy components and code manually. I know this is a backword, but the only safe way to avoid problems is still don't know where the problem is, but a simple * .bdsproj replacement won't help !!! And I didn't see anything suspicious in * .dfm.

0


source







All Articles