How to serialize in "destructor" ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Trevor Balcom

    How to serialize in "destructor" ?

    I have a class which has a member variable of the type System.ArrayLis t.
    I plan on having the ArrayList serialize/deserialize itself to/from XML. I
    have worked out the code to do this and have the ArrayList is currently
    deserializing in the constructor. When is the correct time to serialize the
    ArrayList to XML? In C++ I would do this in the destructor. I see C# has a
    destructor, but I am unsure about all of this IDisposable, Finalize, Dispose
    and such. When would I perform cleanup code? Could someone explain these
    terms to me ? Are there any source examples on MSDN to explain this ?


  • Lucean Morningside

    #2
    Re: How to serialize in "destructo r" ?

    "Trevor Balcom" <tbalcom@attbi. co> wrote in message news:<#e50hzhTD HA.212@TK2MSFTN GP10.phx.gbl>.. .[color=blue]
    > I have a class which has a member variable of the type System.ArrayLis t.
    > I plan on having the ArrayList serialize/deserialize itself to/from XML. I
    > have worked out the code to do this and have the ArrayList is currently
    > deserializing in the constructor. When is the correct time to serialize the
    > ArrayList to XML? In C++ I would do this in the destructor. I see C# has a
    > destructor, but I am unsure about all of this IDisposable, Finalize, Dispose
    > and such. When would I perform cleanup code? Could someone explain these
    > terms to me ? Are there any source examples on MSDN to explain this ?[/color]

    You shouldn't even consider seralizing in the finalizer. As far as I know, when
    the finalizer is executed, you can't guarantee that all objects that would have
    been part of your object graph would still be available in memory. You might
    consider seralizing explicitly before program termination. Or periodically if
    that is not possible.

    You might want to further explain the current porblem that you are trying to
    solve here.

    -LM

    Comment

    • Jon Skeet

      #3
      Re: How to serialize in &quot;destructo r&quot; ?

      Lucean Morningside <m@exquisitor.c om> wrote:[color=blue]
      > You shouldn't even consider seralizing in the finalizer. As far as I know, when
      > the finalizer is executed, you can't guarantee that all objects that would have
      > been part of your object graph would still be available in memory.[/color]

      I believe you can - they're still strongly reachable while your object
      is strongly reachable. However, they may have been finalized before you
      have, and generally not be in a very useful state. I agree it's not a
      good idea to do anything like serialization in the finalizer though.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      Working...