<div dir="ltr">There are still issues with garbage collection that we have partial solutions for, but haven't put into master yet that may be causing issues, but you'd be pretty unlucky. Usually such failures are rare until the garbage collection torture is enabled.<div><br></div><div>I don't think your observer function is quite right - the image registration one </div><div><br></div><div><table class="" style="border-collapse:collapse;border-spacing:0px;color:rgb(51,51,51);font-family:Helvetica,arial,nimbussansl,liberationsans,freesans,clean,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:13px;line-height:18px"><tbody style=""><tr style=""></tr><tr style=""><td id="LC27" class="" style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre"><span class="" style="color:rgb(121,93,163)">commandIteration</span> <span class="" style="color:rgb(167,29,93)"><-</span> <span class="" style="color:rgb(167,29,93)">function</span>(<span class="" style="">method</span>)</td></tr><tr style=""><td id="L28" class="" style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td id="LC28" class="" style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">{</td></tr><tr style=""><td id="L29" class="" style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td id="LC29" class="" style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">     <span class="" style="color:rgb(121,93,163)">res</span> <span class="" style="color:rgb(167,29,93)"><-</span> <span class="" style="color:rgb(167,29,93)">function</span>() {</td></tr><tr style=""><td id="L30" class="" style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td id="LC30" class="" style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">     <span class="" style="">msg</span> <span class="" style="color:rgb(167,29,93)"><-</span> paste(<span class="" style="color:rgb(24,54,145)"><span class="" style="">"</span>Optimizer iteration number <span class="" style="">"</span></span>, <span class="" style="">method</span><span class="" style="color:rgb(167,29,93)">$</span>GetOptimizerIteration(),</td></tr><tr style=""><td id="L31" class="" style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td id="LC31" class="" style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">                  <span class="" style="color:rgb(24,54,145)"><span class="" style="">"</span> = <span class="" style="">"</span></span>, <span class="" style="">method</span><span class="" style="color:rgb(167,29,93)">$</span>GetMetricValue(), <span class="" style="color:rgb(24,54,145)"><span class="" style="">"</span> : <span class="" style="">"</span></span>, <span class="" style="">method</span><span class="" style="color:rgb(167,29,93)">$</span>GetOptimizerPosition(),</td></tr><tr style=""><td id="L32" class="" style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td id="LC32" class="" style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">                  <span class="" style="color:rgb(24,54,145)"><span class="" style="">"</span><span class="" style="">\n</span><span class="" style="">"</span></span> )</td></tr><tr style=""><td id="L33" class="" style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td id="LC33" class="" style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">     cat(<span class="" style="">msg</span>)</td></tr><tr style=""><td id="L34" class="" style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td id="LC34" class="" style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">    }</td></tr><tr style=""><td id="L35" class="" style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td id="LC35" class="" style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">    <span class="" style="color:rgb(167,29,93)">return</span>(<span class="" style="">res</span>)</td></tr><tr style=""><td id="L36" class="" style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td id="LC36" class="" style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">}</td></tr><tr style=""><td id="L37" class="" style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td id="LC37" class="" style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">
</td></tr><tr style=""><td id="L38" class="" style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td></tr></tbody></table>Note that it is returning a function created internally. This is a "closure" in R (I think) and the trick is that it retains a copy of the environment, which includes the input argument.</div><div><br></div><div>Yours example is passing the result of print, which isn't a function. This kind of error is meant to raise an exception, rather than crash ungracefully, so there may be some more work to do there.</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Apr 10, 2016 at 11:10 AM, Mathew Guilfoyle <span dir="ltr"><<a href="mailto:mrguilfoyle@gmail.com" target="_blank">mrguilfoyle@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">There seems to be a problem with using the $AddCommand function to ImageRegistrationMethod objects in the R wrapping to SimpleITK.<br>
<br>
Using R 3.2.4 on OS X 10.11<br>
<br>
Example:<br>
<br>
f <- function(x){print(x$GetMetricValue())}<br>
<br>
registration = ImageRegistrationMethod()<br>
<br>
registration$AddCommand(‘sitkIterationEvent’, f(registration))<br>
<br>
This last line causes R to abort/hard crash (no error message).  This behaviour is irrespective of which event type I use or the function passed as the second argument (e.g. even something simple like print(‘Hello’))<br>
<br>
The AddCommand method is inherited from the SimpleITK ProcessObject type.  Digging around in the SimpleITK.R file I can see that the ProcessObject$AddCommand method expects a second argument of type ‘_p_itk__simple__Command’.<br>
<br>
There is a ‘Command’ method that generates a new object of type ‘_p_itk__simple__Command’.  If I pass this resulting object as the second argument to registration$AddCommand then I do not get an error.  However, there doesn’t seem to be any way to point the ‘_p_itk__simple__Command’ object to my function or otherwise convert my function to a ‘_p_itk__simple__Command’ type.<br>
<br>
The $AddCommand method is used in a couple of example scripts on the SimpleITK site (using it for monitoring a registration and a filter e.g. <a href="https://github.com/SimpleITK/SimpleITK/blob/master/Examples/FilterProgressReporting.R" rel="noreferrer" target="_blank">https://github.com/SimpleITK/SimpleITK/blob/master/Examples/FilterProgressReporting.R</a>) so I don’t know if it was working previously and a bug has been introduced more recently? (the example scripts cause the same error when I run them now)<br>
<br>
<br>
<br>
_____________________________________<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at<br>
<a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Kitware offers ITK Training Courses, for more information visit:<br>
<a href="http://www.kitware.com/products/protraining.php" rel="noreferrer" target="_blank">http://www.kitware.com/products/protraining.php</a><br>
<br>
Please keep messages on-topic and check the ITK FAQ at:<br>
<a href="http://www.itk.org/Wiki/ITK_FAQ" rel="noreferrer" target="_blank">http://www.itk.org/Wiki/ITK_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/insight-users" rel="noreferrer" target="_blank">http://public.kitware.com/mailman/listinfo/insight-users</a><br>
</blockquote></div><br></div>