big confusion

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

    #1

    big confusion

    Hello,

    I have 3 form (e.g. A, B, C). Form A has collection that is passed to Form B which in turn pass that to Form C. Now this is reference rather than a copy. Now I am creating my object from my class in Form C and conditionally adding it to collection in Form A. After adding that object I dispose Form C, and user can close Form B as they want or continue with other things.

    Assuming: when I add my object to the collection in Form A it is added as reference in the list of collection (as it is a big object and no copy is happening so collection will add the ref for the object in its collection list).

    Question is, when I dispose Form C will the my-object that I created in Form C still exsists, and in turn some how Form C is also not disposed (GCed) as my object is part of it?

    What is really happening behind?

    Thanks,
    Po
  • Light

    #2
    Re: big confusion

    I may get this wrong but in "smart pointer" terms then what would
    happen is you have an object A and each time you make a copy of it
    (reference) the the count is incrememented. The object is not clear
    for collection until that count reaches 0.

    There are cases of circular relationships and relationships involving
    events that can arise. These are a bit too complex for me to explain
    without reviewing them but it is possible to have the dead listeners.

    However, when you refer to "created in form C" then you need to look at
    the scope that the object lives in. If that object is still valid then
    yes it will still be alive or else it wont. But will form C be valid
    then only if there is an object that is in scope.

    I think you are confusing the class data and object data. I think the
    answer is no to your question but not sure I fully understood it.
    Curtis

    Premier Popup Notepad & Help System For Developers

    Comment

    • Mark R. Dawson

      #3
      RE: big confusion

      Hi Pohihihi,
      if I understand correctly you are saying that if you have a collection in
      form A and add an item to the same collection from form C then form C get
      GCed what happens.

      Basically how the GC works is that it creates a graph of relationships
      from object roots, such as local variables currently in scope, items on the
      stack, static objects and goes and finds all of the object that can be
      reached from these roots. Objects that cannot be reached from any root are
      eligable for garbage collection since there is nothing that is referencing
      them. In your case, becauase you added the object in Form C to the
      collection, the collection now has a reference to that object, even if FormC
      goes away the garbage collector knows that the object you added still is
      referenced and accessbile and it will not be collected.

      I hope that answers your question.

      Mark Dawson





      "Pohihihi" wrote:
      [color=blue]
      > Hello,
      >
      > I have 3 form (e.g. A, B, C). Form A has collection that is passed to Form B which in turn pass that to Form C. Now this is reference rather than a copy. Now I am creating my object from my class in Form C and conditionally adding it to collection in Form A. After adding that object I dispose Form C, and user can close Form B as they want or continue with other things.
      >
      > Assuming: when I add my object to the collection in Form A it is added as reference in the list of collection (as it is a big object and no copy is happening so collection will add the ref for the object in its collection list).
      >
      > Question is, when I dispose Form C will the my-object that I created in Form C still exsists, and in turn some how Form C is also not disposed (GCed) as my object is part of it?
      >
      > What is really happening behind?
      >
      > Thanks,
      > Po[/color]

      Comment

      • Truong Hong Thi

        #4
        Re: big confusion

        >Question is, when I dispose Form C will the my-object that I created in Form C still >exsists, and in turn some how Form C is also not disposed (GCed) as my object is part >of it?
        Form C and my-object are allocated separately on the managed-heap. The
        fact that Form C holds a reference to my-object does not make it to be
        collected when Form C is collected. my-object memory location is still
        referenced by the collection which in turn referenced by Form A so it
        is not eligible for being collected.

        Comment

        • Pohihihi

          #5
          Re: big confusion

          If I understand even Form C is collected my-object will be alive. What part
          is not clear is that will Form C go away or will hang around until my-object
          is alive. As per Truong's reply what I get is that object will still be
          alive and Form C will be collected. If that is so then I get the answer
          correctly. Thanks,


          "Mark R. Dawson" <MarkRDawson@di scussions.micro soft.com> wrote in message
          news:D462B729-EAFD-4174-915A-2F5C9BAAE49D@mi crosoft.com...[color=blue]
          > Hi Pohihihi,
          > if I understand correctly you are saying that if you have a collection in
          > form A and add an item to the same collection from form C then form C get
          > GCed what happens.
          >
          > Basically how the GC works is that it creates a graph of relationships
          > from object roots, such as local variables currently in scope, items on
          > the
          > stack, static objects and goes and finds all of the object that can be
          > reached from these roots. Objects that cannot be reached from any root
          > are
          > eligable for garbage collection since there is nothing that is referencing
          > them. In your case, becauase you added the object in Form C to the
          > collection, the collection now has a reference to that object, even if
          > FormC
          > goes away the garbage collector knows that the object you added still is
          > referenced and accessbile and it will not be collected.
          >
          > I hope that answers your question.
          >
          > Mark Dawson
          > http://www.markdawson.org
          >
          >
          >
          >
          > "Pohihihi" wrote:
          >[color=green]
          >> Hello,
          >>
          >> I have 3 form (e.g. A, B, C). Form A has collection that is passed to
          >> Form B which in turn pass that to Form C. Now this is reference rather
          >> than a copy. Now I am creating my object from my class in Form C and
          >> conditionally adding it to collection in Form A. After adding that object
          >> I dispose Form C, and user can close Form B as they want or continue with
          >> other things.
          >>
          >> Assuming: when I add my object to the collection in Form A it is added as
          >> reference in the list of collection (as it is a big object and no copy is
          >> happening so collection will add the ref for the object in its collection
          >> list).
          >>
          >> Question is, when I dispose Form C will the my-object that I created in
          >> Form C still exsists, and in turn some how Form C is also not disposed
          >> (GCed) as my object is part of it?
          >>
          >> What is really happening behind?
          >>
          >> Thanks,
          >> Po[/color][/color]


          Comment

          • Truong Hong Thi

            #6
            Re: big confusion

            Yes, that is it. Whenever an object is created with the "new" operator,
            it is allocated on the managed heap. It does not matter which
            class/method creates it. When the object becomes unreachable (no
            references to it from alive objects), it is eligible for garbage
            collection. The fact that my-object is created in one of the Form C's
            methods does not make any sense to the GC.

            Thi

            Comment

            • Pohihihi

              #7
              Re: big confusion

              Thanks,


              "Truong Hong Thi" <thi1981@gmail. com> wrote in message
              news:1137135708 .974409.31470@g 44g2000cwa.goog legroups.com...[color=blue]
              > Yes, that is it. Whenever an object is created with the "new" operator,
              > it is allocated on the managed heap. It does not matter which
              > class/method creates it. When the object becomes unreachable (no
              > references to it from alive objects), it is eligible for garbage
              > collection. The fact that my-object is created in one of the Form C's
              > methods does not make any sense to the GC.
              >
              > Thi
              >[/color]


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: big confusion

                Truong Hong Thi wrote:[color=blue][color=green]
                > >Question is, when I dispose Form C will the my-object that I created in Form C still
                > > exsists, and in turn some how Form C is also not disposed (GCed) as my object is part
                > >of it?[/color][/color]
                [color=blue]
                > Form C and my-object are allocated separately on the managed-heap. The
                > fact that Form C holds a reference to my-object does not make it to be
                > collected when Form C is collected. my-object memory location is still
                > referenced by the collection which in turn referenced by Form A so it
                > is not eligible for being collected.[/color]

                It's very hard to follow exactly what you're talking about without
                concrete code. Could you provide a short but complete code example of
                what's happening and state what you *want* to happen?

                Jon

                Comment

                Working...