LoginSignup
5

More than 1 year has passed since last update.

posted at

updated at

【Unity】スタックトレースを有効にしてNativeArrayのメモリリークを探す

ちょっと詰まったのでメモ。
UnityのバージョンはUnity2020.2.0a。

Job SystemでNativeArrayを使うスクリプトをオブジェクトに付けてゲームを再生すると次のようなエラーが出た。

A Native Collection has not been disposed, resulting in a memory leak. Enable Full StackTraces to get more details.

コンソールのメニューのStack Trace LoggingからすべてのスタックトレースをFullに変更した。image.png

しかしエラーの表記はさっきのまま変わらず詳細は表示されない。

解決策

NativeArrayのスタックトレースはコンソールのメニューからいじれるものとは別らしい。NativeLeakDetection.ModeEnabledWithStackTraceに変更してやればよい。

SetupNativeLeakDetection.cs
using UnityEngine;
using Unity.Collections;

public class SetupNativeLeakDetection : MonoBehaviour
{
    void Start()
    {
        NativeLeakDetection.Mode = NativeLeakDetectionMode.EnabledWithStackTrace;
    }
}

エラー表示は次のようになった

A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
Unity.Collections.NativeArray`1:.ctor(Int32, Allocator, NativeArrayOptions) (発生した関数名) (at (ソースのパス):(行数))

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
5