Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ free memory allocation in constructor when facing exception
    primarykey
    data
    text
    <p>I found some interesting behavior in the case of exception when construct an object:</p> <pre><code>class bookentry { public: bookentry(){ t1.reset(new test1); //0 test1 *t11 = new test1; //1 test2 *t22 = new test2; //2 throw 1; //3 } protected: private: auto_ptr&lt;test1&gt; t1; auto_ptr&lt;test2&gt; t2; }; </code></pre> <p>From what I test:</p> <p>If we throw an exception from <strong><em>test2's constructor (#2)</em></strong>, then </p> <ol> <li><strong>t1</strong> : will be fully destroyed, i.e. t1's destructor will called, and memory will be freed.</li> <li><strong>t11</strong>: will never be destroyed (neither calling t11's destructor or freeing memory).</li> <li><strong>t22</strong>: this is funny part, t22's destructor will not be called which is right, but <strong>t22's memory will be freed!</strong></li> </ol> <p>If we throw an exception from <strong>bookentry's constructor (#3)</strong>,this time, things will be a litter different: <strong>t22</strong> will never be destroyed (neither calling t22's destructor or freeing memory), t11 is same as above</p> <p>I am just confused with the sentence at #2</p> <blockquote> <p>test2 *t22 = new test2; //2</p> </blockquote> <p><em>It seems</em> that when we throw an exception from class constructor, and new expression will make sure that corresponding delete is called, <strong>but</strong> any object that fully created via new expression before that exception will be leaked(t11, or t11 and t22 if we throw an exception at #3).</p> <p>so, if we write codes like below and the 5th object construction failed:</p> <blockquote> <p>test2 *t2s = new test2[10];</p> </blockquote> <p>then, it seems it's a litter bit safer:</p> <ol> <li>the already constructed objects will be destroyed,</li> <li>the memory will be freed.</li> </ol> <p>My questions, is this standard c++ behavior? I just test msvc 2012 and gcc 4.4.6, both looks implementing such mechanism.</p> <p><strong>Edit</strong> TO make my question more clearly:</p> <p>In some case you need write code like t1.reset(new test1)[see #1 in my codes] in other normal functions. In such case, if an exception is thrown from constructor. How can I reason about the program state? is there any memory leak? </p> <p><strong>My test make sure</strong> these is no memory leaks in such case, but is it standard c++ behavior?</p>
    singulars
    1. This table or related slice is empty.
    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