5 Replies - 691 Views - Last Post: 01 June 2012 - 01:26 AM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

When you dispose of an objects is the objects objects Disposed Too?

Posted 30 May 2012 - 06:45 PM

What I want to know is if I have a panel, tabpage etc. object that I add a control too at runtime like a richtextbox button etc. If that object is disposed to save memory later, are all the containing objects of that object like a richtextbox in a tabpage are they ALSO DISPOSED too?
Is This A Good Question/Topic? 0
  • +

Replies To: When you dispose of an objects is the objects objects Disposed Too?

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4479
  • View blog
  • Posts: 7,801
  • Joined: 08-June 10

Re: When you dispose of an objects is the objects objects Disposed Too?

Posted 30 May 2012 - 08:51 PM

*
POPULAR

Well, I don't know off the top of my head, so a test!

private static void FormTest() {
    var form = new DisposalForm();

    var button1 = new Button();
    form.Controls.Add(button1);

    var tabControl = new TabControl();
    form.Controls.Add(tabControl);

    var tabPage = new TabPage();
    tabControl.Controls.Add(tabPage);
    
    var button2 = new Button();
    tabPage.Controls.Add(button2);

    Print(tabPage, button2, tabControl, form, button1);

    Console.WriteLine("\nDisposing Form.\n");
    form.Dispose();

    Print(tabPage, button2, tabControl, form, button1);
}

private static void Print(TabPage tabPage, Button button2, TabControl tabControl, DisposalForm form, Button button1) {
    Console.WriteLine("form: {0}", form.IsDisposed);
    Console.WriteLine("button1: {0}", button1.IsDisposed);
    Console.WriteLine("tabControl: {0}", tabControl.IsDisposed);
    Console.WriteLine("tabPage: {0}", tabPage.IsDisposed);
    Console.WriteLine("button2: {0}", button2.IsDisposed);
}


Output:

Quote

Designer test
form: False
button1: False
tabControl: False
tabPage: False
button2: False

Disposing Form.

form: True
button1: True
tabControl: True
tabPage: True
button2: True


So, looks like it.
Was This Post Helpful? 6
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Re: When you dispose of an objects is the objects objects Disposed Too?

Posted 30 May 2012 - 10:47 PM

Thanks Curtis. I don't know why I didn't think to do that. I appreciate it.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5507
  • View blog
  • Posts: 11,808
  • Joined: 02-June 10

Re: When you dispose of an objects is the objects objects Disposed Too?

Posted 31 May 2012 - 06:31 AM

View PostCurtis Rutland, on 30 May 2012 - 09:51 PM, said:

Well, I don't know off the top of my head, so a test!


Wow... Doing your own testing and experimenting instead of going on line and asking others... How'd you think of that?

Is there some way we can drill this into the heads of all the rookies? Post it on the home page in 72 point font? Make it required reading before being able to post a question?

If I could +20 you for this I would.
Was This Post Helpful? 0
  • +
  • -

#5 jjames967  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 01-January 12

Re: When you dispose of an objects is the objects objects Disposed Too?

Posted 31 May 2012 - 11:52 PM

In my understanding of C#... it's very similar to Java, where objects are managed by the garbage collector. When an object is no longer being used it goes to the collector, which disposes of the objects automatically when parent objects/classes are closed. Which is very nice, compared to C++ where you had to manually dispose of an object, otherwise it might be left in memory including pointer objects. Which of course, causes memory-overloading. Nasty stuff.
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2112
  • View blog
  • Posts: 3,230
  • Joined: 21-June 11

Re: When you dispose of an objects is the objects objects Disposed Too?

Posted 01 June 2012 - 01:26 AM

View Postjjames967, on 01 June 2012 - 08:52 AM, said:

In my understanding of C#... it's very similar to Java, where objects are managed by the garbage collector. When an object is no longer being used it goes to the collector, which disposes of the objects automatically when parent objects/classes are closed. Which is very nice, compared to C++ where you had to manually dispose of an object, otherwise it might be left in memory including pointer objects. Which of course, causes memory-overloading. Nasty stuff.


You didn't really answer the question. That's fine since the question was already answered, but I'm not sure what your post adds to the discussion (or how it even relates to it for that matter).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1