Everything from previous scene is Leaking over to the next scene!

I am really close to releasing my current game, but I cannot seem to find the source of this current leak which is plaguing my entire game.

It appears that everything that is in the Menu scene of my game is leaked over in memory to any other scene in my game. I have followed what other people have suggested on these forums, by creating a loading scene which calls Resources.UnloadUnusedAssets, before loading the next scene, but this appears to not help in the least.

I am using this code to check for my leaks:

using UnityEngine;
using System.Collections;

public class DetectLeaks : MonoBehaviour {
	
	
    void OnGUI () {
        GUILayout.Label("All " + Resources.FindObjectsOfTypeAll(typeof(UnityEngine.Object)).Length);
        GUILayout.Label("Textures " + Resources.FindObjectsOfTypeAll(typeof(Texture)).Length);
        GUILayout.Label("AudioClips " + Resources.FindObjectsOfTypeAll(typeof(AudioClip)).Length);
        GUILayout.Label("Meshes " + Resources.FindObjectsOfTypeAll(typeof(Mesh)).Length);
        GUILayout.Label("Materials " + Resources.FindObjectsOfTypeAll(typeof(Material)).Length);
        GUILayout.Label("GameObjects " + Resources.FindObjectsOfTypeAll(typeof(GameObject)).Length);
        GUILayout.Label("Components " + Resources.FindObjectsOfTypeAll(typeof(Component)).Length);
		
		if(GUILayout.Button("Unload Unused Assets"))
		{
			Resources.UnloadUnusedAssets();
			print("UNLOADING");
		}
		
		if(GUILayout.Button("Print Components"))
		{
			foreach ( Component component in Resources.FindObjectsOfTypeAll(typeof(Component)) )
			{
				print("COMPONENT: " + component.name);
			}
		}
		
		if(GUILayout.Button("Print Textures"))
		{
			foreach ( Texture text in Resources.FindObjectsOfTypeAll(typeof(Texture)) )
			{
				print("TEXTURE: " + text.name);
			}
		}
		
		if(GUILayout.Button("Print AudioClips"))
		{
			foreach ( AudioClip audio in Resources.FindObjectsOfTypeAll(typeof(AudioClip)) )
			{
				print("Audio: " + audio.name);
			}
		}
    }
}

As you can see I have 3 buttons that will print out resources that are still loaded in to memory. I am currently having the menu automatically load into an empty scene with just that script on the camera. Once in the empty scene by tapping printing out all of the textures, audio clips and components that are currently loaded, I can see that pretty much everything in the previous scene is still loaded in to memory.

I do not have DontDestroyOnLoad() on any scripts, and any case in which I use a static singleton variable, I set to null in OnDestroy.

I am at a loss for what else could be causing this, but it is absolutely killing my memory usage on devices. Does anyone know why these items would all still be getting left in memory?

are your references to the assets also statics?
if so thats enough to never make them vanish as statics “never go out of scope”

References to the assets? I don’t think so. The only static variables I make are singleton variables for a Monobehavior which are set in Awake to something like instance = this , then are unset in OnDestroy like instance = null . As far as I know of, I don’t use any other static variables.

Okey

and above check is in the 3rd scene ie the one after the loading scene, where you check it?

Yes and I even have a button that calls Resources.UnloadUnusedAssets() in that scene, just to see if maybe it needed to be called at a later time to work, but it appears to have no effect.

Very strange thing.
Though I just realized that I’m an kind of blind: You use the Resources function - I’m pretty sure thats not telling you what you have loaded at the time.
Try it without Resources. in front just like that (or Object.FindObjectsOfType), thats whats around right now in an active form

also I assume we talk about unity3 here right? with unity iphone 1.x where this function was introduced, the only way it would do anything in scenes was actually destroying anything. just not having a reference to it was forcefully generating a leak till scene switches :wink:

I get the same results from FindObjectsOfType as I do with Resources.FindObjectsOfType , and yes I am using unity3

Anyone else have any ideas how I can track this down? Its absolutely killing me! I even took out some references to static events which I thought might be causing everything not be garbage collected but I still cannot get rid of these leftover textures, and sounds which are eating up all my memory.

Did you also check any plugins to make sure they aren’t using DontDestroyOnLoad? For example, I found that the plugin I use for Facebook login was calling that function, causing my main menu object to never get unloaded. Drove me crazy for a while.

I have actually found out what was going on. I am using EZ GUI for my menus in game, and it turns out 4 EZ Gui components keep references to a static instance of themself, which in turn caused everything using EZ GUI components or any children of them to not get garbage collected. Once I nullified these properties in the OnDestroy method, all my issues were solved!

I posted about this in the EZ GUI product forum, so hopefully the developer will get these fixes in to the next version of it.

I’m running with the same issue. Can you tell us which EZ GUI components have these static instances ?

Thanks.

edit: found your post on AnBSoft forum http://forum.anbsoft.com/viewtopic.php?f=10&t=1727