Skip to content

Commit

Permalink
pdImage handler: fix issues with circular references
Browse files Browse the repository at this point in the history
This solves a number of problematic memory leaks during batch processing
  • Loading branch information
tannerhelland committed Aug 18, 2016
1 parent 90a5a87 commit 26d5a19
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
19 changes: 9 additions & 10 deletions Classes/pdImage.cls
Original file line number Diff line number Diff line change
Expand Up @@ -780,16 +780,17 @@ End Function
'When this DIB is no longer being used, we can deactivate it to save on resources.
Public Sub DeactivateImage()

'Erase the composite buffer
'Erase any internal image buffers
If Not (compositeBuffer Is Nothing) Then Set compositeBuffer = Nothing

'Erase the canvas buffer
If Not (canvasBuffer Is Nothing) Then Set canvasBuffer = Nothing

'If a selection exists, wipe it
If Not (mainSelection Is Nothing) Then Set mainSelection = Nothing
'Erase the selection manager (*very* important because of circular references)
If Not (mainSelection Is Nothing) Then
Set mainSelection.containingPDImage = Nothing
Set mainSelection = Nothing
End If

'Deactivate the Undo/Redo handler
'Deactivate the Undo/Redo handler (*very* important because of circular references)
If Not (undoManager Is Nothing) Then
undoManager.ClearUndos
Set undoManager.parentPDImage = Nothing
Expand All @@ -804,17 +805,15 @@ Public Sub DeactivateImage()

'Release all layers
Dim i As Long
If m_numOfLayers > 0 Then
If (m_numOfLayers > 0) Then
For i = 0 To UBound(imgLayers)
Set imgLayers(i) = Nothing
Next i
m_numOfLayers = 0
End If

'It doesn't make a big difference, but we can also empty out this image's String-type variables to save a bit of space.
imgStorage.DeleteEntry "OriginalFileName"
imgStorage.DeleteEntry "OriginalFileExtension"
imgStorage.DeleteEntry "CurrentLocationOnDisk"
Set imgStorage = Nothing

'Mark this image as inactive
IsActive = False
Expand Down
5 changes: 4 additions & 1 deletion Modules/Main.bas
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,10 @@ Public Sub FinalShutdown()

Dim i As Long
For i = LBound(pdImages) To UBound(pdImages)
Set pdImages(i) = Nothing
If (Not pdImages(i) Is Nothing) Then
pdImages(i).DeactivateImage
Set pdImages(i) = Nothing
End If
Next i

'Delete any remaining temp files in the cache
Expand Down
8 changes: 6 additions & 2 deletions Modules/VBP_LoadModule.bas
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,12 @@ Public Function QuickLoadImageToDIB(ByVal imagePath As String, ByRef targetDIB A
End If
End If

'Deactivate the (now useless) DIB
If (Not tmpPDImage Is Nothing) Then Set tmpPDImage = Nothing
'Deactivate the (now useless) DIB and parent object
If (Not tmpPDImage Is Nothing) Then
tmpPDImage.DeactivateImage
Set tmpPDImage = Nothing
End If

If (Not targetDIB Is Nothing) Then Set targetDIB = Nothing

'Re-enable the main interface
Expand Down
10 changes: 5 additions & 5 deletions Modules/VBP_MiscInterface.bas
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ Private Sub SetUIMode_NoImages()

'If no images are currently open, but images were previously opened during this session, release any memory associated
' with those images. This helps minimize PD's memory usage at idle.
If g_NumOfImagesLoaded >= 1 Then
If (g_NumOfImagesLoaded >= 1) Then

'Loop through all pdImage objects and make sure they've been deactivated
Dim i As Long
Expand Down Expand Up @@ -1839,15 +1839,15 @@ Public Function GetRuntimeUIDIB(ByVal dibType As PD_RUNTIME_UI_DIB, Optional ByV
End Function

'New test functions to (hopefully) help address high-DPI issues where VB's internal scale properties report false values
Public Function APIWidth(ByVal srcHWnd As Long) As Long
Public Function APIWidth(ByVal srcHwnd As Long) As Long
Dim tmpRect As winRect
GetWindowRect srcHWnd, tmpRect
GetWindowRect srcHwnd, tmpRect
APIWidth = tmpRect.x2 - tmpRect.x1
End Function

Public Function APIHeight(ByVal srcHWnd As Long) As Long
Public Function APIHeight(ByVal srcHwnd As Long) As Long
Dim tmpRect As winRect
GetWindowRect srcHWnd, tmpRect
GetWindowRect srcHwnd, tmpRect
APIHeight = tmpRect.y2 - tmpRect.y1
End Function

Expand Down

0 comments on commit 26d5a19

Please sign in to comment.