fix: filters memory leak on apple (#2534)

# Summary

While animating filters, I discovered a memory leak due to not releasing
the `CGImage` after creating the mask.

## Test Plan

```tsx
import {useEffect} from 'react';
import Animated, { useSharedValue, withRepeat, withTiming } from 'react-native-reanimated';
import Svg, {FeOffset, Filter, Rect} from 'react-native-svg';

const AnimatedFilter = Animated.createAnimatedComponent(Filter);
const AnimatedFeOffset = Animated.createAnimatedComponent(FeOffset);
const AnimatedRect = Animated.createAnimatedComponent(Rect);
export default function Example() {
  const offset = useSharedValue(0.5);

  useEffect(() => {
    offset.value = withRepeat(withTiming(300, {duration: 3000}), -1, true);
  }, []);

  return (
    <Svg height="300" width="300">
      <AnimatedFilter id="filter">
        <AnimatedFeOffset dx={offset} dy={offset} result="offOut" />
      </AnimatedFilter>
      <AnimatedRect
        x="0"
        y="0"
        width="300"
        height="300"
        fill="red"
        filter="url(#filter)"
      />
    </Svg>
  );
}
```

## Compatibility

| OS      | Implemented |
| ------- | :---------: |
| iOS     |          |
| macOS   |          |
This commit is contained in:
Jakub Grzywacz
2024-11-15 12:00:12 +01:00
committed by GitHub
parent bba8b3f0a8
commit 6aff3094ce

View File

@@ -172,9 +172,10 @@ using namespace facebook::react;
CGContextAddPath(context, path); CGContextAddPath(context, path);
CGContextFillPath(context); CGContextFillPath(context);
CGImageRef maskImage = CGBitmapContextCreateImage(context); CGImage *maskImage = CGBitmapContextCreateImage(context);
CIImage *ciMaskImage = [CIImage imageWithCGImage:maskImage];
return [CIImage imageWithCGImage:maskImage]; CGImageRelease(maskImage);
return ciMaskImage;
} }
static CIFilter *sourceAlphaFilter() static CIFilter *sourceAlphaFilter()