Skip to content

[System.Runtime.Interopservices.Marshal]::ReleaseComObject() causes debugger crashes in latest release #3511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
4 tasks done
GeekTieGuy opened this issue Aug 19, 2021 · 9 comments
Assignees
Labels
Area-Debugging Issue-Bug A bug to squash. Resolution-Fixed Will close automatically.

Comments

@GeekTieGuy
Copy link

GeekTieGuy commented Aug 19, 2021

Prerequisites

  • I have written a descriptive issue title.
  • I have searched all issues to ensure it has not already been reported.
  • I have read the troubleshooting guide.
  • I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.

Summary

I'm using PowerShell to automate Excel via COM objects, and did not experience debugger crashes before the update to the latest extension. The debugger crash seems to happen when making calls to [System.Runtime.Interopservices.Marshal]::ReleaseComObject()

I'm unsure of how exactly I can provide a reproduction recipe. I'm open to suggestions.

I can work around the issue for now by not running my scripts in the debugger, but if I had been at an earlier stage of my work, having the debugger crash would have slowed me down a lot.

PowerShell Version

Name                           Value
----                           -----
PSVersion                      5.1.18362.1593
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.1593
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Visual Studio Code Version

1.59.0
379476f0e13988d90fab105c5c19e7abc8b1dea8
x64

Extension Version

Steps to Reproduce

    $excel = New-Object -comobject Excel.Application
    $excel.Visible = $true
    $excel.WindowState = "xlMaximized"
    $workbook = $excel.Workbooks.Open($excelFilePath)

    $pictures = @{}
    foreach ($worksheetName in $excelSheetsAndCharts.Keys) {
        $worksheet = $workbook.Worksheets.item($worksheetName)
        $worksheet.Activate()
        Write-Host "Activated Excel Worksheet $worksheetName"
        if ($worksheet.Name -eq "Team Dashboard") {
            Write-Host "Switching to charts with All data in worksheet 'Team Dashboard'"
            $range = $worksheet.Range("J2")
            $range.Value = "All"
        }
        foreach ($chartName in $excelSheetsAndCharts[$worksheetName]) {
            Write-Host "Producing PNG of chart $chartName"
            $chart = $worksheet.ChartObjects($chartName)
            $excel.Goto($chart.TopLeftCell, $true)

            $chartFile = $fullFolderPath + "\$chartName.png"
            $chart.Chart.Export($chartFile, "PNG") > $null
            #$chart.Copy() > $null

            $png = New-Object System.Drawing.Bitmap $chartFile
            $aspectRatio = $png.Height / $png.Width
            $wordPicWidth = 618
            if ($aspectRatio -gt 1) {
                $wordPicWidth = 309
            }
            $wordPicHeight = [int]($wordPicWidth * $aspectRatio)
        
            $picture = [ordered] @{
                Name = $chartName
                File   = $chartFile
                Width  = $wordPicWidth
                Height = $wordPicHeight
            }
            $pictures.Add($chartName, $picture) > $null

            Write-Host "Releasing COM object for: $chartName"
            [System.Runtime.Interopservices.Marshal]::ReleaseComObject($chart) #> $null
            Remove-Variable chart
            Remove-Variable png
        }
        if ($worksheet.Name -eq "VersionProgress") {
            Write-Host "Producing PNG of Forecast cells in worksheet VersionProgress"
            $range = $worksheet.Range("CellsForecast")
            $range.Cells.CopyPicture(1, 2)
            $chart = $worksheet.ChartObjects("ChartForecast")
            #$chart.Chart.Delete() > $null
            $chart.Chart.Paste() > $null
            $excel.Goto($chart.TopLeftCell, $true)
            $chartFile = $fullFolderPath + "\ChartForecast.png"
            $chart.Chart.Export($chartFile, "PNG") > $null

            $png = New-Object System.Drawing.Bitmap $chartFile
            $aspectRatio = $png.Height / $png.Width
            $wordPicWidth = 618
            if ($aspectRatio -gt 1) {
                $wordPicWidth = 309
            }
            $wordPicHeight = [int]($wordPicWidth * $aspectRatio)

            $picture = [ordered] @{
                Name = "ChartForecast"
                File   = $chartFile
                Width  = $wordPicWidth
                Height = $wordPicHeight
            }
            $pictures.Add("ChartForecast", $picture) > $null

            Write-Host "Releasing COM objects for: ChartForecast"
            [System.Runtime.Interopservices.Marshal]::ReleaseComObject($range) #> $null
            [System.Runtime.Interopservices.Marshal]::ReleaseComObject($chart) #> $null
            Remove-Variable chart
            Remove-Variable range
            Remove-Variable png
        }
        if ($worksheet.Name -eq "Team Dashboard") {
            Write-Host "Switching to charts with Bug data in worksheet 'Team Dashboard'"
            $range = $worksheet.Range("J2")
            $range.Value = "Defects"

            foreach ($chartName in $excelSheetsAndCharts[$worksheetName]) {
                Write-Host "Producing PNG of Bugs chart $chartName"
                $chart = $worksheet.ChartObjects($chartName)
                $excel.Goto($chart.TopLeftCell, $true)

                $chartFile = $fullFolderPath + "\Bugs_$($chartName).png"
                $chart.Chart.Export($chartFile, "PNG") > $null
                #$chart.Copy() > $null

                $png = New-Object System.Drawing.Bitmap $chartFile
                $aspectRatio = $png.Height / $png.Width
                $wordPicWidth = 618
                if ($aspectRatio -gt 1) {
                    $wordPicWidth = 309
                }
                $wordPicHeight = [int]($wordPicWidth * $aspectRatio)
        
                $picture = [ordered] @{
                    Name   = ("Bugs_" + $chartName)
                    File   = $chartFile
                    Width  = $wordPicWidth
                    Height = $wordPicHeight
                }
                $pictures.Add(("Bugs_" + $chartName), $picture) > $null

                Write-Host "Releasing COM objects for: $chartName"
                [System.Runtime.Interopservices.Marshal]::ReleaseComObject($range) #> $null
                [System.Runtime.Interopservices.Marshal]::ReleaseComObject($chart) #> $null
                Remove-Variable chart
                Remove-Variable range
                Remove-Variable png
            }
        }
    
        [System.Runtime.Interopservices.Marshal]::ReleaseComObject($worksheet) > $null
    }
    $workbook.Close()
    $excel.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) > $null
    Remove-Variable excel

