<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sun, Jan 22, 2017 at 10:15 AM, Alessandro Volz <span dir="ltr"><<a href="mailto:alessandro.volz@gmail.com" target="_blank">alessandro.volz@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div>Thanks David,</div><div><br></div><div>So yes, I got there this way: </div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(49,89,93)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">    reslice-></span><span style="font-variant-ligatures:no-common-ligatures">GetOutput</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">()-></span><span style="font-variant-ligatures:no-common-ligatures">GetPoi<wbr>ntData</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">()-></span><span style="font-variant-ligatures:no-common-ligatures">SetScalars</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">vtkSmartP<wbr>ointer</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"><</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">vtkFloatArray</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">>::</span><span style="font-variant-ligatures:no-common-ligatures">New</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">());</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    reslice-></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">GetOutput</span><span style="font-variant-ligatures:no-common-ligatures">()-></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">GetPoint<wbr>Data</span><span style="font-variant-ligatures:no-common-ligatures">()-></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">GetScalars</span><span style="font-variant-ligatures:no-common-ligatures">()-></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">SetVoidA<wbr>rray</span><span style="font-variant-ligatures:no-common-ligatures">(myBufferPtr</span><span style="font-variant-ligatures:no-common-ligatures">, myBufferBytesLen</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">1</span><span style="font-variant-ligatures:no-common-ligatures">);</span></div></div><div><br></div><div>It works fine, but could you explain why in <span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">vtkImageData</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">::<wbr>AllocateScalars</span> we have:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">if</span><span style="font-variant-ligatures:no-common-ligatures"> (scalars && scalars-></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">GetDataType</span><span style="font-variant-ligatures:no-common-ligatures">() == dataType</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">      && scalars-></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">GetReferenceCount</span><span style="font-variant-ligatures:no-common-ligatures">() == </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">1</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div>Why does it matter, the ReferenceCount be equal to 1 ?</div></div></blockquote><div><br></div><div>I didn't know that check was there, but it is checking the reference count to see if it holds the only reference to data array.  If it does, then it simply reallocates the array that it has, otherwise it creates a new array.</div><div><br></div><div>Your code is creating a loose reference here:</div><div><br></div><div>    reslice->GetOutput()->GetPointData()->SetScalars(vtkSmartPointer<vtkFloatArray>::New());<br></div><div><br></div><div>The SetScalars() method does not take a smart pointer, so the smart pointer is being converted into a regular pointer.  The result is exactly the same as if you called vtkFloatArray::New().</div><div><br></div><div>In VTK, you should only call vtkSmartPointer<vtkFloatArray>::New() if you are assigning the result to a smart pointer.  Otherwise it's a memory leak.  This is why most smart pointer like std::shared_ptr don't have a typecast operator to allow them to devolve into regular pointers.  For VTK, it was decided that the convenience of having a typecast operator was worth the risk of memory leaks, since we have nightly testing to check for leaks.</div><div><br></div><div>To avoid the leak and fix the reference count,</div><div><br></div><div>    vtkSmartPointer<vtkFloatArray> scalars = vtkSmartPointer<vtkFloatArray>::New();</div><div>    scalars->SetVoidArray(myBufferPointer, myBufferLen, 1);</div><div>    reslice->GetOutput()->GetPointData()->SetScalars(scalars);</div><div><br></div><div>And of course you'll have to make sure the smart pointer goes out of scope before you call Update() on reslice.  To be absolutely sure the reference count will be one when AllocateScalars is called, do this:</div><div><br></div><div><div>    vtkFloatArray *scalars = vtkFloatArray::New();</div><div>    scalars->SetVoidArray(myBufferPointer, myBufferLen, 1);</div><div>    reslice->GetOutput()->GetPointData()->SetScalars(scalars);</div></div><div>    scalars->Delete();</div><div><br></div><div>One further item of importance is that the "size" for SetVoidArray() is the number of elements, not the number of bytes.  That's something that has confused me in the past, and the documentation is not at all clear on that point.</div><div><br></div><div> - David</div><div> </div></div></div></div>