<div dir="ltr">Dear VTK developers,<div><br></div><div>First of all: Am I posting this maybe in the wrong mailing list??</div><div><br></div><div>Anyway, here I go: In some project I am using the vtkIntersectPolyData filter. This works as expected many times, but from time to time I had crashes. I managed to track down these crashes to a little bug in that class that can be fixed by a very little change:</div><div><br></div><div>in vtkIntersectionPolyDataFilter.cxx(14'1), replace</div><div><br></div><div><pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(9,46,100)"> lineBool</span><span style="color:rgb(192,192,192)"> </span>=<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,128,0)">new</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,128,0)">bool</span>[<span style="color:rgb(9,46,100)">pd</span>-><span style="font-style:italic;color:rgb(0,103,124)">GetNumberOfCells</span>()];</pre></div><div><br></div><div>by</div><div><pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(9,46,100)"><br></span></pre><pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(9,46,100)"> lineBool</span><span style="color:rgb(192,192,192)"> </span>=<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,128,0)">new</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,128,0)">bool</span>[<span style="color:rgb(9,46,100)">pd</span>-><span style="font-style:italic;color:rgb(0,103,124)">GetNumberOfCells</span>()<span style="color:rgb(192,192,192)"> </span>+<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">1</span>];</pre></div><div><br></div><div>The crash happened when this array was released (heap corruption), and it turned out that indeed under some circumstances the program writes one element more than there is allocated memory. This happens if further down in the GetSingleLoop function the "else if" branch is chosen inside the "if -- else if -- else" construct: Here the pd cells array is extended by one element.</div><div><br></div><div>With the above fix the crash goes indeed away, but then later on the program throws an exception at vtkCellLinks.h(171):</div><div><br></div><div><pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">/**</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">*</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">Increment</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">the</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">count</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">of</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">the</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">number</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">of</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">cells</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">using</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">the</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">point.</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">*/</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,128,0)">void</span><span style="color:rgb(192,192,192)"> </span><span style="font-weight:600;color:rgb(0,103,124)">IncrementLinkCount</span>(<span style="color:rgb(128,0,128)">vtkIdType</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">ptId</span>)<span style="color:rgb(192,192,192)"> </span>{<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,128,0)">this</span>-><span style="color:rgb(128,0,0)">Array</span>[<span style="color:rgb(9,46,100)">ptId</span>].<span style="color:rgb(128,0,0)">ncells</span>++;};</pre>
<pre style="margin-top:0px;margin-bottom:0px"><br></pre><pre style="margin-top:0px;margin-bottom:0px">call stack at this point:</pre><pre style="margin-top:0px;margin-bottom:0px"><br></pre><pre style="margin-top:0px;margin-bottom:0px">1 vtkCellLinks::IncrementLinkCount vtkCellLinks.h 171 0x7feced1c91a
2 vtkCellLinks::BuildLinks vtkCellLinks.cxx 153 0x7feced1a89a
3 vtkPolyData::BuildLinks vtkPolyData.cxx 1096 0x7fecf022ff1
4 debug_vtkIntersectionPolyDataFilter::Impl::SplitCell debug_vtkIntersectionPolyDataFilter.cxx 1007 0x7fec0c994e2
5 debug_vtkIntersectionPolyDataFilter::Impl::SplitMesh debug_vtkIntersectionPolyDataFilter.cxx 630 0x7fec0c971cc
6 debug_vtkIntersectionPolyDataFilter::RequestData debug_vtkIntersectionPolyDataFilter.cxx 2590 0x7fec0c94531
7 vtkPolyDataAlgorithm::ProcessRequest vtkPolyDataAlgorithm.cxx 90 0x7fecf556116
8 vtkExecutive::CallAlgorithm vtkExecutive.cxx 775 0x7fecf50eb55
9 vtkDemandDrivenPipeline::ExecuteData vtkDemandDrivenPipeline.cxx 491 0x7fecf50477c
10 vtkCompositeDataPipeline::ExecuteData vtkCompositeDataPipeline.cxx 179 0x7fecf4fc560
11 vtkDemandDrivenPipeline::ProcessRequest vtkDemandDrivenPipeline.cxx 274 0x7fecf503343
12 vtkStreamingDemandDrivenPipeline::ProcessRequest vtkStreamingDemandDrivenPipeline.cxx 328 0x7fecf588f67
13 vtkDemandDrivenPipeline::UpdateData vtkDemandDrivenPipeline.cxx 444 0x7fecf5042da
14 vtkStreamingDemandDrivenPipeline::Update vtkStreamingDemandDrivenPipeline.cxx 403 0x7fecf589412
15 vtkStreamingDemandDrivenPipeline::Update vtkStreamingDemandDrivenPipeline.cxx 366 0x7fecf58916c
16 vtkAlgorithm::Update vtkAlgorithm.cxx 1457 0x7fecf4e5348
17 vtkAlgorithm::Update vtkAlgorithm.cxx 1451 0x7fecf4e53b3
18 vtkCemTestIntersectionFilter::RequestData vtkCemTestIntersectionFilter.cxx 252 0x7fec0c84e7f
19 vtkPolyDataAlgorithm::ProcessRequest vtkPolyDataAlgorithm.cxx 90 0x7fecf556116
20 vtkExecutive::CallAlgorithm vtkExecutive.cxx 775 0x7fecf50eb55
... <More>
</pre></div><div><br></div><div>Note that debug_vtkIntersectionPolyDataFilter is a copy of vtkIntersectionPolyDataFilter that I prepared to do my testing and add some debug output stuff etc. without interfering with the original code.</div><div><br></div><div>I guess that the exception is again a consequence of adding that "cell" in GetSingleLoop, but now I must admit that my understanding of the internals of these geometric object classes is not yet good enough to see what went wrong! The code that needs checking by an expert seems to me the following</div><div><br></div><div>vtkIntersectionPolyDataFilter.cxx(1536..1555):</div><div><br></div><div><pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">//There</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">is</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">one</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">line</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">attached</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">to</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">point.</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">This</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">means</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">the</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">intersection</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">has</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">//an</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">open</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">intersection</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">loop</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">(i.e.</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">the</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">surfaces</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">are</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">open</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">and</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">one</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">does</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">not</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">//completeley</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">intersect</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">the</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">other.</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">//Make</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">an</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">artificial</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">triangle</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">loop</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">in</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">this</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">case</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,128,0)">else</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,128,0)">if</span><span style="color:rgb(192,192,192)"> </span>(<span style="color:rgb(9,46,100)">pointCells</span>-><span style="color:rgb(0,103,124)">GetNumberOfIds</span>()<span style="color:rgb(192,192,192)"> </span><<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">2</span>)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span>{</pre>
<pre style="margin-top:0px;margin-bottom:0px"> <span style="font-family:arial,sans-serif;color:rgb(128,0,128)">vtkSmartPointer</span><span style="font-family:arial,sans-serif"><</span><span style="font-family:arial,sans-serif;color:rgb(128,0,128)">vtkPolyData</span><span style="font-family:arial,sans-serif">></span><span style="font-family:arial,sans-serif;color:rgb(192,192,192)"> </span><span style="font-family:arial,sans-serif;color:rgb(9,46,100)">currentpd</span><span style="font-family:arial,sans-serif;color:rgb(192,192,192)"> </span><span style="font-family:arial,sans-serif">=</span><br></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,0,128)">vtkSmartPointer</span><<span style="color:rgb(128,0,128)">vtkPolyData</span>>::<span style="color:rgb(0,103,124)">New</span>();</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,0,128)">vtkSmartPointer</span><<span style="color:rgb(128,0,128)">vtkCellArray</span>><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">currentcells</span><span style="color:rgb(192,192,192)"> </span>=</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,0,128)">vtkSmartPointer</span><<span style="color:rgb(128,0,128)">vtkCellArray</span>>::<span style="color:rgb(0,103,124)">New</span>();</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">currentcells</span><span style="color:rgb(192,192,192)"> </span>=<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">pd</span>-><span style="color:rgb(0,103,124)">GetLines</span>();</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">currentcells</span>-><span style="color:rgb(0,103,124)">InsertNextCell</span>(<span style="color:rgb(0,0,128)">2</span>);</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">currentcells</span>-><span style="color:rgb(0,103,124)">InsertCellPoint</span>(<span style="color:rgb(9,46,100)">nextPt</span>);</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">currentcells</span>-><span style="color:rgb(0,103,124)">InsertCellPoint</span>(<span style="color:rgb(9,46,100)">startPt</span>);</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">nextCell</span><span style="color:rgb(192,192,192)"> </span>=<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">currentcells</span>-><span style="font-style:italic;color:rgb(0,103,124)">GetNumberOfCells</span>()-<span style="color:rgb(0,0,128)">1</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">currentpd</span>-><span style="color:rgb(0,103,124)">SetLines</span>(<span style="color:rgb(9,46,100)">currentcells</span>);</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">currentpd</span>-><span style="font-style:italic;color:rgb(0,103,124)">SetPoints</span>(<span style="color:rgb(9,46,100)">pd</span>-><span style="font-style:italic;color:rgb(0,103,124)">GetPoints</span>());</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">pd</span>-><span style="font-style:italic;color:rgb(0,103,124)">DeepCopy</span>(<span style="color:rgb(9,46,100)">currentpd</span>);</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">pd</span>-><span style="color:rgb(0,103,124)">BuildLinks</span>();</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span>}</pre>
<pre style="margin-top:0px;margin-bottom:0px"><br></pre></div><div>My guess is that this code is not yet good enough to properly insert the "cell" into pd...</div><div><br></div><div>Any idea?</div><div><br></div><div>Regards,</div><div>Cornelis</div><div><div><br></div>-- <br><div class="gmail_signature">Cornelis Bockemühl<br>Basel, Schweiz<br></div>
</div></div>