Results 1 to 4 of 4

Thread: Create then dispose Images on the Fly

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2007
    Posts
    127

    Create then dispose Images on the Fly

    I read in images from a file location to a collection of bitmapimages and use the collection of bitmapimages as the Image.Source for image controls that i display on screen. However i seem to be getting bad memory leaks as the garbage collector does not seem to realize its time to dispose of the Image objects after they should be.

    Code:
     Dim x As Int32 = 0
                    For x = 1 To pictureCount
                        Dim picbox As New Image
                        Dim rand As Int32
                        Do
                            rand = RandomNumber(collectionPictures.Count, 1)
                        Loop While PicExists(rand) = True
                        picbox.Source = CType(collectionPictures.Item(rand), ImageSource)
                        picbox.Margin = New Thickness(5)
                        picbox.Name = "picBox" & rand.ToString
    
                        collectionUsedPics.Add(rand)
                        Grid.SetRow(picbox, picRow)
                        Grid.SetColumn(picbox, picCol)
                        Grid.SetRowSpan(picbox, 1)
                        If picCol = 1 Then
                            Grid.SetRowSpan(picbox, 2)
                            picbox.MaxHeight = 650
                        End If
                        If picCol = 1 And picRow = 1 Then
                            Grid.SetRow(picbox, 1)
                            Grid.SetColumn(picbox, 2)
                        End If
                        gridContent.Children.Add(picbox)
                        picCol = picCol + 1
                        If picCol > 2 Then
                            picCol = 0
                            picRow = picRow + 1
                        End If
                    Next x
                    Container.Children.Add(gridContent)
    i clear all the children in all the containers and it still doesnt dispose of the images, so the memory builds up to like 1000mb... anyone have any idea? thanks

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Create then dispose Images on the Fly

    An object will not be disposed as long as a reference to that object exists somewhere. Even if it's a reference to a reference to a reference to a reference of an object, that counts.

    I understand that this is a part of your code, so are there references to the images elsewhere? How are you clearing and disposing?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Create then dispose Images on the Fly

    It's also important to understand the difference between disposal and garbage collection. While they are related they are very much not the same thing. Memory leaks have nothing to do with disposal. Disposing an object means releasing its system resources. In this context, memory is not considered a system resource. We're talking about file handles and the like. You can, and should, dispose any object that supports it when you're finished with that object. The object still exists in memory after being disposed but is generally unusable. Once all references to an object have been removed, then that object is eligible for garbage collection. When the GC comes to clean up an object, i.e. reclaim the memory it occupies, it will first finalise the object if it hasn't already been disposed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2007
    Posts
    127

    Re: Create then dispose Images on the Fly

    i believe the problem was similar to what mendhak was saying. i used a collection of images to create the controls and thats when the garbage collector would not clean up the images i created so the memory would build. so i just read the files directly from the hd and now the the garbage collector does clean the image up after it is not being displayed. Thanks for your input.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width