JustARandomGuy
JustARandomGuy

Reputation: 73

How to fix this memory error

After googling it, I think it is a memory leak but I do not know how to fix this.

Assertion failed: TLS Allocator ALLOC_TEMP_THREAD, underlying allocator ALLOC_TEMP_THREAD has unfreed allocations, size 1528

Im Using Unity 2018.1.0.0f2.

Here is the script that causes the error (Sorry for my awful code organization skills, new to world generation)

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
public class WorldGeneration : MonoBehaviour {

public float GenerationDelay;
public float RoomPicker;
public float LevelNumber;

public float RoomCount;
public float MaxRoomCount;
public float LevelSample1Rooms;
public float MaxLevelSample1Rooms;
public float LevelSample2Rooms;
public float MaxLevelSample2Rooms;
public float LevelSample3Rooms;
public float MaxLevelSample3Rooms;
public float LevelSample4Rooms;
public float MaxLevelSample4Rooms;
public float LevelSample5Rooms;
public float MaxLevelSample5Rooms;


public GameObject LevelSample1;
public GameObject LevelSample2;
public GameObject LevelSample3;
public GameObject LevelSample4;
public GameObject LevelSample5;

public Transform SpawnLevelPoint;

public Transform SpawnLevelPoint1;
public bool Leveloadedatpoint1;
public Transform SpawnLevelPoint2;
public bool Leveloadedatpoint2;
public Transform SpawnLevelPoint3;
public bool Leveloadedatpoint3;
public Transform SpawnLevelPoint4;
public bool Leveloadedatpoint4;
public Transform SpawnLevelPoint5;
public bool Leveloadedatpoint5;
public Transform SpawnLevelPoint6;
public bool Leveloadedatpoint6;

void Start()
{

    StartCoroutine("Createpoint1room");
}

public IEnumerator Createpoint1room()
{
    if (Leveloadedatpoint1 == false)
    {


        RoomPicker = Random.Range(1, 20);
        yield return new WaitForSeconds(GenerationDelay);
        if (RoomPicker == 1 || RoomPicker == 2 || RoomPicker == 3 || RoomPicker == 4 || RoomPicker == 5)
        {
            if (Leveloadedatpoint1 == false)
            {
                if (LevelSample1Rooms < MaxLevelSample1Rooms)
                {
                    LevelSample1Rooms += 1;
                    Instantiate(LevelSample1, SpawnLevelPoint1.transform.position, Quaternion.identity);
                    Leveloadedatpoint1 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint2room");
                }
                else
                {
                    StartCoroutine("Createpoint1room");
                }
            }
            else
            {
                StartCoroutine("Createpoint1room");
            }
        }
        if (RoomPicker == 6 || RoomPicker == 7 || RoomPicker == 8)
        {
            if (Leveloadedatpoint1 == false)
            {
                if (LevelSample2Rooms < MaxLevelSample2Rooms)
                {
                    LevelSample2Rooms += 1;
                    Instantiate(LevelSample2, SpawnLevelPoint1.transform.position, Quaternion.identity);
                    Leveloadedatpoint1 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint2room");
                }
                else
                {
                    StartCoroutine("Createpoint1room");
                }
            }
            else
            {
                StartCoroutine("Createpoint1room");
            }
        }
        if (RoomPicker == 9 || RoomPicker == 10 || RoomPicker == 11)
        {
            if (Leveloadedatpoint1 == false)
            {
                if (LevelSample3Rooms < MaxLevelSample3Rooms)
                {
                    LevelSample3Rooms += 1;
                    Instantiate(LevelSample3, SpawnLevelPoint1.transform.position, Quaternion.identity);
                    Leveloadedatpoint1 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint2room");
                }
                else
                {
                    StartCoroutine("Createpoint1room");
                }
            }
            else
            {
                StartCoroutine("Createpoint1room");
            }
        }
        if (RoomPicker == 12 || RoomPicker == 13 || RoomPicker == 14)
        {
            if (Leveloadedatpoint1 == false)
            {
                if (LevelSample4Rooms < MaxLevelSample4Rooms)
                {
                    LevelSample4Rooms += 1;
                    Instantiate(LevelSample4, SpawnLevelPoint1.transform.position, Quaternion.identity);
                    Leveloadedatpoint1 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint2room");
                }
                else
                {
                    StartCoroutine("Createpoint1room");
                }
            }
            else
            {
                StartCoroutine("Createpoint1room");
            }
        }
        if (RoomPicker == 15 || RoomPicker == 16 || RoomPicker == 17)
        {
            if (Leveloadedatpoint1 == false)
            {
                if (LevelSample5Rooms < MaxLevelSample5Rooms)
                {
                    LevelSample5Rooms += 1;
                    Instantiate(LevelSample5, SpawnLevelPoint1.transform.position, Quaternion.identity);
                    Leveloadedatpoint1 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint2room");
                }
                else
                {
                    StartCoroutine("Createpoint1room");
                }
            }
            else
            {
                StartCoroutine("Createpoint1room");
            }
        }
        if (RoomPicker == 18 || RoomPicker == 19 || RoomPicker == 20)
        {
            StartCoroutine("Createpoint1room");
        }

        Debug.Log("Point 1: The number is " + RoomPicker);
    }
    else
    {
        StartCoroutine("Createpoint2room");
    }
}
public IEnumerator Createpoint2room()
{
    if (Leveloadedatpoint2 == false)
    {
        RoomPicker = Random.Range(1, 20);
        yield return new WaitForSeconds(GenerationDelay);
        if (RoomPicker == 1 || RoomPicker == 2 || RoomPicker == 3 || RoomPicker == 4 || RoomPicker == 5)
        {
            if (Leveloadedatpoint2 == false)
            {
                if (LevelSample1Rooms < MaxLevelSample1Rooms)
                {
                    LevelSample1Rooms += 1;
                    Instantiate(LevelSample1, SpawnLevelPoint2.transform.position, Quaternion.identity);
                    Leveloadedatpoint2 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint3room");
                }
                else
                {
                    StartCoroutine("Createpoint2room");
                }
            }
            else
            {
                StartCoroutine("Createpoint2room");
            }
        }
        if (RoomPicker == 6 || RoomPicker == 7 || RoomPicker == 8)
        {
            if (Leveloadedatpoint2 == false)
            {
                if (LevelSample2Rooms < MaxLevelSample2Rooms)
                {
                    LevelSample2Rooms += 1;
                    Instantiate(LevelSample2, SpawnLevelPoint2.transform.position, Quaternion.identity);
                    Leveloadedatpoint2 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint3room");
                }
                else
                {
                    StartCoroutine("Createpoint2room");
                }
            }
            else
            {
                StartCoroutine("Createpoint2room");
            }
        }
        if (RoomPicker == 9 || RoomPicker == 10 || RoomPicker == 11)
        {
            if (Leveloadedatpoint2 == false)
            {
                if (LevelSample3Rooms < MaxLevelSample3Rooms)
                {
                    LevelSample3Rooms += 1;
                    Instantiate(LevelSample3, SpawnLevelPoint2.transform.position, Quaternion.identity);
                    Leveloadedatpoint2 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint3room");
                }
                else
                {
                    StartCoroutine("Createpoint2room");
                }
            }
            else
            {
                StartCoroutine("Createpoint2room");
            }
        }
        if (RoomPicker == 12 || RoomPicker == 13 || RoomPicker == 14)
        {
            if (Leveloadedatpoint2 == false)
            {
                if (LevelSample4Rooms < MaxLevelSample4Rooms)
                {
                    LevelSample4Rooms += 1;
                    Instantiate(LevelSample4, SpawnLevelPoint2.transform.position, Quaternion.identity);
                    Leveloadedatpoint2 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint3room");
                }
                else
                {
                    StartCoroutine("Createpoint2room");
                }
            }
            else
            {
                StartCoroutine("Createpoint2room");
            }
        }
        if (RoomPicker == 15 || RoomPicker == 16 || RoomPicker == 17)
        {
            if (Leveloadedatpoint2 == false)
            {
                if (LevelSample5Rooms < MaxLevelSample5Rooms)
                {
                    LevelSample5Rooms += 1;
                    Instantiate(LevelSample5, SpawnLevelPoint2.transform.position, Quaternion.identity);
                    Leveloadedatpoint2 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint3room");
                }
                else
                {
                    StartCoroutine("Createpoint2room");
                }
            }
            else
            {
                StartCoroutine("Createpoint2room");
            }
        }
        if (RoomPicker == 18 || RoomPicker == 19 || RoomPicker == 20)
        {
            StartCoroutine("Createpoint2room");
        }

        Debug.Log("Point 2: The number is " + RoomPicker);
    }
    else
    {
        StartCoroutine("Createpoint3room");
    }
}
public IEnumerator Createpoint3room()
{
    if (Leveloadedatpoint3 == false)
    {
        RoomPicker = Random.Range(1, 20);
        yield return new WaitForSeconds(GenerationDelay);
        if (RoomPicker == 1 || RoomPicker == 2 || RoomPicker == 3 || RoomPicker == 4 || RoomPicker == 5)
        {
            if (Leveloadedatpoint3 == false)
            {
                if (LevelSample1Rooms < MaxLevelSample1Rooms)
                {
                    LevelSample1Rooms += 1;
                    Instantiate(LevelSample1, SpawnLevelPoint3.transform.position, Quaternion.identity);
                    Leveloadedatpoint3 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint4room");
                }
                else
                {
                    StartCoroutine("Createpoint3room");
                }
            }
            else
            {
                StartCoroutine("Createpoint3room");
            }
        }
        if (RoomPicker == 6 || RoomPicker == 7 || RoomPicker == 8)
        {
            if (Leveloadedatpoint3 == false)
            {
                if (LevelSample2Rooms < MaxLevelSample2Rooms)
                {
                    LevelSample2Rooms += 1;
                    Instantiate(LevelSample2, SpawnLevelPoint3.transform.position, Quaternion.identity);
                    Leveloadedatpoint3 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint4room");
                }
                else
                {
                    StartCoroutine("Createpoint3room");
                }
            }
            else
            {
                StartCoroutine("Createpoint3room");
            }
        }
        if (RoomPicker == 9 || RoomPicker == 10 || RoomPicker == 11)
        {
            if (Leveloadedatpoint3 == false)
            {
                if (LevelSample3Rooms < MaxLevelSample3Rooms)
                {
                    LevelSample3Rooms += 1;
                    Instantiate(LevelSample3, SpawnLevelPoint3.transform.position, Quaternion.identity);
                    Leveloadedatpoint3 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint4room");
                }
                else
                {
                    StartCoroutine("Createpoint3room");
                }
            }
            else
            {
                StartCoroutine("Createpoint3room");
            }
        }
        if (RoomPicker == 12 || RoomPicker == 13 || RoomPicker == 14)
        {
            if (Leveloadedatpoint3 == false)
            {
                if (LevelSample4Rooms < MaxLevelSample4Rooms)
                {
                    LevelSample4Rooms += 1;
                    Instantiate(LevelSample4, SpawnLevelPoint3.transform.position, Quaternion.identity);
                    Leveloadedatpoint3 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint4room");
                }
                else
                {
                    StartCoroutine("Createpoint3room");
                }
            }
            else
            {
                StartCoroutine("Createpoint3room");
            }
        }
        if (RoomPicker == 15 || RoomPicker == 16 || RoomPicker == 17)
        {
            if (Leveloadedatpoint3 == false)
            {
                if (LevelSample5Rooms < MaxLevelSample5Rooms)
                {
                    LevelSample5Rooms += 1;
                    Instantiate(LevelSample5, SpawnLevelPoint3.transform.position, Quaternion.identity);
                    Leveloadedatpoint3 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint4room");
                }
                else
                {
                    StartCoroutine("Createpoint3room");
                }
            }
            else
            {
                StartCoroutine("Createpoint3room");
            }
        }
        if (RoomPicker == 18 || RoomPicker == 19 || RoomPicker == 20)
        {
            StartCoroutine("Createpoint3room");
        }

        Debug.Log("Point 3: The number is " + RoomPicker);
    }
    else
    {
        StartCoroutine("Createpoint4room");
    }
}
public IEnumerator Createpoint4room()
{
    if (Leveloadedatpoint4 == false)
    {
        RoomPicker = Random.Range(1, 20);
        yield return new WaitForSeconds(GenerationDelay);
        if (RoomPicker == 1 || RoomPicker == 2 || RoomPicker == 3 || RoomPicker == 4 || RoomPicker == 5)
        {
            if (Leveloadedatpoint4 == false)
            {
                if (LevelSample1Rooms < MaxLevelSample1Rooms)
                {
                    LevelSample1Rooms += 1;
                    Instantiate(LevelSample1, SpawnLevelPoint4.transform.position, Quaternion.identity);
                    Leveloadedatpoint4 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint5room");
                }
                else
                {
                    StartCoroutine("Createpoint4room");
                }
            }
            else
            {
                StartCoroutine("Createpoint4room");
            }
        }
        if (RoomPicker == 6 || RoomPicker == 7 || RoomPicker == 8)
        {
            if (Leveloadedatpoint4 == false)
            {
                if (LevelSample2Rooms < MaxLevelSample2Rooms)
                {
                    LevelSample2Rooms += 1;
                    Instantiate(LevelSample2, SpawnLevelPoint4.transform.position, Quaternion.identity);
                    Leveloadedatpoint4 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint5room");
                }
                else
                {
                    StartCoroutine("Createpoint4room");
                }
            }
            else
            {
                StartCoroutine("Createpoint4room");
            }
        }
        if (RoomPicker == 9 || RoomPicker == 10 || RoomPicker == 11)
        {
            if (Leveloadedatpoint4 == false)
            {
                if (LevelSample3Rooms < MaxLevelSample3Rooms)
                {
                    LevelSample3Rooms += 1;
                    Instantiate(LevelSample3, SpawnLevelPoint4.transform.position, Quaternion.identity);
                    Leveloadedatpoint4 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint5room");
                }
                else
                {
                    StartCoroutine("Createpoint4room");
                }
            }
            else
            {
                StartCoroutine("Createpoint4room");
            }
        }
        if (RoomPicker == 12 || RoomPicker == 13 || RoomPicker == 14)
        {
            if (Leveloadedatpoint4 == false)
            {
                if (LevelSample4Rooms < MaxLevelSample4Rooms)
                {
                    LevelSample4Rooms += 1;
                    Instantiate(LevelSample4, SpawnLevelPoint4.transform.position, Quaternion.identity);
                    Leveloadedatpoint3 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint4room");
                }
                else
                {
                    StartCoroutine("Createpoint4room");
                }
            }
            else
            {
                StartCoroutine("Createpoint4room");
            }
        }
        if (RoomPicker == 15 || RoomPicker == 16 || RoomPicker == 17)
        {
            if (Leveloadedatpoint3 == false)
            {
                if (LevelSample5Rooms < MaxLevelSample5Rooms)
                {
                    LevelSample5Rooms += 1;
                    Instantiate(LevelSample5, SpawnLevelPoint4.transform.position, Quaternion.identity);
                    Leveloadedatpoint3 = true;
                    yield return new WaitForSeconds(GenerationDelay);
                    StartCoroutine("Createpoint5room");
                }
                else
                {
                    StartCoroutine("Createpoint4room");
                }
            }
            else
            {
                StartCoroutine("Createpoint4room");
            }
        }
        if (RoomPicker == 18 || RoomPicker == 19 || RoomPicker == 20)
        {
            StartCoroutine("Createpoint4room");
        }

        Debug.Log("Point 4: The number is " + RoomPicker);
    }
    else
    {
        StartCoroutine("Createpoint4room");
    }
}
public IEnumerator Createpoint5room()
{
    yield return new WaitForSeconds(GenerationDelay);
    Debug.Log("Generation finished");
}
}

Edit1: This error also seems to crash my Editor and crashes the build of the game as well.

Upvotes: 4

Views: 14183

Answers (3)

Underwater008
Underwater008

Reputation: 1

I had the same issue in Unity 2021, I tried the Clean Cache method and restarted Unity. The memory error was gone but some new warnings about missing referenced script showed up.

Upvotes: 0

H_7
H_7

Reputation: 277

The real solution is really simple and doesn't require the restart of Unity. Just go to preferences and under GI tab, hit Clean Cache button. No more errors for you !-)

Upvotes: 0

Junuxx
Junuxx

Reputation: 14251

I was getting tons of Stack allocator ALLOC_TEMP_THREAD has unfreed allocations, size ... messages. Which is almost, but not exactly the same message you're getting, but that might be because of the different Unity version (I'm on 2018.2.9).

But as described here, simply restarting Unity fixed it for me.

Upvotes: 4

Related Questions