CommunityToolkit / CommunityToolkit/Lottie-Windows
Undisposed resource problem in the generated codes
- Dominant language
- C#
- Stars
- 717
- Forks
- 76
- PR merge metrics
- No merged PRs in 30d
Description
This is part of the generated code
```csharp
CompositionEffectFactory EffectFactory()
{
var compositeEffect = new CompositeEffect();
compositeEffect.Mode = CanvasComposite.DestinationIn;
compositeEffect.Sources.Add(new CompositionEffectSourceParameter("destination"));
compositeEffect.Sources.Add(new CompositionEffectSourceParameter("source"));
var result = _c.CreateEffectFactory(compositeEffect);
return result;
}
```
It is not using the `using` keyword for the `CompositeEffect` object being created, leaving undisposed resources. GitHub CodeQL scans also confirm this problem:
The correct code would be this:
```csharp
CompositionEffectFactory EffectFactory()
{
using CompositeEffect compositeEffect = new();
compositeEffect.Mode = CanvasComposite.DestinationIn;
compositeEffect.Sources.Add(new CompositionEffectSourceParameter("destination"));
compositeEffect.Sources.Add(new CompositionEffectSourceParameter("source"));
var result = _c.CreateEffectFactory(compositeEffect);
return result;
}
```
## Explanation
After `CreateEffectFactory` has created the `CompositionEffectFactory`, the code does not need the original `CompositeEffect` instance anymore. Disposing the local `CompositeEffect` after factory creation does not dispose the `CompositionEffectFactory` or the `CompositionEffectBrush` created from it. `EffectBrush()` creates the factory, creates a brush from it, sets the two source parameters, and returns the brush, so the `CompositeEffect` lifetime can end inside `EffectFactory()`.
## Details
* .NET 10
* Visual Studio latest version
* LottieGen version: 8.2.250604.1
* Command: `LottieGen -Language CSharp -Public -WinUIVersion 3.0 -InputFile Item.json`
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the LottieGen entry point using the supplied `LottieGen -Language CSharp -Public -WinUIVersion 3.0 -InputFile Item.json` command, then locate where the generated `EffectFactory()` code is emitted. Regenerate the C# output and verify that the `CompositeEffect` is disposed after factory creation without changing the returned factory or brush behavior; the CodeQL resource warning should be resolved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100