question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

ms-vscode.powershell@2021.5.1
ms-vscode.powershell@2021.8.0

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).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
andschwacommented, Aug 19, 2021

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.

1reaction
GeekTieGuycommented, Aug 28, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Weird crash when debugging COM object destructor
When calling C# from C++ sometimes the garbage collector doesn't properly get called before program end. Try forcing garbage collection at ...
Read more >
VSTestHost.exe crashing ... COM+ ... how to debug?
If I run the tests via debug I never seem to get a crash. ... System.Runtime.InteropServices.Marshal.ReleaseComObject(p_comObject) Finally
Read more >
Redemption FAQ - Dimastr.Com
Cleanup); this will cause Redemption to release the Extended MAPI session. When I set SafeMailItem.Item property to an Outlook item, my program crashes....
Read more >
Mostly when program crashes in debug mode visual studio ...
Mostly when program crashes in debug mode visual studio locks the libraries so no build is possible until I restart visual studio.
Read more >
How to properly release Excel COM objects - Add-in Express
ReleaseComObject (range); if (sheet != null) Marshal. ... Convert an Excel column number to a column name or letter: C# and VB.NET examples ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found