Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/PerfView.Tests/StackViewer/StackWindowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -859,5 +859,39 @@ public override string GetFrameName(StackSourceFrameIndex frameIndex, bool verbo
return frameIndex.ToString();
}
}

[WpfFact]
[WorkItem(2308, "https://github.com/Microsoft/perfview/issues/2308")]
public void TestExportFlameGraphWithInvalidCanvasSize()
{
// Create a canvas with zero size (simulating an unrendered or collapsed canvas)
var canvas = new Canvas();
canvas.Width = 0;
canvas.Height = 0;
canvas.Measure(new Size(0, 0));
canvas.Arrange(new Rect(0, 0, 0, 0));

var tempFile = Path.GetTempFileName();
try
{
// Attempt to export should throw ArgumentOutOfRangeException with a meaningful message
var exception = Assert.Throws<ArgumentOutOfRangeException>(() =>
{
FlameGraph.Export(canvas, tempFile);
});

// Verify the exception message is helpful
Assert.Contains("Canvas has an invalid size", exception.Message);
Assert.Contains("width=0", exception.Message);
Assert.Contains("height=0", exception.Message);
}
finally
{
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
}
}
}
}
13 changes: 12 additions & 1 deletion src/PerfView/StackViewer/FlameGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,18 @@ public static IEnumerable<FlameBox> Calculate(CallTree callTree, double maxWidth
public static void Export(Canvas flameGraphCanvas, string filePath)
{
var rectangle = new Rect(flameGraphCanvas.RenderSize);
var renderTargetBitmap = new RenderTargetBitmap((int)rectangle.Right, (int)rectangle.Bottom, 96d, 96d, PixelFormats.Default);
int width = (int)rectangle.Right;
int height = (int)rectangle.Bottom;

// Validate that the canvas has a valid size before attempting to export
if (width <= 0 || height <= 0)
{
throw new ArgumentOutOfRangeException(
nameof(flameGraphCanvas),
$"Canvas has an invalid size (width={width}, height={height}). Please ensure the flame graph is visible and has been rendered before attempting to export.");
}

var renderTargetBitmap = new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Default);
renderTargetBitmap.Render(flameGraphCanvas);

var pngEncoder = new PngBitmapEncoder();
Expand Down
14 changes: 11 additions & 3 deletions src/PerfView/StackViewer/StackWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2764,10 +2764,18 @@ private void DoSaveFlameGraph(object sender, RoutedEventArgs e)
var result = saveDialog.ShowDialog();
if (result == true)
{
if (FlameGraphCanvas.IsEmpty || m_RedrawFlameGraphWhenItBecomesVisible)
RedrawFlameGraph();
try
{
if (FlameGraphCanvas.IsEmpty || m_RedrawFlameGraphWhenItBecomesVisible)
RedrawFlameGraph();

FlameGraph.Export(FlameGraphCanvas, saveDialog.FileName);
FlameGraph.Export(FlameGraphCanvas, saveDialog.FileName);
StatusBar.Log($"Saved flame graph to {saveDialog.FileName}");
}
catch (ArgumentOutOfRangeException ex)
{
StatusBar.LogError($"Failed to save flame graph: {ex.Message}");
}
}
}

Expand Down