View previous topic :: View next topic |
Author |
Message |
tim
Joined: 24 Jul 2005 Posts: 26
|
Posted: Mon Jul 25, 2005 4:24 am Post subject: PictureBox -> Bitmap ownership |
|
|
Hi,
I created the following demo:
Code: |
import dfl.all;
class MainForm: Form
{
PictureBox picBox;
this()
{
text = "Picturebox demo";
picBox = new PictureBox;
picBox.setBounds(0, 0, 100, 100);
picBox.borderStyle = BorderStyle.FIXED_SINGLE;
picBox.image = new Bitmap("logo.bmp");
picBox.sizeMode = PictureBoxSizeMode.AUTO_SIZE;
picBox.parent = this;
layoutChildren();
}
void layoutChildren()
{
int x = clientSize.width / 2 - picBox.width / 2;
int y = clientSize.height / 2 - picBox.height / 2;
picBox.setBounds(x, y, picBox.width, picBox.height);
}
protected void onResize(EventArgs args)
{
layoutChildren();
}
}
int main()
{
Application.run(new MainForm());
return 0;
}
|
When you assign to the bitmap property of the PictureBox, does it automatically take ownership?
Or do you have to call .dispose() yourself?
Thanks,
-Tim |
|
Back to top |
|
|
tim
Joined: 24 Jul 2005 Posts: 26
|
Posted: Mon Jul 25, 2005 4:27 am Post subject: |
|
|
Oh, I forgot I had access to the source code, very handy! It seems it does not take ownership.
My question now is: Is the image garbage collected automatically or do I have to call dispose() manually? |
|
Back to top |
|
|
Chris Miller
Joined: 27 Mar 2004 Posts: 514 Location: The Internet
|
Posted: Mon Jul 25, 2005 12:26 pm Post subject: |
|
|
tim wrote: | My question now is: Is the image garbage collected automatically or do I have to call dispose() manually? |
It will be collected. However, if you are creating a lot of images, you may wish to manually trigger the collector with std.gc.fullCollect() so that it will reclaim the old ones, as the collector can take awhile to kick in sometimes. This is especially important on win9x, which has limited resources.
I wrote a program that created a new icon for a lot of rapid events and the task manager said I had thousands of GDI objects. Inserting manual collection fixed it nicely.
In the future, DFL may automatically trigger collections from time to time; I just need to decide where/when/if it will. |
|
Back to top |
|
|
tim
Joined: 24 Jul 2005 Posts: 26
|
Posted: Mon Jul 25, 2005 1:07 pm Post subject: |
|
|
I don't have access to my development machine right now and cannot take a look at the source code, but is it better to invoke the garbage collector to recycle all memory than individually releasing resources such as images manually by calling .dispose()?
The first option seems a little wasteful... Or is .dispose() not the right method to call at all / should never be manually called? |
|
Back to top |
|
|
Chris Miller
Joined: 27 Mar 2004 Posts: 514 Location: The Internet
|
Posted: Mon Jul 25, 2005 1:17 pm Post subject: |
|
|
tim wrote: | I don't have access to my development machine right now and cannot take a look at the source code, but is it better to invoke the garbage collector to recycle all memory than individually releasing resources such as images manually by calling .dispose()?
The first option seems a little wasteful... Or is .dispose() not the right method to call at all / should never be manually called? |
Well, the GC is safer. If other pieces of code are still using that image and you dispose it, you just trashed it on them. But it's up to you. It's kind of like COW. |
|
Back to top |
|
|
|