dotnet / dotnet/winforms

[VB] PictureBox-images won’t get disposed automatically, even if the containing Form and PictureBox were disposed

Open
#9,135 3 comments 0 reactions 1 assignee Claimed by @JeremyKuhne View on GitHub
priority-3 tenet-performance
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

### .NET version

.NET 8.0.100-preview.5.23266.8

### Did it work in .NET Framework?

No

### Did it work in any of the earlier releases of .NET Core or .NET 5+?

No

### Issue description

https://github.com/dotnet/winforms/assets/86937911/e037cd8e-91eb-4d61-85e5-10c01f94ba2a

TestCoreapp:
[Testpicturecore.zip](https://github.com/dotnet/winforms/files/11498763/Testpicturecore.zip)

TestFrameworkapp:
[Testpictureframework.zip](https://github.com/dotnet/winforms/files/11498765/Testpictureframework.zip)

The initial feedback ticket is:
https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1817656

Customer Summary:
The images won’t get disposed, even if they should implicitly (!). I can dispose them explicitly, but if the programmers and software developers don’t know this, they can get serious problems with memory leaks. The PictureBox class implements the IDisposable interface, but it won’t get used by the Form-Class-Designer. So, as long as the app runs, useless image instances are still in memory.

### Steps to reproduce

1. Create a WinForms .NET Core VB project.
2. Add a button to form1 designer and set text property as "create another SubForm dialog instance".
3. Add a picturebox to form1 designer, and click smart tag, then click "choose image…" to import a picture to project resource file.
4. Add below code into Form1.vb.
```
Private F2 As Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
F2 = New Form2
F2.ShowDialog(Me)
F2.Dispose()
F2.PictureBox1.Dispose()
Me.PictureBox1.Image = F2.PictureBox1.Image
End Sub
```
5. Add a new VB Form named Form2.vb to same project.
6. Add flowlayoutpanel and picturebox control to form2 designer.
7. Add below code into Form2.vb.
```
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBox1.Image = My.Resources.ProfileImage
For i = 1 To 100
Dim NewPicBox As New PictureBox With {.Image = My.Resources.ProfileImage}
FlowLayoutPanel1.Controls.Add(NewPicBox)
Next
End Sub
```
8. Build and run the project.
9. Start the task manager to see the memory consumption of the app.
10. Click the "create another SubForm dialog instance" button -> memory usage about 61 MB.
![image](https://github.com/dotnet/winforms/assets/86937911/7ff89e56-7612-4c4c-a731-bb9a241b634f)
11. Close the SubForm window -> the PicBox image of the closed and disposed SubForm is still available in the main form.
12. The memory consumtion doesn’t change after closing and disposing the SubForm instance.
![image](https://github.com/dotnet/winforms/assets/86937911/178062ee-4567-4a19-bd83-e772185aa3cd)
13.Create another instance of the SubForm -> memory consumption raises to nearly 116 MB.
![image](https://github.com/dotnet/winforms/assets/86937911/c2f11a7f-0e48-427a-96f9-0a3edb90b4a3)
14. It raises further for every created but also closed SubForm instance.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.