<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><br></div><div>//=========================</div><div><br></div><div><div>
#include <petsc.h></div><div><br></div><div><br></div><div>class thing{</div><div><br></div><div><span class="" style="white-space:pre"> </span>public:</div><div><span class="" style="white-space:pre"> </span>Vec x;</div>
<div><span class="" style="white-space:pre"> </span>thing(){};</div><div><span class="" style="white-space:pre"> </span>thing(int N){ </div><div><span class="" style="white-space:pre"> </span>PetscPrintf(PETSC_COMM_WORLD, "x before create = %i\n", x);</div>
<div><span class="" style="white-space:pre"> </span>VecCreateSeq(PETSC_COMM_SELF, N, &x);</div><div><span class="" style="white-space:pre"> </span>PetscPrintf(PETSC_COMM_WORLD, "x after create = %i\n", x);</div>
<div><span class="" style="white-space:pre"> </span>}</div><div><span class="" style="white-space:pre"> </span></div><div><span class="" style="white-space:pre"> </span>~thing(){</div><div><span class="" style="white-space:pre"> </span>PetscPrintf(PETSC_COMM_WORLD, "x before destroy = %i\n", x);<span class="" style="white-space:pre"> </span></div>
<div><span class="" style="white-space:pre"> </span>VecDestroy(&x);</div><div><span class="" style="white-space:pre"> </span>PetscPrintf(PETSC_COMM_WORLD, "x after destroy = %i\n", x);<span class="" style="white-space:pre"> </span></div>
<div><span class="" style="white-space:pre"> </span>}</div><div><span class="" style="white-space:pre"> </span></div><div>};</div><div><br></div><div><br></div><div>int main(int argc, char** argv){</div><div><br></div><div>
<span class="" style="white-space:pre"> </span>PetscInitialize(&argc, &argv, PETSC_NULL, PETSC_NULL);</div><div><br></div><div><span class="" style="white-space:pre"> </span>thing t;</div><div><span class="" style="white-space:pre"> </span>PetscPrintf(PETSC_COMM_WORLD, "x before everything = %i\n", t.x);</div>
<div><span class="" style="white-space:pre"> </span>t = thing(2);</div><div><span class="" style="white-space:pre"> </span>PetscPrintf(PETSC_COMM_WORLD, "x after everything = %i\n", t.x);</div><div><span class="" style="white-space:pre"> </span></div>
<div><span class="" style="white-space:pre"> </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>