目录

PixelMapNotRelease

Call the release() method in a timely manner after use to release the PixelMap object created by calling APIs such as createPixelMap(), createPixelMapSync(), and getEffectPixelMap(), preventing the image from consuming excessive memory.

Rule Configuration

// rules.json
{
  "rules": {
    "arkts": {
      "ResourceLeak": [
        {
          "ruleId": "PixelMapNotRelease",
          "severity": "error",
          "skip": false
        }
      ]
    }
  }
}

Example of Correct Code

import image from '@ohos.multimedia.image';
import { effectKit } from "@kit.ArkGraphics2D";
import UIAbility from '@ohos.app.ability.UIAbility';

/**
 * Positive example: Resources are correctly released.
 */
export class TestRelease extends UIAbility {
  private pixelMap1: image.PixelMap | null = null;
  private pixelMap2: image.PixelMap | null = null;
  private pixelMap3: image.PixelMap | null = null;
  async onCreate(): Promise<void> {
    // Test the image.ImageSource.createPixelMap creation API.
    const imageSource1 = image.createImageSource("file://test1.jpg");
    this.pixelMap1 = await imageSource1.createPixelMap();
    imageSource1.release();
    // Test the image.ImageSource.createPixelMapSync creation API.
    const imageSource2 = image.createImageSource("file://test2.jpg");
    this.pixelMap2 = imageSource2.createPixelMapSync();
    imageSource2.release();
    // Test the effectKit.Filter.getEffectPixelMap creation API.
    const color = new ArrayBuffer(96);
    let opts : image.InitializationOptions = {
      editable: true,
      pixelFormat: 3,
      size: {
        height: 4,
        width: 6
      }
    };
    image.createPixelMap(color, opts).then((pixelMap) => {
      effectKit.createEffect(pixelMap).grayscale().getEffectPixelMap().then(data => {
        this.pixelMap3 = data;
      })
    })
  }
  onDestroy(): void {
    this.pixelMap1?.release();
    this.pixelMap2?.release();
    this.pixelMap3?.release();
  }
}

Example of Incorrect Code

import image from '@ohos.multimedia.image';
import { effectKit } from "@kit.ArkGraphics2D";
import UIAbility from '@ohos.app.ability.UIAbility';

/**
 * Negative example: Resources are not released.
 */
export class TestNotRelease extends UIAbility {
  private pixelMap1: image.PixelMap | null = null;
  private pixelMap2: image.PixelMap | null = null;
  private pixelMap3: image.PixelMap | null = null;
  async onCreate(): Promise<void> {
    // Test the image.ImageSource.createPixelMap resource creation API.
    const imageSource1 = image.createImageSource("file://test1.jpg");
    this.pixelMap1 = await imageSource1.createPixelMap(); // leak
    imageSource1.release();
    // Test the image.ImageSource.createPixelMapSync resource creation API.
    const imageSource2 = image.createImageSource("file://test2.jpg");
    this.pixelMap2 = imageSource2.createPixelMapSync(); // leak
    imageSource2.release();
    // Test the effectKit.Filter.getEffectPixelMap resource creation API.
    const color = new ArrayBuffer(96);
    let opts : image.InitializationOptions = {
      editable: true,
      pixelFormat: 3,
      size: {
        height: 4,
        width: 6
      }
    };
    image.createPixelMap(color, opts).then((pixelMap) => {
      effectKit.createEffect(pixelMap).grayscale().getEffectPixelMap().then(data => { // leak
        this.pixelMap3 = data;
      })
    })
  }
}

For details about how to configure Code Scanner check rules, see Using codescanner.