Release resources used in displaying images.

I have inherited a very, very large c# program this displays images using Windows Controls.Add(catalogItem) to display a group of images related to the image that is clicked. What I cannot figure out is how to release the Handles/User Objects when the images are no longer displayed.
My program crashes, of course, when 10,000+ User objects are displayed. No, they are not displayed all at once, but a group of as many as 70 are displayed at one time.
What I would like to do is return/clean up those resources when the [Back] button is clicked.
I have tried using a Controls.Remove(catalogItem) call, and it does remove the picture from view, but it does not release the resources internally.
I am using Visual Studio Community 2017.
Thanks!!!
Kenn SimonsSenior Programmer/AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dustin SaundersDirector of OperationsCommented:
When you're done with an object, if it implements IDisposable you can call .Dispose() to destroy it.

C# garbage collection does a good job of keeping things clean, you can always call that with:

GC.Collect();

Open in new window


and if you need to suspend the thred until it completes

GC.WaitForPendingFinalizers();

Open in new window


See:
https://msdn.microsoft.com/en-us/library/system.gc(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx
Dustin SaundersDirector of OperationsCommented:
Also note that when possible that the statement

using (Object object) { }

Open in new window

will safely dispose of the object when you finish with it.

See:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement
Kenn SimonsSenior Programmer/AnalystAuthor Commented:
@Dustin Saunders: I have tried using GC, but it does not release the User object handle (if the is the correct term). What I think I need is some way of looping through the same "list" that was used to render the images and text descriptions on screen, and remove them before the new "list" is displayed. Does that make sense?
I have a catalog of items that displays the details of a group of items, depending on the image clicked. When the Back button is clicked, that is when I think I need to release/remove the list of User object handles.
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

käµfm³d 👽Commented:
garbage collection does a good job of keeping things clean, you can always call that with
So good, in fact, that you should really never have to call it directly. It should be exceedingly rare that you force a GC collect on your own.

If something is not getting released, then it could be that it is unmanaged memory. The GC can't do anything about unmanaged memory. You have to properly clean up that yourself (and by "yourself" I mean whoever wrote the code). Window/control handles are unamanaged memory. Those have to be cleaned up using Win API calls (if memory serves).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kenn SimonsSenior Programmer/AnalystAuthor Commented:
@käµfm³d: OK, I will try that approach.  Write a foreach loop to release each component and see if that releases the handle.
Dustin SaundersDirector of OperationsCommented:
Appropriate answers/information regarding the problem.
Kenn SimonsSenior Programmer/AnalystAuthor Commented:
Nothing I have tried so far releases the objects. I have been tasked with another problem at the moment, so I will return to this one as soon as I can.
Thank you @käµfm³d and Dustin for your suggestions.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.