www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Memory errors

reply "PhilE" <theotherphil hotmail.com> writes:
When using both simpleimage and DWT my program crashes with 
"core.exception.InvalidMemoryOperationError" after I show an 
image in a GUI window and then close the window. I'm presumably 
misunderstanding something about how the windows deal with memory 
deallocation, but this happens even when copying tutorial 
examples.

e.g.

module main;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.graphics.all;

void main ()
{
     auto display = new Display;
     auto shell = new Shell;

     shell.setText("Title");

     Label label = new Label (shell, SWT.BORDER);
     label.setBounds(shell.getClientArea());

     Image image = new Image(display, "C:/dev/data/tree.png");
     label.setImage(image);

     shell.pack();
     shell.open();

     while (!shell.isDisposed)
         if (!display.readAndDispatch())
             display.sleep();

     display.dispose();
}

This successfully creates the display and shows an image, but 
when I close the window the memory error exception is thrown. If 
I run the same code without creating the image then there's no 
errors. If I create the image without setting it on the label I 
still get the error. Any ideas?
Mar 16 2014
next sibling parent "PhilE" <theotherphil hotmail.com> writes:
Ah, I need to dispose of the image before disposing of the 
display.

On Sunday, 16 March 2014 at 19:33:09 UTC, PhilE wrote:
 When using both simpleimage and DWT my program crashes with 
 "core.exception.InvalidMemoryOperationError" after I show an 
 image in a GUI window and then close the window. I'm presumably 
 misunderstanding something about how the windows deal with 
 memory deallocation, but this happens even when copying 
 tutorial examples.

 e.g.

 module main;

 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.graphics.all;

 void main ()
 {
     auto display = new Display;
     auto shell = new Shell;

     shell.setText("Title");

     Label label = new Label (shell, SWT.BORDER);
     label.setBounds(shell.getClientArea());

     Image image = new Image(display, "C:/dev/data/tree.png");
     label.setImage(image);

     shell.pack();
     shell.open();

     while (!shell.isDisposed)
         if (!display.readAndDispatch())
             display.sleep();

     display.dispose();
 }

 This successfully creates the display and shows an image, but 
 when I close the window the memory error exception is thrown. 
 If I run the same code without creating the image then there's 
 no errors. If I create the image without setting it on the 
 label I still get the error. Any ideas?
Mar 16 2014
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 3/16/14, PhilE <theotherphil hotmail.com> wrote:
 When using both simpleimage and DWT my program crashes with
 "core.exception.InvalidMemoryOperationError"
It's likely something is trying to allocate in a destructor.
Mar 16 2014