Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Disposing of object used in a UserControl...

Disposing of object used in a UserControl...

Scheduled Pinned Locked Moved C#
questionhelp
13 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Luc Pattyn

    Hi Pete, I always have been, and still am, struggling with this question. Here is a simple example: I create a UserControl that holds a PictureBox; I create a modal Form holding two such UserControls and some Buttons Initially the Form loads one image and shows it in both UC. then: scenario 1: I press a button and another image gets loaded in one of the UCs (the original image remains in use in the other UC) scenario 2: I press another button and one UserControl should vanish (the image remains in use in the other UC) scenario 3: I press yet another button, it closes the Form (the image is no longer referenced). So who should be calling Image.Dispose()? It can't be the UC, as it does not "own" the image. It shouldn't be the Form, as it will be very vulnerable as soon as the use cases get a bit complex. So we need some pattern here that automates/hides the problem. FWIW: the problem vanishes if one never assigns the same image to different UCs, which implies each UC shows a different instance of Image (possibly showing the same thing though), but I want to avoid loading the same image twice if I can reliably do so. And I don't see how a WeakReference will help me; I know how to use them to possibly keep an object alive, as in a user-created cache. However here the PictureBoxes want to be sure they have the image at hand that they are supposed to show, nothing is supposed to be weak here. :confused:

    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


    I only read formatted code with indentation, so please use PRE tags for code snippets.


    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #4

    Wow, talk about going way beyond the original problem. Right, here are some thoughts based on work we've done with this in the past. If you want to show the same image in multiple locations then you don't make the lifetime of the image the responsibility of the items that are showing it. In other words, I'd have an ImageManager class that contained the image. When the controls want the image, then I would return a clone of that image to the control instead - the control can then happily dispose of that image. The lifetime of the ImageManager is then independent of the user control and can be maintained as a WeakReference. We have used this technique successfully in several projects and it's worked well for us.

    "WPF has many lovers. It's a veritable porn star!" - Josh Smith

    As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

    My blog | My articles | MoXAML PowerToys | Onyx

    L 1 Reply Last reply
    0
    • P Pete OHanlon

      Wow, talk about going way beyond the original problem. Right, here are some thoughts based on work we've done with this in the past. If you want to show the same image in multiple locations then you don't make the lifetime of the image the responsibility of the items that are showing it. In other words, I'd have an ImageManager class that contained the image. When the controls want the image, then I would return a clone of that image to the control instead - the control can then happily dispose of that image. The lifetime of the ImageManager is then independent of the user control and can be maintained as a WeakReference. We have used this technique successfully in several projects and it's worked well for us.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #5

      Yeah, that is what I was afraid of, another Manager. :laugh: Thanks. I was still hoping a ready-made solution existed. Acutally, you now said it can't be the UC's responsibility. That is a change of heart from before[^]. The UC, being a component, is open to reuse.

      Pete O'Hanlon wrote:

      going way beyond the original problem

      curiosity. the ambition to learn. generalization. advancement. you name it. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read formatted code with indentation, so please use PRE tags for code snippets.


      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


      P 1 Reply Last reply
      0
      • L Luc Pattyn

        Yeah, that is what I was afraid of, another Manager. :laugh: Thanks. I was still hoping a ready-made solution existed. Acutally, you now said it can't be the UC's responsibility. That is a change of heart from before[^]. The UC, being a component, is open to reuse.

        Pete O'Hanlon wrote:

        going way beyond the original problem

        curiosity. the ambition to learn. generalization. advancement. you name it. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #6

        Luc Pattyn wrote:

        another Manager

        You can never have enough managers. The UK is proof how well that works. :rolleyes:

        Luc Pattyn wrote:

        curiosity. the ambition to learn. generalization. advancement. you name it.

        Hoo yeah. I'm almost tempted to write a blog post demonstrating this.

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        L 1 Reply Last reply
        0
        • P Pete OHanlon

          Luc Pattyn wrote:

          another Manager

          You can never have enough managers. The UK is proof how well that works. :rolleyes:

          Luc Pattyn wrote:

          curiosity. the ambition to learn. generalization. advancement. you name it.

          Hoo yeah. I'm almost tempted to write a blog post demonstrating this.

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #7

          Pete O'Hanlon wrote:

          I'm almost tempted to write a blog post demonstrating this.

          what else do you need? I'll be reading it! :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read formatted code with indentation, so please use PRE tags for code snippets.


          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


          P 2 Replies Last reply
          0
          • L Luc Pattyn

            Pete O'Hanlon wrote:

            I'm almost tempted to write a blog post demonstrating this.

            what else do you need? I'll be reading it! :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read formatted code with indentation, so please use PRE tags for code snippets.


            I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #8

            Luc Pattyn wrote:

            what else do you need?

            Time.

            Luc Pattyn wrote:

            I'll be reading it!

            Ah. A reader. Excellent.

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            1 Reply Last reply
            0
            • L Luc Pattyn

              Pete O'Hanlon wrote:

              I'm almost tempted to write a blog post demonstrating this.

              what else do you need? I'll be reading it! :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read formatted code with indentation, so please use PRE tags for code snippets.


              I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #9

              I've posted an article demonstrating a simple weak referenced image library that demonstrates what I've been talking about. It's available here[^].

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              L 1 Reply Last reply
              0
              • P Pete OHanlon

                I've posted an article demonstrating a simple weak referenced image library that demonstrates what I've been talking about. It's available here[^].

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                My blog | My articles | MoXAML PowerToys | Onyx

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #10

                Hi Pete, thanks for the notification. BTW: you may want to check the downloads, they seem pretty empty at the moment. Regards,

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read formatted code with indentation, so please use PRE tags for code snippets.


                I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                P 1 Reply Last reply
                0
                • L Luc Pattyn

                  Hi Pete, thanks for the notification. BTW: you may want to check the downloads, they seem pretty empty at the moment. Regards,

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read formatted code with indentation, so please use PRE tags for code snippets.


                  I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #11

                  What??? Try now - for some reason the article editor put in a borked entry (or two).

                  "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                  As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                  My blog | My articles | MoXAML PowerToys | Onyx

                  L 1 Reply Last reply
                  0
                  • S Shy Agam

                    Hello experts, I'm currently developing a UserControl in which I have a browseable Image property. What is the correct approach to dispose this image? Should my UserControl dispose of it when it gets disposed? That would create a potential problem if the Image is used elsewhere around the application. But then again, not doing so means taking the chance that the image is not disposed. Thanks in advance, Shy.

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #12

                    Shy. Have a read of the article here[^]. It might help.

                    "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                    As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                    My blog | My articles | MoXAML PowerToys | Onyx

                    1 Reply Last reply
                    0
                    • P Pete OHanlon

                      What??? Try now - for some reason the article editor put in a borked entry (or two).

                      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                      My blog | My articles | MoXAML PowerToys | Onyx

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #13

                      All is well now. Thanks. :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      I only read formatted code with indentation, so please use PRE tags for code snippets.


                      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups