Note that there are some explanatory texts on larger screens.

plurals
  1. PObds 2006 C hidden memory manager conflicts (class new / delete[] vs. AnsiString)
    primarykey
    data
    text
    <p>I am using <strong>BDS 2006 Turbo C++</strong> for a long time now and some of my bigger projects (<strong>CAD/CAM,3D</strong> gfx engines and Astronomic computations) occasionally throw an exception (for example once in 3-12 months of 24/7 heavy duty usage). After extensive debugging I found this:</p> <pre class="lang-cpp prettyprint-override"><code>//code1: struct _s { int i; } // any struct _s *s=new _s[1024]; // dynamic allocation delete[] s; // free up memory </code></pre> <p>this code is usually inside template where <code>_s</code> can be also class therefore <code>delete[]</code> this code should work properly, but the <code>delete[]</code> does not work properly for structs (classes looks OK). No exceptions is thrown, the memory is freed, but it somehow damages the memory manager allocation tables and after this any new allocation can be wrong (new can create overlapped allocations with already allocated space or even unallocated space hence the occasional exceptions) </p> <p>I have found that if I add empty destructor to <code>_s</code> than suddenly seems everything OK</p> <pre class="lang-cpp prettyprint-override"><code>struct _s { int i; ~_s(){}; } </code></pre> <p>Well now comes the weird part. After I update this to my projects I have found that <code>AnsiString</code> class has also bad reallocations. For example:</p> <pre class="lang-cpp prettyprint-override"><code>//code2: int i; _s *dat=new _s[1024]; AnsiString txt=""; // setting of dat for (i=0;i&lt;1024;i++) txt+="bla bla bla\r\n"; // usage of dat delete[] dat; </code></pre> <p>In this code <code>dat</code> contains some useful data, then later is some <code>txt</code> string created by adding lines so the <code>txt</code> must be reallocated few times and sometimes the <code>dat</code> data is overwritten by <code>txt</code> (even if they are not overlapped, I thing the temp <code>AnsiString</code> needed to reallocate <code>txt</code> is overlapped with <code>dat</code>)</p> <p>So my questions are:</p> <ol> <li>Am I doing something wrong in code1, code2 ?</li> <li><p>Is there any way to avoid <code>AnsiString</code> (re)allocation errors ? (but still using it)</p> <ul> <li>After extensive debugging (after posting question 2) I have found that <code>AnsiString</code> do not cause problems. They only occur while using them. The real problem is probably in switching between <strong>OpenGL</strong> clients. I have Open/Save dialogs with preview for vector graphics. If I disable <strong>OpenGL</strong> usage for these <strong>VCL</strong> sub-windows than <code>AnsiString</code> memory management errors disappears completely. I am not shore what is the problem (incompatibility between <strong>MFC/VCL</strong> windows or more likely I made some mistake in switching contexts, will further investigate). Concern <strong>OpenGL</strong> windows are:</li> <li>main <strong>VCL</strong> Form + <strong>OpenGL</strong> inside <code>Canvas</code> client area</li> <li>child of main <strong>MFC</strong> Open/Save dialog + docked preview <strong>VCL</strong> Form + <strong>OpenGL</strong> inside <code>Canvas</code> client area</li> </ul></li> </ol> <p><strong>P.S.</strong></p> <ol> <li>these errors depend on number of <code>new/delete/delete[]</code> usages not on the allocated sizes</li> <li>both code1 and code2 errors are repetitive (for example have a parser to load complex ini file and the error occurs on the same line if the ini is not changed)</li> <li>I detect these errors only on big projects (plain source code > 1MB) with combined usage of <code>AnsiString</code> and templates with internal dynamic allocations, but is possible that they are also in simpler projects but occurs so rarely that I miss it.</li> <li>Infected projects specs: <ul> <li>win32 noinstall standalone (using <strong>Win7sp1 x64</strong> but on <strong>XPsp3 x32</strong> behaves the same)</li> <li>does not meter if use <strong>GDI</strong> or <strong>OpenGl/GLSL</strong></li> <li>does not meter if use device driver <strong>DLL</strong>s or not</li> <li>no <strong>OCX</strong>,or nonstandard <strong>VCL</strong> component</li> <li>no <strong>DirectX</strong></li> <li>1 Byte aligned compilation/link</li> <li>do not use <strong>RTL</strong>,packages or frameworks (standalone)</li> </ul></li> </ol> <p>Sorry for bad English/grammar ... any help / conclusion / suggestion appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload