Posted by
Fred Damen on
Jan 10, 2021; 10:45pm
URL: http://imagej.273.s1.nabble.com/Issue-about-ImagePlus-tp5024349p5024350.html
Greetings Philippe,
Even though Java considers pointers evil and hides them from you, object
variables are essentially what C++ would call smart pointers. A smart
pointer is an middleman object that functions as a pointer and keeps a
reference count, so that when the count goes to zero the actual referred
object is deleted.
So in case one imp1,
first points to the ImagePlus that you open from a website,
then imp1.show increased the ref count on website ImagePlus,
then imp1.close decreased the ref count on website ImagePlus,
then existing run's scope decreased the ref count on website ImagePlus
to zero and thus allowing garbage collection to do its job.
In case two imp1,
first points to the ImagePlus that you open from a website,
then imp1.show increased the ref count on website ImagePlus,
then imp1 = HyperStack... causes imp1 to drop its reference to the
website ImagePlus from run's scope, decreasing ref count by one,
then imp1.close is probably a noop as HyperStack... ImagePlus has not
been show(en),
then exiting run's scope decreased the ref count on HyperStack...
ImagePlus to zero, thus allowing garbage collection to do its job.
N.B. website ImagePlus still has a ref count of one due to it being
show(en) and never having close called on it.
Not sure what you mean by "HyperStackConverter.toHyperStack method is
creating a static ImagePlus and not a ImagePlus" as there is no "static"
involved.
Enjoy,
Fred
On Sun, January 10, 2021 1:12 pm, CARL Philippe (LBP) wrote:
> Dear all,
> Happy new year for everybody (hoping that it will be better than the last
> one)!
> Empirically I found an issue that I'm not really able to understand.
> So the following code is working as expected (i.e. the imp1 gets closed as
> expected):
> public class My_Plugin1 implements PlugIn {
> ImagePlus imp1, imp2, imp3;
> public void run(String arg) {
> imp1 =
> IJ.openImage("
https://imagej.net/images/bat-cochlea-volume.zip");
> imp1.show();
> imp2 = HyperStackConverter.toHyperStack(imp1, 2, 3, 19, "default",
> "Grayscale");
> imp3 = SubHyperstackMaker.makeSubhyperstack(imp2, "1", "1-3", "1-19");
> imp3.show();
> imp1.close();
> imp2.close();
> }
> }
> But with the following code:
> public class My_Plugin2 implements PlugIn {
> ImagePlus imp1, imp3;
> public void run(String arg) {
> imp1 =
> IJ.openImage("
https://imagej.net/images/bat-cochlea-volume.zip");
> imp1.show();
> imp1 = HyperStackConverter.toHyperStack(imp1, 2, 3, 19, "default",
> "Grayscale");
> imp3 = SubHyperstackMaker.makeSubhyperstack(imp1, "1", "1-3", "1-19");
> imp3.show();
> imp1.close();
> }
> }
> the imp1 window isn't closed.
> Is this because the HyperStackConverter.toHyperStack method is creating a
> static ImagePlus and not a ImagePlus?
> I thank you very much in advance for your lighting on this.
> My best regards,
> Philippe
>
> --
> ImageJ mailing list:
http://imagej.nih.gov/ij/list.html>
--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html