<div>Reinhard,</div>
<div>I added a test Testing/Code/Common/itkImportContainerTest that exercises the ImportImageContainer class. As it turns out, the code coverage was fairly low for this class. As I wrote it, I discovered that the class can be tricky to use.
</div>
<div>&nbsp;</div>
<div>If you are on a linux system, I suggest that you run your code with valgrind,s memtest. <a href="http://valgrind.org/">http://valgrind.org/</a></div>
<div>&nbsp;</div>
<div>I suspect that your code is mucking improperly with the import pointer. If you can&#39;t run valgrind, perhaps you could&nbsp;post a small program that illustrates the error and we could help analyze it.</div>
<div>&nbsp;</div>
<div>Bill<br></div>
<div class="gmail_quote">On Dec 9, 2007 5:55 PM, Reinhard Hameeteman &lt;<a href="mailto:k.hameeteman+maillist@gmail.com">k.hameeteman+maillist@gmail.com</a>&gt; wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Hi Luis,<br><br>Thanks for taking a look.<br><br>Let me try to untangle things by reducing it to one method:
<br><br>I want to pass the output buffer of the itk filter (an itk::Image&lt;<br>itk::Vector&lt; float, 3&gt;, 3&gt; image) to my application. So the itk filter<br>is responsible for allocating the buffer, but is told not to
<br>deallocate the buffer when getting out of scope like this:<br><br>// create my own image<br>CustomImageType m_MemberImage.;<br><br>// pointer to an aray of 3 floats<br>vecf3 * pixelPointer;<br><br>// get pointer to itk output buffer
<br>pixelPointer = &nbsp;itkFilter-&gt;GetOutput()-&gt;GetPixelContainer()-&gt;GetImportPointer();<br><br>// pass the itk buffer to my own application<br>m_MemberImage.setData( pixelPointer );<br><br>// tell itk filter not to destroy the buffer
<br>
<div class="Ih2E3d">itkFilter-&gt;GetOutput()-&gt;GetPixelContainer()-&gt;SetContainerManageMemory(false);<br><br></div>This works fine until I want to deallocated the image buffer using:<br>delete[] pixelPointer;<br><br>
I&#39;ve used this method before with itk::Image&lt; short, 3&gt; images and it<br>worked fine, but now with the vector image my program crashes upon<br>deleting the buffer.<br><br>Sorry for the confusing, I hope this clarifies it a bit.
<br><font color="#888888"><br>Reinhard<br></font>
<div>
<div></div>
<div class="Wj3C7c"><br><br>On Dec 9, 2007 2:37 AM, Luis Ibanez &lt;<a href="mailto:luis.ibanez@kitware.com">luis.ibanez@kitware.com</a>&gt; wrote:<br>&gt;<br>&gt; Hi Reinhard,<br>&gt;<br>&gt;<br>&gt; I&#39;m afraid that by now,
<br>&gt; your questions are too entangled to address them correctly.<br>&gt;<br>&gt;<br>&gt; Let me rephrase the questions and please correct me if I&#39;m getting<br>&gt; them wrong.<br>&gt;<br>&gt;<br>&gt; 1) you are running an ITK pipeline and you want the output of the
<br>&gt; &nbsp; &nbsp; pipeline to store its result in a block of memory that you have<br>&gt; &nbsp; &nbsp; pre-allocated from your application.<br>&gt;<br>&gt;<br>&gt; 2) You want to do this with a filter that produces as output an<br>&gt; &nbsp; &nbsp; image whose pixel type is Vector&lt;float,3&gt;.
<br>&gt;<br>&gt;<br>&gt; 3) You are attempting to use the following method:<br>&gt;<br>&gt; &nbsp; &nbsp; &nbsp;m_Filter-&gt;GetOutput()-&gt;SetRegions(region);<br>&gt;<br>&gt; &nbsp; &nbsp; &nbsp;m_Filter-&gt;GetOutput()-&gt;GetPixelContainer()-&gt;SetImportPointer(
<br>&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;static_cast&lt; OutputPixelType * &gt;( applicationAllocatedBuffer ),<br>&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;totalNumberOfPixels, false);<br>&gt;<br>&gt; &nbsp; &nbsp; &nbsp;m_Filter-&gt;GetOutput()-&gt;Allocate( );<br>&gt;<br>&gt;<br>&gt; 4) When you run it, it crashes at the moment of dealocating the
<br>&gt; &nbsp; &nbsp; memory.<br>&gt;<br>&gt;<br>&gt;<br>&gt; Is that right ?<br>&gt;<br>&gt;<br>&gt; If so, this leads to suspect that<br>&gt;<br>&gt;<br>&gt; A) you have not allocated the right size of memory in your application
<br>&gt;<br>&gt;<br>&gt; or<br>&gt;<br>&gt;<br>&gt; B) your assumptions about the organization of the pixel components<br>&gt; &nbsp; &nbsp; in memory are not correct.<br>&gt;<br>&gt;<br>&gt;<br>&gt; Please provide a fresh description of the state of your problem.
<br>&gt;<br>&gt;<br>&gt; &nbsp; &nbsp;Thanks<br>&gt;<br>&gt;<br>&gt;<br>&gt; &nbsp; &nbsp; &nbsp; Luis<br>&gt;<br>&gt;<br>&gt;<br>&gt;<br>&gt; ---------------------------<br>&gt;<br>&gt; Reinhard Hameeteman wrote:<br>&gt; &gt; To comment on my own questions:
<br>&gt; &gt; The error in the second approach, was due to an invalid pointer the<br>&gt; &gt; was passed to the itk filter.<br>&gt; &gt; However the other 2 questions still remain. And since this approach<br>&gt; &gt; preferable because you allocate the memory once it is needed and not
<br>&gt; &gt; long before that (depending on your filter), I would very much get<br>&gt; &gt; some comments on those issues.<br>&gt; &gt; Is someone capable of doing so?<br>&gt; &gt;<br>&gt; &gt; Reinhard<br>&gt; &gt;<br>
&gt; &gt;<br>&gt; &gt; On Dec 3, 2007 11:50 AM, Reinhard Hameeteman<br>&gt; &gt; &lt;<a href="mailto:k.hameeteman+maillist@gmail.com">k.hameeteman+maillist@gmail.com</a>&gt; wrote:<br>&gt; &gt;<br>&gt; &gt;&gt;Hi,<br>&gt; &gt;&gt;
<br>&gt; &gt;&gt;I am experiencing similar problems as described in<br>&gt; &gt;&gt;<a href="http://www.itk.org/pipermail/insight-users/2005-December/016027.html" target="_blank">http://www.itk.org/pipermail/insight-users/2005-December/016027.html
</a>.<br>&gt; &gt;&gt;I use an itk pipeline within another image processing framework<br>&gt; &gt;&gt;(MeVisLab). To avoid unnecessary memory copying I want to pass a<br>&gt; &gt;&gt;pointer to the image data output from a itk filter output to an other
<br>&gt; &gt;&gt;data structure. As Luis described in e.g.<br>&gt; &gt;&gt;<a href="http://www.itk.org/pipermail/insight-users/2007-May/022343.html" target="_blank">http://www.itk.org/pipermail/insight-users/2007-May/022343.html
</a> there<br>&gt; &gt;&gt;are 2 methods:<br>&gt; &gt;&gt;1) Mummify the buffer of the output filter<br>&gt; &gt;&gt;2) Provide the memory buffer to the output filter so when it computes<br>&gt; &gt;&gt;the output image, it is already stored in the buffer that you
<br>&gt; &gt;&gt;provided.<br>&gt; &gt;&gt;<br>&gt; &gt;&gt;Until now, I used method 1 with success. Like this:<br>&gt; &gt;&gt;CustomDatatType m_MemberImage.;<br>&gt; &gt;&gt;m_MemberImage.setData(<br>&gt; &gt;&gt;itkFilter-&gt;GetOutput()-&gt;GetPixelContainer()-&gt;GetImportPointer() );
<br>&gt; &gt;&gt;itkFilter-&gt;GetOutput()-&gt;GetPixelContainer()-&gt;SetContainerManageMemory(false);<br>&gt; &gt;&gt;<br>&gt; &gt;&gt;However this only works when I have a scalar voxel type and now I have<br>&gt; &gt;&gt;created a pipeline that produces a &nbsp;itk::Image&lt; itk::Vector&lt; float, 3
<br>&gt; &gt;&gt;<br>&gt; &gt;&gt;&gt;, 3&gt; image. The creation of the image and the passing of the pointer<br>&gt; &gt;&gt;<br>&gt; &gt;&gt;to my own data structure works fine. But as soon as I try to release<br>&gt; &gt;&gt;the memory, my program crashes. This does not happen when the voxel
<br>&gt; &gt;&gt;type is of type int which is still unexplained.<br>&gt; &gt;&gt;<br>&gt; &gt;&gt;I used the second method as mentioned by Luis like this:<br>&gt; &gt;&gt;<br>&gt; &gt;&gt; &nbsp; CustomDatatType m_MemberImage;
<br>&gt; &gt;&gt; &nbsp; // Set size of m_MemberImage and allocate memory<br>&gt; &gt;&gt;<br>&gt; &gt;&gt; &nbsp; // Set size of itk output image<br>&gt; &gt;&gt; &nbsp; region.SetSize( size );<br>&gt; &gt;&gt; &nbsp; region.SetIndex( start );
<br>&gt; &gt;&gt; &nbsp; itkFilter-&gt;GetOutput(1)-&gt;SetRegions( region);<br>&gt; &gt;&gt;<br>&gt; &gt;&gt; &nbsp; // Set pointer of filter image to allocated data<br>&gt; &gt;&gt; &nbsp; ItkFilterType::VectorVoxelType * pixelData;<br>
&gt; &gt;&gt; &nbsp; pixelData = static_cast&lt;ItkFilterType::VectorVoxelType* &gt;(<br>&gt; &gt;&gt;m_MemberImage.getData() );<br>&gt; &gt;&gt; &nbsp; itkFilter-&gt;GetOutput(1)-&gt;<br>&gt; &gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetPixelContainer()-&gt;
<br>&gt; &gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetImportPointer(pixelData, m_MemberImage.getSize(), false);<br>&gt; &gt;&gt;<br>&gt; &gt;&gt;Using this method, there is still a problem with the vector voxel<br>&gt; &gt;&gt;typed image. Using an image with int voxels things works fine, but as
<br>&gt; &gt;&gt;soon as the mentioned vector voxel type is used, access violations<br>&gt; &gt;&gt;occur when the itk filter writes to the output buffer. This suggests<br>&gt; &gt;&gt;that somehow the size of the buffer or the datatype is not set
<br>&gt; &gt;&gt;correctly. I checked both and they are ok.<br>&gt; &gt;&gt;<br>&gt; &gt;&gt;My questions:<br>&gt; &gt;&gt;- Could someone comment on the difference in deleting a scalar and<br>&gt; &gt;&gt;vector voxel typed image buffer.
<br>&gt; &gt;&gt;- Is there more generally, any difference in the memory management of<br>&gt; &gt;&gt;scalar and multiple component voxel typed images?<br>&gt; &gt;&gt;- Are there any suggestions on where to look for the problem in the
<br>&gt; &gt;&gt;second approach?<br>&gt; &gt;&gt;<br>&gt; &gt;&gt;Thanks a lot.<br>&gt; &gt;&gt;<br>&gt; &gt;<br>&gt; &gt; _______________________________________________<br>&gt; &gt; Insight-users mailing list<br>&gt; &gt; 
<a href="mailto:Insight-users@itk.org">Insight-users@itk.org</a><br>&gt; &gt; <a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>&gt; &gt;<br>
&gt;<br>_______________________________________________<br>Insight-users mailing list<br><a href="mailto:Insight-users@itk.org">Insight-users@itk.org</a><br><a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">
http://www.itk.org/mailman/listinfo/insight-users</a><br></div></div></blockquote></div><br>