Visuals

No response

Logs

I'd rather email these. Please let me know when you want them (if you want them).

@GeekTieGuy GeekTieGuy added the Issue-Bug A bug to squash. label Aug 19, 2021
@ghost ghost added the Needs: Triage Maintainer attention needed! label Aug 19, 2021
@andyleejordan
Copy link
Member

Hey, I'm releasing an update to the preview extension probably tomorrow with a lot of debugger stability fixes. I'll ping you here when it's available on the marketplace, would love to see if we get lucky and the problem goes away.

@andyleejordan
Copy link
Member

Any chance we got lucky? v2021.8.1-preview (aka "PowerShell Preview") is up!

@GeekTieGuy
Copy link
Author

Looks like with that version, I can't reliably invoke the debugging session. Sometimes it will launch the script, sometimes it won't. Most of the time it won't, actually. But when it does (after a fresh VS Code launch for example), things no longer crash while debugging the script! So - Yay? One step back - one step forward.

@andyleejordan
Copy link
Member

Yup, one step forward and one step backward is exactly how this feels sometimes. Slowly starting in on the test project to bring all our existing tests back online, and am making a deliberate point to add regression tests for everything fixed going forward, because the state of this is just...rough.

@andyleejordan
Copy link
Member

Also for not being able to reliably invoke the debugging session, I think that's #3513.

@GeekTieGuy
Copy link
Author

Thank you for the work you're doing with this!!!

@andyleejordan
Copy link
Member

PowerShell Preview 2021.8.2 will be out in just a few minutes, would you test and confirm this issue has been fully resolved? Thanks!

@andyleejordan andyleejordan added Area-Debugging and removed Needs: Triage Maintainer attention needed! labels Aug 23, 2021
@andyleejordan andyleejordan self-assigned this Aug 23, 2021
@andyleejordan andyleejordan added the Needs: Fix Verification Please verify the fix for us! label Aug 23, 2021
@andyleejordan andyleejordan added this to the Committed-vNext milestone Aug 23, 2021
@andyleejordan
Copy link
Member

@GeekTieGuy ok that's available on the marketplace! It seems promising, can you see if it still repros?

@GeekTieGuy
Copy link
Author

I've been able to work in the debugger much better now. I would consider things good enough now to close this issue, so I'll just do that.

@ghost ghost added the Needs: Maintainer Attention Maintainer attention needed! label Aug 28, 2021
@andyleejordan andyleejordan added Resolution-Fixed Will close automatically. and removed Needs: Fix Verification Please verify the fix for us! Needs: Maintainer Attention Maintainer attention needed! labels Aug 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Debugging Issue-Bug A bug to squash. Resolution-Fixed Will close automatically.
Projects
None yet
Development

No branches or pull requests

2 participants