<div dir="ltr">There are couple of things you are doing wrong here!<div><br></div><div>> <span style="font-size:13px;font-family:arial,sans-serif">Shouldn't it be destroying the Vec declared by the very first "thing t", and hence throwing an error saying that you can't destroy a v that has not been created? </span><br>

</div><div><span style="font-size:13px;font-family:arial,sans-serif"><br></span></div><div><font face="arial, sans-serif">No. The destructor of an object is called when either 1) manually call `delete` on an instance allocated via `new` or 2) when the object is allocated on the stack and goes out of scope. In your case the destructor for `t` is called when program reaches the end of `main` i.e. when it terminates.</font></div>

<div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">In your case, you are first creating an empty object using the default ctor and then assigning it to a temporary object `thing(2)`. Once the assignment is done, the destructor of the temporary object is called. Also note that since you have not implemented an assignment operator for your class, the default is tiled which merely copies `x` which is most certainly what you want since in PETSc `Vec` is simply an opaque pointer. This means to properly be able to copy one `thing` to another you need to implement the assignment operator and most probably also the copy ctor . See </font><a href="http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three">http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three</a> for more details on this.</div>

<div><br></div><div>Also If you are going to depend on dtor to destroy pets objects, you need to implement a class that initializes and finalizes PETSc through ctor and dtor and call it before calling any other object. Otherwise all other classes will be calling their dtors after PetscFinalize. Something like this should serve the purpose</div>

<div><br></div><div><div><div>class PetscSession{</div><div>public:</div><div><span class="" style="white-space:pre">     </span>PetscSession(int argc, char* argv[]){</div><div><span class="" style="white-space:pre">              </span>PetscInitialize(&argc, &argv, NULL, NULL);</div>

<div><span class="" style="white-space:pre">    </span>}</div><div><span class="" style="white-space:pre">  </span>~PetscSession(){</div><div><span class="" style="white-space:pre">           </span>PetscFinalize();</div><div><span class="" style="white-space:pre">   </span>}</div>

<div>};</div><div><br></div><div>int main(int argc, char* argv[]){</div><div><span class="" style="white-space:pre">  </span>PetscSession petsc(argc, argv);</div><div><span class="" style="white-space:pre">    </span>// All other classes come after PetscSession has been called</div>

<div><span class="" style="white-space:pre">    </span>return 0;</div><div>}</div></div></div><div><br></div><div><font face="arial, sans-serif"><br></font><div><br></div><div class="gmail_extra"><div class="gmail_quote">On Wed, Feb 5, 2014 at 7:14 PM, Matthew Knepley <span dir="ltr"><<a href="mailto:knepley@gmail.com" target="_blank">knepley@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div><div class="h5">

<div class="gmail_quote">On Wed, Feb 5, 2014 at 8:02 PM, David Liu <span dir="ltr"><<a href="mailto:daveliu@mit.edu" target="_blank">daveliu@mit.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">


<div dir="ltr">Hi, this is a question mainly to clear up my understanding of what the Vec object is. Consider the following C++ code:<div><br></div><div>//=========================</div><div><br></div><div><div>
#include <petsc.h></div><div><br></div><div><span style="color:rgb(34,34,34)"> </span><br></div></div></div></blockquote></div></div></div></div></div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<div dir="ltr"><div class="gmail_extra"><div><div class="h5"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<div dir="ltr"><div><div></div><div>class thing{</div><div><br></div><div><span style="white-space:pre-wrap">     </span>public:</div><div><span style="white-space:pre-wrap">  </span>Vec x;</div>
<div><span style="white-space:pre-wrap">  </span>thing(){};</div><div><span style="white-space:pre-wrap">       </span>thing(int N){ </div><div><span style="white-space:pre-wrap">           </span>PetscPrintf(PETSC_COMM_WORLD, "x before create = %i\n", x);</div>



<div><span style="white-space:pre-wrap">          </span>VecCreateSeq(PETSC_COMM_SELF, N, &x);</div><div><span style="white-space:pre-wrap">                </span>PetscPrintf(PETSC_COMM_WORLD, "x after create = %i\n", x);</div>
<div><span style="white-space:pre-wrap">  </span>}</div><div><span style="white-space:pre-wrap">        </span></div><div><span style="white-space:pre-wrap"> </span>~thing(){</div><div><span style="white-space:pre-wrap">                </span>PetscPrintf(PETSC_COMM_WORLD, "x before destroy = %i\n", x);<span style="white-space:pre-wrap">  </span></div>



<div><span style="white-space:pre-wrap">          </span>VecDestroy(&x);</div><div><span style="white-space:pre-wrap">              </span>PetscPrintf(PETSC_COMM_WORLD, "x after destroy = %i\n", x);<span style="white-space:pre-wrap">                   </span></div>



<div><span style="white-space:pre-wrap">  </span>}</div><div><span style="white-space:pre-wrap">        </span></div><div>};</div><div><br></div><div><br></div><div>int main(int argc, char** argv){</div><div><br></div><div>
<span style="white-space:pre-wrap">     </span>PetscInitialize(&argc, &argv, PETSC_NULL, PETSC_NULL);</div><div><br></div><div><span style="white-space:pre-wrap">  </span>thing t;</div><div><span style="white-space:pre-wrap"> </span>PetscPrintf(PETSC_COMM_WORLD, "x before everything = %i\n", t.x);</div>



<div><span style="white-space:pre-wrap">  </span>t = thing(2);</div><div><span style="white-space:pre-wrap">    </span>PetscPrintf(PETSC_COMM_WORLD, "x after everything = %i\n", t.x);</div><div><span style="white-space:pre-wrap">       </span></div>



<div><span style="white-space:pre-wrap">  </span>PetscFinalize();</div><div>}</div></div><div><br></div><div>//=========================<br></div><div><br></div><div>The output, when run sequentially, is</div><div><br>
</div><div><div>x before everything = 0</div><div>x before create = 0</div><div>x after create = -326926224</div><div>x before destroy = -326926224</div><div>x after destroy = 0</div><div>x after everything = -326926224</div>



</div><div><br></div><div>(among some unimportant error messages). If I try to VecGetSize(t.x, &N), immediately after the line "t = thing(2)", I get an error indicating that t.x has been destroyed. </div><div>



<br></div><div>This behavior, as well as the printed output, suggests that the destructor being called during the line "t = thing(2)" is destroying the Vec just created by "thing(2)". Shouldn't it be destroying the Vec declared by the very first "thing t", and hence throwing an error saying that you can't destroy a v that has not been created? </div>



</div>
</blockquote></div><br></div></div>This has nothing to do with Vec, it is about C++ copy semantics. This is why I would</div><div class="gmail_extra">never tell someone to use C++.</div><div class="gmail_extra"><br></div>

<div class="gmail_extra">
   Matt<span class=""><font color="#888888"><br clear="all"><div><br></div>-- <br>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>

-- Norbert Wiener
</font></span></div></div>
</blockquote></div><br></div></div></div>