FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

PictureBox -> Bitmap ownership

 
Post new topic   Reply to topic     Forum Index -> DFL
View previous topic :: View next topic  
Author Message
tim



Joined: 24 Jul 2005
Posts: 26

PostPosted: Mon Jul 25, 2005 4:24 am    Post subject: PictureBox -> Bitmap ownership Reply with quote

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
View user's profile Send private message
tim



Joined: 24 Jul 2005
Posts: 26

PostPosted: Mon Jul 25, 2005 4:27 am    Post subject: Reply with quote

Oh, I forgot I had access to the source code, very handy! Smile 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
View user's profile Send private message
Chris Miller



Joined: 27 Mar 2004
Posts: 514
Location: The Internet

PostPosted: Mon Jul 25, 2005 12:26 pm    Post subject: Reply with quote

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
View user's profile Send private message
tim



Joined: 24 Jul 2005
Posts: 26

PostPosted: Mon Jul 25, 2005 1:07 pm    Post subject: Reply with quote

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... Confused Or is .dispose() not the right method to call at all / should never be manually called?
Back to top
View user's profile Send private message
Chris Miller



Joined: 27 Mar 2004
Posts: 514
Location: The Internet

PostPosted: Mon Jul 25, 2005 1:17 pm    Post subject: Reply with quote

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... Confused 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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> DFL All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group