- Home /
Cleaning up leaked objects in scene since no game object, component or manager is referencing them
I receive this error quite frequently - it seems random to me. What are "leaked objects" - what are some common causes of this error?
"Cleaning up leaked objects in scene since no game object, component or manager is referencing them"
I don't have an answer, but I think I've seen this error occasionally when I haven't necessarily been doing anything that seems like it would cause such an error. I'm not sure what causes it.
I get this when I procedurally create materials from an editor menu, after I delete the object to which the material was attached and then save the scene. Once I see the message once I get it again every time I save the scene. Seems harmless ... ?
Why does Unity play these guessing games with us all the time? These cryptic error messages tell us about potential problems, but don't give us ANY information that would be useful in solving them. Why should we have to incrementally remove scripts to find the culprit, when the error message COULD have told us exactly WHAT it deleted in the first place? If it deleted it, it SHOULD know.
I know this doesn't help solve the problem, but I just had to vent. I found this thread while looking for information on this same error, and it's frustrating to find another dead end. While it might be theoretically possible to pinpoint the cause, in practical terms, it may not be worth the time and effort. It still bugs me that it's happening though.
Answer by Zyxil · Jan 13, 2011 at 01:55 AM
On this thread in the forum: http://forum.unity3d.com/threads/6591-Cleaning-up-leaked-objects
Samantha says:
This is an internal message related to how we're handling meta data. It's actually a message informing you that some optimizations have been made. One could argue it's not necessary, but for now it's not hurting anything. A simple clear of the Console will get rid of the error.rid of the error.
Answer by RC-1290 · Mar 29, 2014 at 03:03 AM
This message has to do with Garbage Collection, which automatically finds unused memory and makes it available for re-use. The Scripting Behind the Scenes talk has more information about how that works in Unity.
Most objects are attached to GameObjects, which are seen as 'roots', so they're automatically marked as being in-use. But this is not the case for objects like ScriptableObject, which you might need to create assets or to store data for editor tools.
During an Assembly Reload (e.g.: after compiling, when switching playmode or when loading a scene. See this blog post about Serialization), the garbage collector runs. Any assets that don't have their HideFlags set to a variation of HideAndDontSave, and aren't referenced by another 'root' such as a GameObject, are 'cleaned'.
Usually when this happens, the message will show up every time you save changes to a scene, even if no new objects have been leaked, until you reload the scene. I don't know why this happens, but I guess the 'cleaned' objects aren't actually destroyed. And unfortunately the error message doesn't give any information about which objects were leaked, or where they were created (Although to be fair, that doesn't seem like a trivial problem to solve).
Finding the cause
To find the origin of the problem, you can incrementally remove scripts(don't forget about custom windows, inspectors and menu items that might run), reload the scene, make a trivial change to the scene (add an empty GameObject), and save. The last thing you removed before the error disappeared then probably created an instance without setting its HideFlags.
It is important to note that if you set an Object's HideFlags to HideAndDontSave, you are responsible for freeing the memory its memory when it's no longer needed using DestroyImmediate.
Answer by icepick912 · Nov 23, 2013 at 09:12 PM
When deleting the object you should also call DestroyImmediate(object to destroy) on each object you want cleaned up, if you are using procedural materials on an object, then i would call DestroyImmediate(Material) to get rid of this message.
Only if they are marked to be ignored by the Garbage Collector in the first place.
Answer by sebstar · Jun 27, 2012 at 03:46 PM
since Unity 3.5.2 there exist a possiblity to unload unused assets
static function UnloadAsset (assetToUnload : Object) : void
static function UnloadUnusedAssets () : AsyncOperation
http://docs.unity3d.com/Documentation/ScriptReference/Resources.html
The message states that leaked object is being cleaned up, which it is not since the message reappears again on every save. Calling UnloadUnusedAssets() from a custom editor doesn't seem to work. The only thing that works is closing and reopening the project. I unfortunately am generating my own procedural meshes and textures and when I delete the objects that generated them they leave behind these ghosts. I wish unity would clean them up for real.
@sotirosn: Are those messages added to the scene in any way?
Your answer
Follow this Question
Related Questions
Error in Android Export 0 Answers
What does "Couldn't find matching instance in prefab" mean? 2 Answers
GameObject is already being activated or desactivated 2 Answers
How to change variables on a new gameobject's script 1 Answer
The referenced script on this Behaviour is missing! non-existant GameObject 0 Answers