- Home /
Does my script have a memory leak ?
What I did in this editor script is that when you make a right click with the mouse button on any Folder in the Assets it will add to menu a new option name Get Prefabs..
When you click on Get Prefabs it will find all the prefabs in all sub folders and will add to them a Rigidbody and then will save the prefabs for the changes made and will unload the content to release memory.
Yesterday it was working fine but today I'm getting two exceptions sometimes the whole editor crash without and exception.
Yesterday when I did Get Prefabs it was showing the working blue circle and in the end all prefabs was with Rigidbody. Today the whole editor get freeze and in the end when click on a prefab in the Assets the editor crash or it's showing two exceptions and then crash.
This is the two exceptions :
InvalidOperationException: Preview scene could not be created
And
A PreviewRenderUtility was not clean up properly before assembly reloading which lead to leaking this scene in the Editor. This can be caused by not calling Cleanup() during the OnDisable of an Editor or an EditorWindow
But in many cases it's crashing the whole editor without any exception. And the crash happens when I select a prefab in the asset just click on him after the process ended.
The exceptions or crashes don't happen when the script is working but after the script finished his process then when I click on a prefab in the assets the editor crash.
This is some errors taken from the editor error log file created by the editor :
Unity Editor by Unity Technologies [version: Unity 2019.3.11f1_ceef2d848e70]
Unity.exe caused an Access Violation (0xc0000005) in module Unity.exe at 0033:ac93ddfc.
Error occurred at 2020-06-04_194016. C:\Program Files\Unity Editors\2019.3.11f1\Editor\Unity.exe, run by choco.
40% physical memory in use. 32700 MB physical memory [19347 MB free]. 2619 MB process peak paging file [2431 MB used]. 1660 MB process peak working set [1469 MB used]. System Commit Total/Limit/Peak: 22242MB/47969MB/43262MB System Physical Total/Available: 32700MB/19347MB System Process Count: 284 System Thread Count: 3632 System Handle Count: 151003 Disk space data for 'C:\Users\choco\AppData\Local\Temp\Unity\Editor\Crashes\Crash_2020-06-04_163954384\': 799346913280 bytes free of 999526756352 total.
Write to location 00000000000000BC caused an access violation.
Context: RDI: 0x0000000000000000 RSI: 0x0000014f2000c6f0 RAX: 0x0000014ec0af8d40 RBX: 0x0000000000000017 RCX: 0x0000014f2000c6f0 RDX: 0x0000000000000000 RIP: 0x00007ff7ac93ddfc RBP: 0x00000000ffffffff SegCs: 0x0000000000000033 EFlags: 0x0000000000010246 RSP: 0x0000000f8baeb0b0 SegSs: 0x000000000000002b R8: 0x0000000000000006 R9: 0x0000000000000001 R10: 0x0000014ec00276d0 R11: 0x0000000f8baeb230 R12: 0x0000000000000006 R13: 0x0000000000000000 R14: 0x0000000000000000 R15: 0x0000014f24ce61e0
Bytes at CS:EIP: 41 c7 86 bc 00 00 00 02 00 00 00 e8 b4 c0 10 fe
Mono DLL loaded successfully at 'C:\Program Files\Unity Editors\2019.3.11f1\Editor\Data\MonoBleedingEdge\EmbedRuntime\mono-2.0-bdwgc.dll'.
Stack Trace of Crashed Thread 24396: ERROR: SymGetSymFromAddr64, GetLastError: 'Attempt to access invalid address.' (Address: 00007FF7AC93DDFC) 0x00007FF7AC93DDFC (Unity) (function-name not available) ERROR: SymGetSymFromAddr64, GetLastError: 'Attempt to access invalid address.' (Address: 00007FF7AACDB233) 0x00007FF7AACDB233 (Unity) (function-name not available) ERROR: SymGetSymFromAddr64, GetLastError: 'Attempt to access invalid address.' (Address: 00007FF7AB7D1991) 0x00007FF7AB7D1991 (Unity) (function-name not available) ERROR: SymGetSymFromAddr64, GetLastError: 'Attempt to access invalid address.' (Address: 00007FF7AB7D10FE) 0x00007FF7AB7D10FE (Unity) (function-name not available) ERROR: SymGetSymFromAddr64, GetLastError: 'Attempt to access invalid address.' (Address: 00007FF7AB7D0D9B) 0x00007FF7AB7D0D9B (Unity) (function-name not available)
I added a file attached the error log text file :
This is the script :
using Packages.Rider.Editor.Util;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using UnityEditor;
using UnityEngine;
public class AddComponents : Editor
{
private static string GetClickedDirFullPath()
{
string clickedAssetGuid = Selection.assetGUIDs[0];
string clickedPath = AssetDatabase.GUIDToAssetPath(clickedAssetGuid);
string clickedPathFull = Path.Combine(Directory.GetCurrentDirectory(), clickedPath);
FileAttributes attr = File.GetAttributes(clickedPathFull);
return attr.HasFlag(FileAttributes.Directory) ? clickedPathFull : Path.GetDirectoryName(clickedPathFull);
}
private static string GetPath()
{
string path = GetClickedDirFullPath();
int index = path.IndexOf("Assets");
string result = path.Substring(index);
return result;
}
static List<string> paths = new List<string>();
[MenuItem("Assets/Get Prefabs")]
private static void GetFolders()
{
string selectedPath = GetPath();
string[] assetsPaths = AssetDatabase.GetAllAssetPaths();
foreach (string assetPath in assetsPaths)
{
if (assetPath.Contains(selectedPath))
{
if (assetPath.Contains("Prefabs"))
{
paths.Add(assetPath);
}
}
}
}
[MenuItem("Assets/Get Prefabs")]
public static void GetPrefabs()
{
try
{
GetFolders();
for (int i = 0; i < paths.Count; i++)
{
if (File.Exists(paths[i]))
{
if (paths[i].EndsWith(".prefab"))
{
GameObject contentsRoot = PrefabUtility.LoadPrefabContents(paths[i]);
var rb = contentsRoot.GetComponent<Rigidbody>();
if (rb == null)
{
// Modify Prefab contents.
contentsRoot.AddComponent<Rigidbody>();
// Save contents back to Prefab Asset and unload contents.
PrefabUtility.SaveAsPrefabAsset(contentsRoot, paths[i]);
PrefabUtility.UnloadPrefabContents(contentsRoot);
}
}
}
}
}
catch(Exception err)
{
}
}
}