- Home /
Some objects were not cleaned up when closing the scene
Hi,
I get the following error when I exit my application in the Editor: Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?) I know there have been similar questions asked before but they all had an additional Error Message pointing to some line code with an OnDestroy() Method. My Problem, the Error Message is not pointing to any code (s. picture). Plus I dont have a single OnDestroy() Method in all my scripts!!! I know it's probably impossible to solve for you without seeing my code, but I only need a hint where to look, because right now I have no idea where to start looking for the origin of the error.
EDIT: I dived a little bit deeper. Stack trace logging gives the following output:
Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?) 0x0000000140E4D6ED (Unity) StackWalker::GetCurrentCallstack 0x0000000140E4F411 (Unity) StackWalker::ShowCallstack 0x00000001406021D3 (Unity) GetStacktrace 0x00000001405FF59D (Unity) DebugStringToFile 0x00000001405FF9FC (Unity) DebugStringToFile 0x000000014046A277 (Unity) ValidateNoSceneObjectsAreLoaded 0x0000000140B73CA2 (Unity) EditorSceneManager::RestoreSceneBackups 0x0000000140BE2847 (Unity) Application::ExitPlayMode 0x0000000140BE2C3B (Unity) Application::SetIsPlaying 0x0000000140BE3716 (Unity) Application::TickTimer 0x0000000140E47E2E (Unity) FindMonoBinaryToUse 0x0000000140E49461 (Unity) WinMain 0x00000001415AD460 (Unity) strnlen 0x00007FFEB7E98364 (KERNEL32) BaseThreadInitThunk
In the Editor.log I found the following outout:
Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)
"(Filename: C:/buildslave/unity/build/Runtime/Misc/SaveAndLoadHelper.cpp Line: 595)"
I got no idea about how to deal with the SaveAndLoadHelper.cpp. I can't even find it under the given path
Are you using any plugins? If so, plugin could be the one causing problems.
only AdColony. I'll look into it. Thanks for the hint
it's a common problem when using lazy singletons, those, that instantiate themselves when called and not in existence
Answer by LK84 · Nov 23, 2016 at 03:22 PM
Thanks to @hexagonius and @Mister-Mortal hints I found the solution. The error was indeed caused by a singleton. I am using a game Manager implementing the Singleton Pattern. It used to look like this:
public class gameManager:MonoBehaviour
{
private static gameManager _instance;
public static gameManager Instance
{
get
{
if (_instance == null)
{
GameObject go = new GameObject("gameManager");
go.AddComponent<gameManager>();
DontDestroyOnLoad(go);
}
return _instance;
}
}
public event VideoEvent OnVideoFinished;
public event VideoEvent OnVideoStarted;
Awake()
{
_instance=this;
AdColony.OnVideoFinished = IsVideoFinished;
AdColony.OnVideoStarted = VideoStarted;
.....
}
private void IsVideoFinished(bool adShown)
{
if(OnVideoFinished!=null)
OnVideoFinished(this, new GenericEventArgs<bool>(adShown));
}
private void VideoStarted()
{
if (OnVideoStarted != null)
{
OnVideoStarted(this, EventArgs.Empty);
}
}
}
As you can see there are two events sources in the gameManager class for AdColony videos. The delegate is defined as followed: public delegate void VideoEvent (object sender, EventArgs e);
I have Event Listener to those events at several points in my game: They are implemented like this:
void Start()
{
gameManager.Instance.OnVideoFinished += LocalMethod;
}
void OnDisable()
{
gameManager.Instance.OnVideoFinished -= LocalMethod;
}
LocalMethod(object sender, EventArgs e)
{
//Do Something
}
The error is caused for the following reason: When I quit the application objects gets destroyed in a random order. So If the gameManager Instance gets destroyed before any of the OnDisable() Methods, a new Instance of the gameManager class is created after the application has exited causing the error!! To solve the error I implemented the following modifications:
In the gameManager class (according to http://wiki.unity3d.com/index.php/Singleton):
private static object _lock = new object();
private static gameManager _instance;
public static gameManager Instance
{
get
{
if (applicationIsQuitting)
{
return null;
}
lock (_lock)
{
if (_instance == null)
{
GameObject go = new GameObject("gameManager");
go.AddComponent<gameManager>();
DontDestroyOnLoad(go);
}
}
return _instance;
}
}
private static bool applicationIsQuitting = false;
public void OnDestroy()
{
Debug.Log("Gets destroyed");
applicationIsQuitting = true;
}
In the class containing the Event Listener:
void OnDisable()
{
if (gameManager.Instance != null)
gameManager.Instance.OnVideoFinished -= OnVideoFinished;
}
Now if the gameManager instance is already destroyed a new instance can't be created
This did the trick for me. Thanks for sharing your solution!
I found out a better way to know if the application is quitting:
[RuntimeInitializeOnLoad$$anonymous$$ethod]
static void RunOnStart()
{
Application.quitting += () => applicationIsQuitting = true;
}
The rest of the code worked great for me.
Thanks @Sylker ! For anybody else who can't get it to compile with "[RuntimeInitializeOnLoad$$anonymous$$ethod]", try this instead: [RuntimeInitializeOnLoad$$anonymous$$ethod]
Answer by f3publishing · Aug 14, 2018 at 01:32 PM
There is something missing. Add _instance = before your AddComponent line.
Your answer
Follow this Question
Related Questions
Visual Studio Code doesn't work for with unity 2 Answers
What is happening and what does this MEAN!?!?!?!?!?!?!?!? AHHHHH!!!!,WHAT IS HAPPENING?!?! 1 Answer
Too many errors in the IDE (even for a fresh project), but editor doesn't throw any errors! 1 Answer
Fatal error! System out of memory! Unity 3D 1 Answer
Cannot import videos 0 Answers