Note that there are some explanatory texts on larger screens.

plurals
  1. POUnusual calling of destructor
    primarykey
    data
    text
    <p>I am having a strange problem instantiating a structure living inside a class, where in construction it calls the destructor (several times) and even calls the parent object destructor.</p> <p>Class with structure:</p> <pre><code>class Model { public: struct StepModelIO { StepModelIO(Model model, ...) {DoConstruction(Model model, ...);} StepModelIO(const StepModelIO &amp;other) {DoConstruction(Model model, ...); } ~StepModelIO() {} DoConstruction() { ... } } Model(...) { ... } Model(const Model &amp;other) {DoConstruction(...);} ~Model() { ... } private: DoConstruction(...) { } } </code></pre> <p>Calling function:</p> <pre><code>void main() { Model::StepModelIO stepArgs = Model::StepModelIO(...); } </code></pre> <p>The resulting set of calls, with 'object' being the <code>StepModelIO</code> and 'parent' being the <code>Model</code>:</p> <ol> <li>Construct parent w/ copy constructor</li> <li>Construct object</li> <li>Destruct parent</li> <li>Construct object w/ copy constructor</li> <li>Construct parent w/ copy constructor</li> <li>Construct object</li> <li>Destruct parent</li> <li>Destruct object</li> <li>Destruct object (again...)</li> <li>Destruct parent</li> </ol> <p>Unsurprisingly the resulting structure (a <code>StepModelIO</code>) is not in a good state after this all happens, and the path seemed ridiculous. I have the structure housed like this to use the same generic at the parent Model object, which may explain some of the issues.</p> <p>I have tried (perhaps naively) to use the 'rule of three' on constructors and destructors, it's possible I've munged this up badly.</p> <p><strong>Edit:</strong> Full code</p> <pre><code>template&lt;typename U, typename V&gt; class Model{ public: struct StepModelIO { Model&lt;U, V&gt; model; U u; V v; StepModelIO() {} StepModelIO(Model&lt;U, V&gt; model, U u, V v) { this-&gt;model = model; this-&gt;u = u; this-&gt;v = v; } StepModelIO (const StepModelIO &amp;other) { StepModelIO(other.model, other.u, other.v); } ~StepModelIO() { } }; Model(char * libraryPath) {DoConstruction(libraryPath);} Model() {} Model (const Model &amp;other) { DoConstruction(other.m_LibraryPath); } ~Model() { this-&gt;Stop(); } void Init() { if (!this-&gt;m_Initialised) { this-&gt;ModelInit(); m_Initialised = true; } } void Stop() { if (this-&gt;m_Initialised) { this-&gt;ModelStop(); m_Initialised = false; } } void Restart() { this-&gt;ModelRestart(); } void Step(U u, V v) { ModelStep(u, v); } private: char* m_LibraryPath; HINSTANCE m_ModelDLL; bool m_Initialised; typedef int (__cdecl * EmptyModelFunctionPointer)(); // Interpret integer as C code pointer named 'EmptyModelFunctionPointer' typedef int (__cdecl * ModelFunctionPointer)(U u, V v); EmptyModelFunctionPointer ModelInit; EmptyModelFunctionPointer ModelStop; EmptyModelFunctionPointer ModelRestart; ModelFunctionPointer ModelStep; virtual void DoConstruction(char * libraryPath){ this-&gt;m_Initialised = false; this-&gt;m_LibraryPath = libraryPath; this-&gt;m_ModelDLL = LoadLibrary(libraryPath); this-&gt;ModelInit = GetFunction&lt;EmptyModelFunctionPointer&gt;(m_ModelDLL, "Init"); this-&gt;ModelStop = GetFunction&lt;EmptyModelFunctionPointer&gt;(m_ModelDLL, "Stop"); this-&gt;ModelRestart = GetFunction&lt;EmptyModelFunctionPointer&gt;(m_ModelDLL, "Restart"); this-&gt;ModelStep = GetFunction&lt;ModelFunctionPointer&gt;(m_ModelDLL, "Step"); } template&lt;typename pointerType&gt; pointerType GetFunction(HINSTANCE modelLibrary, char * functionName){ return (pointerType)GetProcAddress(HMODULE (modelLibrary),functionName); } }; </code></pre> <p>Caller:</p> <pre><code>StepModelIO&lt;Type_1*, Type_2*&gt; stepArgs = StepModelIO&lt;Type_1*, Type_2*&gt;(newModel, &amp;a, &amp;b[0]); </code></pre>
    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