Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Issue with pooling

Discussion in 'Scripting' started by Flynn_Prime, May 11, 2023.

  1. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    Hello. I'm getting issues with my projectiles being destroyed instead of despawning back to their pool (using the magical LeanPool.Spawn/Despawn methods). This is what I get in the inspector with a debug in the OnDestroy method of the projectile and I really can't make sense of it:
    "
    Projectile (Projectile) destroyed instead of returning to the pool
    0x00007ff628afed1d (Unity) StackWalker::GetCurrentCallstack
    0x00007ff628b058d9 (Unity) StackWalker::ShowCallstack
    0x00007ff629a9d0a3 (Unity) GetStacktrace
    0x00007ff62a14d05d (Unity) DebugStringToFile
    0x00007ff627c607d2 (Unity) DebugLogHandler_CUSTOM_Internal_Log
    0x000001765562ca03 (Mono JIT Code) (wrapper managed-to-native) UnityEngine.DebugLogHandler:Internal_Log (UnityEngine.LogType,UnityEngine.LogOption,string,UnityEngine.Object)
    0x000001765562c93b (Mono JIT Code) UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])
    0x000001765562c6c0 (Mono JIT Code) UnityEngine.Logger:Log (UnityEngine.LogType,object)
    0x000001765b3a6fc5 (Mono JIT Code) UnityEngine.Debug:LogError (object)
    0x000001765b3a6f6b (Mono JIT Code) [Projectile.cs:16] Projectile:OnDestroy ()
    0x0000017655799218 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
    0x00007ffa4811feb4 (mono-2.0-bdwgc) [mini-runtime.c:3445] mono_jit_runtime_invoke
    0x00007ffa4805e764 (mono-2.0-bdwgc) [object.c:3066] do_runtime_invoke
    0x00007ffa4805e8fc (mono-2.0-bdwgc) [object.c:3113] mono_runtime_invoke
    0x00007ff628a22794 (Unity) scripting_method_invoke
    0x00007ff628a01f94 (Unity) ScriptingInvocation::Invoke
    0x00007ff628a0207e (Unity) ScriptingInvocation::InvokeChecked
    0x00007ff628a97d56 (Unity) SerializableManagedRef::CallMethod
    0x00007ff6289fc39b (Unity) MonoBehaviour::WillDestroyComponent
    0x00007ff6283b3944 (Unity) GameObject::WillDestroyGameObject
    0x00007ff6286c2961 (Unity) PreDestroyRecursive
    0x00007ff6286c2a48 (Unity) PreDestroyRecursive
    0x00007ff6286c08a7 (Unity) DestroyObjectHighLevel_Internal
    0x00007ff6286c04e4 (Unity) DestroyObjectHighLevel
    0x00007ff6286cae2e (Unity) DestroyWorldObjects
    0x00007ff629932c31 (Unity) EditorSceneManager::RestoreSceneBackups
    0x00007ff62963b4ed (Unity) PlayerLoopController::ExitPlayMode
    0x00007ff62964afe1 (Unity) PlayerLoopController::SetIsPlaying
    0x00007ff62964e44b (Unity) Application::TickTimer
    0x00007ff629aa323a (Unity) MainMessageLoop
    0x00007ff629aa7b0b (Unity) WinMain
    0x00007ff62ae86cbe (Unity) __scrt_common_main_seh
    0x00007ffa94727614 (KERNEL32) BaseThreadInitThunk
    0x00007ffa958c26a1 (ntdll) RtlUserThreadStart"

    My projectiles are only referred to in 3 places... The weapon that spawns them with LeanPool.Spawn. The projectile script that calls LeanPool.Despawn OnTriggerEnter. And a timer class that Despawns the object after a certain amount of time (problem still occurs with this timer script disabled). Stranger thing is that the issue doesn't appear right away but maybe 10 or so seconds into firing constantly. Any help would be appreciated!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    The costs and issues associated with object pooling / pools:

    https://forum.unity.com/threads/object-pooling.1329729/#post-8405055

    https://forum.unity.com/threads/object-pooling-in-a-tower-defense-game.1076897/#post-6945089

    If you insist on pooling anyway, well...

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  3. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    I've been debugging this for 3 days now; I know how to debug but this one has got me stumped. My original post is from a debug that I put in the OnDestroy method of my projectile to see if it was indeed getting destroyed. The error doesn't even refer to any of my own scripts which I thought would happen if my own code was causing the destroy. My code is almost as simple as LeanPool.Spawn(projectile) and then inside the OnTriggerEnter of the projectile LeanPool.Despawn(gameObject).
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    Considering this is an issue with an asset store plugin, you should probably ask the tool's developer, and consider whether this may just be a bug in the plugin?
     
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,903
  6. FaithlessOne

    FaithlessOne

    Joined:
    Jun 19, 2017
    Posts:
    257
  7. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    Thanks for replies guys. I had scoured for LeanPool issues and bugs but the dev pretty much replied to any of them with "you're definitely destroying the item somewhere". I've considered using the new built in object pooling but had always used LeanPool before with little problem. Probably will look at the built in version now as I don't have too much to change in this project to make it work.
     
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    Well, have you actually looked at your stacktrace? Especially this part:
    Code (CSharp):
    1. 0x00007ff6286cae2e (Unity) DestroyWorldObjects
    2. 0x00007ff629932c31 (Unity) EditorSceneManager::RestoreSceneBackups
    3. 0x00007ff62963b4ed (Unity) PlayerLoopController::ExitPlayMode
    4. 0x00007ff62964afe1 (Unity) PlayerLoopController::SetIsPlaying
    So the origin is that "is playing" is set to false. So you're exiting playmode and as such the scene and all objects in the scene will be destroyed. Your Projectile script
    Code (CSharp):
    1. 0x000001765b3a6fc5 (Mono JIT Code) UnityEngine.Debug:LogError (object)
    2. 0x000001765b3a6f6b (Mono JIT Code) [Projectile.cs:16] Projectile:OnDestroy ()
    Seems to print that error message in line 16.

    So I don't quite understand your problem. Do projectiles get destroyed during gameplay? Because what you posted is about the objects getting destroyed when you exit your game which is of course expected.
     
  9. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    Yes the projectiles are being destroyed at runtime, and the errors come then. Unless the stack trace I've copied is the one generated when I exit playmode, in which case I made a mistake there; but i'm almost positive that my Debug OnDestroy was displaying this during runtime. Not at my PC to confirm but I will later. I'm also getting LeanPool warnings that the Projectile I'm trying to spawn has been destroyed which is why I was using the OnDestroy Debug.Log in the first place
     
  10. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    But that's the point of having a stacktrace... It shows you who or where the object gets destroyed. So your whole post is kinda misleading. If you don't have actual evidence that you can show here that things get destroyed at runtime this isn't going anywhere :)
     
  11. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    Yes, I meant I may have made a mistake and copied over the wrong stack trace when I made this post.
     
    Bunny83 likes this.
  12. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    To figure out who is destroying your object, put your log message in the OnDisable() method, not OnDestroy(). UnityObject.Destroy() immediately disables the object but the actual destruction is "scheduled" and only happens on the next frame without any hint of where Destroy() was called.

    (And no, there's no way for OnDisable to tell the difference between an object being merely disabled versus it being scheduled for destruction: this is one of the many holes in Unity's APIs that's been there since forever and they have shown no intention of willing to fix.)
     
    Flynn_Prime likes this.
  13. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    I tried using another 3rd party pooler (that uses UnityEngine.Pool I believe) and after some playing around it seems I only get the errors when a projectile kills an enemy... My enemies are also pooled in a similar fashion. Adding a delay to the despawn of my projectile fixes the problem but I still don't understand why the errors occur anyway.My projectile just knows it hit something, and should despawn. Likewise, my enemy just knows it is dead and should despawn, I don't understand the conflict...

    My projectile script:
    Code (CSharp):
    1. public class Projectile : MonoBehaviour, IDamage
    2. {
    3.     [SerializeField] private float despawnTime;
    4.    
    5.     private DamageData data;
    6.  
    7.     private void OnEnable()
    8.     {
    9.         StartCoroutine(StartDespawnTimer());
    10.     }
    11.  
    12.     private void OnDisable()
    13.     {
    14.         StopAllCoroutines();
    15.     }
    16.  
    17.     private void OnTriggerEnter2D(Collider2D collision)
    18.     {
    19.         if (collision.TryGetComponent(out IDamageable damageable))
    20.         {
    21.             Damage(damageable);
    22.         }
    23.         //MasterObjectPooler.Instance.Release(gameObject, "ProjectilePool");
    24.         LeanPool.Despawn(gameObject, 0.01f);
    25.     }
    26.  
    27.     public void Initialise(DamageData data)
    28.     {
    29.         this.data = data;
    30.     }
    31.  
    32.     public void Damage(IDamageable receiver)
    33.     {
    34.         receiver.TakeDamage(data);
    35.     }
    36.  
    37.     private IEnumerator StartDespawnTimer()
    38.     {
    39.         yield return new WaitForSeconds(despawnTime);
    40.         //MasterObjectPooler.Instance.Release(gameObject, "ProjectilePool");
    41.         LeanPool.Despawn(gameObject);
    42.     }
    43. }
    44.  
    My Enemies Die() method:
    Code (CSharp):
    1.     private void Die()
    2.     {
    3.         EventManager.RaiseEnemyCountChangedEvent(false);
    4.         EventManager.RaiseEnemyDeathEvent(experienceValue.Value);
    5.         LeanPool.Despawn(gameObject);
    6.     }
    I'm happy to use the delay for now but it seems a bit hackish - although I will be using delays at some point for enemy death anims etc anyway but stil.
     
  14. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    Are you sure you don't have any Destroy or DestroyImmediate call in your project? Have you searched through the whole project? Maybe you still have some left over script? Another thing that can "destroy" objects are some of the build in components like a TrailRenderer or the ParticleAnimator of the legacy particle system. Can't think of others at the moment.

    Right, haven't thought about that.
     
  15. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    My project is still very small (like 27 scripts + a few plugins). In the entire project I only use Destroy() once, and that's for a Singleton declaration. I haven't even got as far as adding anything fancy to my projectile or other objects yet; my prototype projectile is just a yellow circle atm.

    Also, no difference to the stack trace if the Debug is in OnDisable instead of OnDestroy (although the original post was the incorrect one from exiting play mode). Here's the callstacks for both projectiles that despawn to pool correctly, and incorrectly respectively:

    Code (CSharp):
    1. Projectile disabled
    2. 0x00007ff60c26ed1d (Unity) StackWalker::GetCurrentCallstack
    3. 0x00007ff60c2758d9 (Unity) StackWalker::ShowCallstack
    4. 0x00007ff60d20d0a3 (Unity) GetStacktrace
    5. 0x00007ff60d8bd05d (Unity) DebugStringToFile
    6. 0x00007ff60b3d07d2 (Unity) DebugLogHandler_CUSTOM_Internal_Log
    7. 0x000001fa43f44603 (Mono JIT Code) (wrapper managed-to-native) UnityEngine.DebugLogHandler:Internal_Log (UnityEngine.LogType,UnityEngine.LogOption,string,UnityEngine.Object)
    8. 0x000001fa43f4451b (Mono JIT Code) UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])
    9. 0x000001fa43f44260 (Mono JIT Code) UnityEngine.Logger:Log (UnityEngine.LogType,object)
    10. 0x000001fa43f44128 (Mono JIT Code) UnityEngine.Debug:Log (object)
    11. 0x000001fa44010a93 (Mono JIT Code) [Projectile.cs:20] Projectile:OnDisable ()
    12. 0x000001fa443904e8 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
    13. 0x00007ffa3b50feb4 (mono-2.0-bdwgc) [mini-runtime.c:3445] mono_jit_runtime_invoke
    14. 0x00007ffa3b44e764 (mono-2.0-bdwgc) [object.c:3066] do_runtime_invoke
    15. 0x00007ffa3b44e8fc (mono-2.0-bdwgc) [object.c:3113] mono_runtime_invoke
    16. 0x00007ff60c192794 (Unity) scripting_method_invoke
    17. 0x00007ff60c171f94 (Unity) ScriptingInvocation::Invoke
    18. 0x00007ff60c17207e (Unity) ScriptingInvocation::InvokeChecked
    19. 0x00007ff60c207d56 (Unity) SerializableManagedRef::CallMethod
    20. 0x00007ff60c164912 (Unity) MonoBehaviour::RemoveFromManager
    21. 0x00007ff60bb1a29d (Unity) GameObject::ActivateAwakeRecursivelyInternal
    22. 0x00007ff60bb19ea9 (Unity) GameObject::ActivateAwakeRecursively
    23. 0x00007ff60bb21d17 (Unity) GameObject::SetSelfActive
    24. 0x00007ff60b3c9c0b (Unity) GameObject_CUSTOM_SetActive
    25. 0x000001fa43ef080c (Mono JIT Code) (wrapper managed-to-native) UnityEngine.GameObject:SetActive (UnityEngine.GameObject,bool)
    26. 0x000001fa4898806b (Mono JIT Code) [LeanGameObjectPool.cs:715] Lean.Pool.LeanGameObjectPool:DespawnNow (UnityEngine.GameObject,bool)
    27. 0x000001fa48987e93 (Mono JIT Code) [LeanGameObjectPool.cs:691] Lean.Pool.LeanGameObjectPool:TryDespawn (UnityEngine.GameObject)
    28. 0x000001fa48987563 (Mono JIT Code) [LeanGameObjectPool.cs:411] Lean.Pool.LeanGameObjectPool:Despawn (UnityEngine.GameObject,single)
    29. 0x000001fa48987373 (Mono JIT Code) [LeanPool.cs:153] Lean.Pool.LeanPool:Despawn (UnityEngine.GameObject,single)
    30. 0x000001fa48973d3b (Mono JIT Code) [Projectile.cs:31] Projectile:OnTriggerEnter2D (UnityEngine.Collider2D)
    31. 0x000001fa4695dcb3 (Mono JIT Code) (wrapper runtime-invoke) <Module>:runtime_invoke_void__this___object (object,intptr,intptr,intptr)
    32. 0x00007ffa3b50feb4 (mono-2.0-bdwgc) [mini-runtime.c:3445] mono_jit_runtime_invoke
    33. 0x00007ffa3b44e764 (mono-2.0-bdwgc) [object.c:3066] do_runtime_invoke
    34. 0x00007ffa3b44e8fc (mono-2.0-bdwgc) [object.c:3113] mono_runtime_invoke
    35. 0x00007ff60c192794 (Unity) scripting_method_invoke
    36. 0x00007ff60c171f94 (Unity) ScriptingInvocation::Invoke
    37. 0x00007ff60c15faec (Unity) MonoBehaviour::HandleNotifications
    38. 0x00007ff60bb213ab (Unity) GameObject::SendMessageAny
    39. 0x00007ff60c883866 (Unity) PhysicsContacts2D::SendCallbackReports
    40. 0x00007ff60c881d71 (Unity) PhysicsContacts2D::ProcessContacts
    41. 0x00007ff60c851c58 (Unity) PhysicsManager2D::Simulate
    42. 0x00007ff60c84e18f (Unity) PhysicsManager2D::FixedUpdate
    43. 0x00007ff60c84e2d6 (Unity) `PhysicsManager2D::Initialize'::`2'::FixedUpdatePhysics2DFixedUpdateRegistrator::Forward
    44. 0x00007ff60be468ba (Unity) ExecutePlayerLoop
    45. 0x00007ff60be46a46 (Unity) ExecutePlayerLoop
    46. 0x00007ff60be4cb29 (Unity) PlayerLoop
    47. 0x00007ff60cdbffb9 (Unity) PlayerLoopController::UpdateScene
    48. 0x00007ff60cdbe15b (Unity) Application::TickTimer
    49. 0x00007ff60d21323a (Unity) MainMessageLoop
    50. 0x00007ff60d217b0b (Unity) WinMain
    51. 0x00007ff60e5f6cbe (Unity) __scrt_common_main_seh
    52. 0x00007ffa94727614 (KERNEL32) BaseThreadInitThunk
    53. 0x00007ffa958c26a1 (ntdll) RtlUserThreadStart
    Code (CSharp):
    1. Projectile disabled
    2. 0x00007ff60c26ed1d (Unity) StackWalker::GetCurrentCallstack
    3. 0x00007ff60c2758d9 (Unity) StackWalker::ShowCallstack
    4. 0x00007ff60d20d0a3 (Unity) GetStacktrace
    5. 0x00007ff60d8bd05d (Unity) DebugStringToFile
    6. 0x00007ff60b3d07d2 (Unity) DebugLogHandler_CUSTOM_Internal_Log
    7. 0x000001fa43f44603 (Mono JIT Code) (wrapper managed-to-native) UnityEngine.DebugLogHandler:Internal_Log (UnityEngine.LogType,UnityEngine.LogOption,string,UnityEngine.Object)
    8. 0x000001fa43f4451b (Mono JIT Code) UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])
    9. 0x000001fa43f44260 (Mono JIT Code) UnityEngine.Logger:Log (UnityEngine.LogType,object)
    10. 0x000001fa43f44128 (Mono JIT Code) UnityEngine.Debug:Log (object)
    11. 0x000001fa44010a93 (Mono JIT Code) [Projectile.cs:20] Projectile:OnDisable ()
    12. 0x000001fa443904e8 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
    13. 0x00007ffa3b50feb4 (mono-2.0-bdwgc) [mini-runtime.c:3445] mono_jit_runtime_invoke
    14. 0x00007ffa3b44e764 (mono-2.0-bdwgc) [object.c:3066] do_runtime_invoke
    15. 0x00007ffa3b44e8fc (mono-2.0-bdwgc) [object.c:3113] mono_runtime_invoke
    16. 0x00007ff60c192794 (Unity) scripting_method_invoke
    17. 0x00007ff60c171f94 (Unity) ScriptingInvocation::Invoke
    18. 0x00007ff60c17207e (Unity) ScriptingInvocation::InvokeChecked
    19. 0x00007ff60c207d56 (Unity) SerializableManagedRef::CallMethod
    20. 0x00007ff60c164912 (Unity) MonoBehaviour::RemoveFromManager
    21. 0x00007ff60bb1a29d (Unity) GameObject::ActivateAwakeRecursivelyInternal
    22. 0x00007ff60bb19ea9 (Unity) GameObject::ActivateAwakeRecursively
    23. 0x00007ff60bb21d17 (Unity) GameObject::SetSelfActive
    24. 0x00007ff60b3c9c0b (Unity) GameObject_CUSTOM_SetActive
    25. 0x000001fa43ef080c (Mono JIT Code) (wrapper managed-to-native) UnityEngine.GameObject:SetActive (UnityEngine.GameObject,bool)
    26. 0x000001fa4898806b (Mono JIT Code) [LeanGameObjectPool.cs:715] Lean.Pool.LeanGameObjectPool:DespawnNow (UnityEngine.GameObject,bool)
    27. 0x000001fa48987e93 (Mono JIT Code) [LeanGameObjectPool.cs:691] Lean.Pool.LeanGameObjectPool:TryDespawn (UnityEngine.GameObject)
    28. 0x000001fa48987563 (Mono JIT Code) [LeanGameObjectPool.cs:411] Lean.Pool.LeanGameObjectPool:Despawn (UnityEngine.GameObject,single)
    29. 0x000001fa48987373 (Mono JIT Code) [LeanPool.cs:153] Lean.Pool.LeanPool:Despawn (UnityEngine.GameObject,single)
    30. 0x000001fa48973d3b (Mono JIT Code) [Projectile.cs:31] Projectile:OnTriggerEnter2D (UnityEngine.Collider2D)
    31. 0x000001fa4695dcb3 (Mono JIT Code) (wrapper runtime-invoke) <Module>:runtime_invoke_void__this___object (object,intptr,intptr,intptr)
    32. 0x00007ffa3b50feb4 (mono-2.0-bdwgc) [mini-runtime.c:3445] mono_jit_runtime_invoke
    33. 0x00007ffa3b44e764 (mono-2.0-bdwgc) [object.c:3066] do_runtime_invoke
    34. 0x00007ffa3b44e8fc (mono-2.0-bdwgc) [object.c:3113] mono_runtime_invoke
    35. 0x00007ff60c192794 (Unity) scripting_method_invoke
    36. 0x00007ff60c171f94 (Unity) ScriptingInvocation::Invoke
    37. 0x00007ff60c15faec (Unity) MonoBehaviour::HandleNotifications
    38. 0x00007ff60bb213ab (Unity) GameObject::SendMessageAny
    39. 0x00007ff60c8838ad (Unity) PhysicsContacts2D::SendCallbackReports
    40. 0x00007ff60c881d71 (Unity) PhysicsContacts2D::ProcessContacts
    41. 0x00007ff60c851c58 (Unity) PhysicsManager2D::Simulate
    42. 0x00007ff60c84e18f (Unity) PhysicsManager2D::FixedUpdate
    43. 0x00007ff60c84e2d6 (Unity) `PhysicsManager2D::Initialize'::`2'::FixedUpdatePhysics2DFixedUpdateRegistrator::Forward
    44. 0x00007ff60be468ba (Unity) ExecutePlayerLoop
    45. 0x00007ff60be46a46 (Unity) ExecutePlayerLoop
    46. 0x00007ff60be4cb29 (Unity) PlayerLoop
    47. 0x00007ff60cdbffb9 (Unity) PlayerLoopController::UpdateScene
    48. 0x00007ff60cdbe15b (Unity) Application::TickTimer
    49. 0x00007ff60d21323a (Unity) MainMessageLoop
    50. 0x00007ff60d217b0b (Unity) WinMain
    51. 0x00007ff60e5f6cbe (Unity) __scrt_common_main_seh
    52. 0x00007ffa94727614 (KERNEL32) BaseThreadInitThunk
    53. 0x00007ffa958c26a1 (ntdll) RtlUserThreadStart
    From what I can see everything is exactly the same EXCEPT:
    0x00007ff60c883866 (Unity) PhysicsContacts2D::SendCallbackReports
    becomes:
    0x00007ff60c8838ad (Unity) PhysicsContacts2D::SendCallbackReports

    Unfortunately this is beyond my level of understanding, and I'm not even sure if it means anything. Although I'm suspecting it has something to do with the physics of my projectile and enemy conflicting somehow.
     
  16. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    How do you determine that the despawn was incorrectly? The object wasn't destroyed in both cases. The object was just deactivated because SetActive was called in responds to being returned to the pool. Do you actually get any OnDestroy calls beside those from unloading the scene? If you have trouble to "match" the OnDestroy and OnDisable calls, you can always give your Projectile instances a unique ID. Just add this to the Projectile class:

    Code (CSharp):
    1. private static int m_IDCounter = 0;
    2. private int m_ID = m_IDCounter++;
    3.  
    4. void OnDestroy()
    5. {
    6.     Debug.Log("Destroy Projectile #"+m_ID+" at frame " + Time.frameCount);
    7. }
    8. void OnDisable()
    9. {
    10.     Debug.Log("Disable Projectile #"+m_ID+" at frame " + Time.frameCount);
    11. }
    12. void OnEnable()
    13. {
    14.     Debug.Log("Enable Projectile #"+m_ID+" at frame " + Time.frameCount);
    15. }
    16.  
    This way you can actually identify every Projectile instance by it's ID. No two instances would have the same id (unless you create more than 4 billion). You also see the actual frame the callback happens. So if an object is actually destroyed, the corresponding disable log would at most be 1 frame apart, but I think it should still be the same frame, just later at the end of the frame.
     
  17. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    Thanks for that @Bunny83. I didn't think to assign them seperate ID's. Anyhow, it seems (almost randomly) that sometimes my projectile will get destroyed and disabled on the same frame:

    Screenshot 2023-05-13 135302.png

    Stacktraces for each:

    Code (CSharp):
    1. Disable Projectile #8 at frame 7548
    2. 0x00007ff7963aed1d (Unity) StackWalker::GetCurrentCallstack
    3. 0x00007ff7963b58d9 (Unity) StackWalker::ShowCallstack
    4. 0x00007ff79734d0a3 (Unity) GetStacktrace
    5. 0x00007ff7979fd05d (Unity) DebugStringToFile
    6. 0x00007ff7955107d2 (Unity) DebugLogHandler_CUSTOM_Internal_Log
    7. 0x00000277c540b003 (Mono JIT Code) (wrapper managed-to-native) UnityEngine.DebugLogHandler:Internal_Log (UnityEngine.LogType,UnityEngine.LogOption,string,UnityEngine.Object)
    8. 0x00000277c540af1b (Mono JIT Code) UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])
    9. 0x00000277c540ac60 (Mono JIT Code) UnityEngine.Logger:Log (UnityEngine.LogType,object)
    10. 0x00000277c540ab28 (Mono JIT Code) UnityEngine.Debug:Log (object)
    11. 0x00000277cb3fbc0b (Mono JIT Code) [Projectile.cs:28] Projectile:OnDisable ()
    12. 0x00000277cb33c0e8 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
    13. 0x00007ffa4631feb4 (mono-2.0-bdwgc) [mini-runtime.c:3445] mono_jit_runtime_invoke
    14. 0x00007ffa4625e764 (mono-2.0-bdwgc) [object.c:3066] do_runtime_invoke
    15. 0x00007ffa4625e8fc (mono-2.0-bdwgc) [object.c:3113] mono_runtime_invoke
    16. 0x00007ff7962d2794 (Unity) scripting_method_invoke
    17. 0x00007ff7962b1f94 (Unity) ScriptingInvocation::Invoke
    18. 0x00007ff7962b207e (Unity) ScriptingInvocation::InvokeChecked
    19. 0x00007ff796347d56 (Unity) SerializableManagedRef::CallMethod
    20. 0x00007ff7962a4912 (Unity) MonoBehaviour::RemoveFromManager
    21. 0x00007ff795c5a29d (Unity) GameObject::ActivateAwakeRecursivelyInternal
    22. 0x00007ff795c59ea9 (Unity) GameObject::ActivateAwakeRecursively
    23. 0x00007ff795c61d17 (Unity) GameObject::SetSelfActive
    24. 0x00007ff795509c0b (Unity) GameObject_CUSTOM_SetActive
    25. 0x00000277c53b3bdc (Mono JIT Code) (wrapper managed-to-native) UnityEngine.GameObject:SetActive (UnityEngine.GameObject,bool)
    26. 0x00000277cb3fb80b (Mono JIT Code) [LeanGameObjectPool.cs:715] Lean.Pool.LeanGameObjectPool:DespawnNow (UnityEngine.GameObject,bool)
    27. 0x00000277cb3fb633 (Mono JIT Code) [LeanGameObjectPool.cs:691] Lean.Pool.LeanGameObjectPool:TryDespawn (UnityEngine.GameObject)
    28. 0x00000277cb3fb4a3 (Mono JIT Code) [LeanGameObjectPool.cs:411] Lean.Pool.LeanGameObjectPool:Despawn (UnityEngine.GameObject,single)
    29. 0x00000277cb3fb2b3 (Mono JIT Code) [LeanPool.cs:153] Lean.Pool.LeanPool:Despawn (UnityEngine.GameObject,single)
    30. 0x00000277cb3fb143 (Mono JIT Code) [Projectile.cs:34] Projectile:OnTriggerEnter2D (UnityEngine.Collider2D)
    31. 0x00000277cb3e2b43 (Mono JIT Code) (wrapper runtime-invoke) <Module>:runtime_invoke_void__this___object (object,intptr,intptr,intptr)
    32. 0x00007ffa4631feb4 (mono-2.0-bdwgc) [mini-runtime.c:3445] mono_jit_runtime_invoke
    33. 0x00007ffa4625e764 (mono-2.0-bdwgc) [object.c:3066] do_runtime_invoke
    34. 0x00007ffa4625e8fc (mono-2.0-bdwgc) [object.c:3113] mono_runtime_invoke
    35. 0x00007ff7962d2794 (Unity) scripting_method_invoke
    36. 0x00007ff7962b1f94 (Unity) ScriptingInvocation::Invoke
    37. 0x00007ff79629faec (Unity) MonoBehaviour::HandleNotifications
    38. 0x00007ff795c613ab (Unity) GameObject::SendMessageAny
    39. 0x00007ff7969c3866 (Unity) PhysicsContacts2D::SendCallbackReports
    40. 0x00007ff7969c1d71 (Unity) PhysicsContacts2D::ProcessContacts
    41. 0x00007ff796991c58 (Unity) PhysicsManager2D::Simulate
    42. 0x00007ff79698e18f (Unity) PhysicsManager2D::FixedUpdate
    43. 0x00007ff79698e2d6 (Unity) `PhysicsManager2D::Initialize'::`2'::FixedUpdatePhysics2DFixedUpdateRegistrator::Forward
    44. 0x00007ff795f868ba (Unity) ExecutePlayerLoop
    45. 0x00007ff795f86a46 (Unity) ExecutePlayerLoop
    46. 0x00007ff795f8cb29 (Unity) PlayerLoop
    47. 0x00007ff796efffb9 (Unity) PlayerLoopController::UpdateScene
    48. 0x00007ff796efe15b (Unity) Application::TickTimer
    49. 0x00007ff79735323a (Unity) MainMessageLoop
    50. 0x00007ff797357b0b (Unity) WinMain
    51. 0x00007ff798736cbe (Unity) __scrt_common_main_seh
    52. 0x00007ffa94727614 (KERNEL32) BaseThreadInitThunk
    53. 0x00007ffa958c26a1 (ntdll) RtlUserThreadStart
    Code (CSharp):
    1. Destroy Projectile #8 at frame 7548
    2. 0x00007ff7963aed1d (Unity) StackWalker::GetCurrentCallstack
    3. 0x00007ff7963b58d9 (Unity) StackWalker::ShowCallstack
    4. 0x00007ff79734d0a3 (Unity) GetStacktrace
    5. 0x00007ff7979fd05d (Unity) DebugStringToFile
    6. 0x00007ff7955107d2 (Unity) DebugLogHandler_CUSTOM_Internal_Log
    7. 0x00000277c540b003 (Mono JIT Code) (wrapper managed-to-native) UnityEngine.DebugLogHandler:Internal_Log (UnityEngine.LogType,UnityEngine.LogOption,string,UnityEngine.Object)
    8. 0x00000277c540af1b (Mono JIT Code) UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])
    9. 0x00000277c540ac60 (Mono JIT Code) UnityEngine.Logger:Log (UnityEngine.LogType,object)
    10. 0x00000277c540ab28 (Mono JIT Code) UnityEngine.Debug:Log (object)
    11. 0x000002787fb6521b (Mono JIT Code) [Projectile.cs:17] Projectile:OnDestroy ()
    12. 0x00000277cb33c0e8 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
    13. 0x00007ffa4631feb4 (mono-2.0-bdwgc) [mini-runtime.c:3445] mono_jit_runtime_invoke
    14. 0x00007ffa4625e764 (mono-2.0-bdwgc) [object.c:3066] do_runtime_invoke
    15. 0x00007ffa4625e8fc (mono-2.0-bdwgc) [object.c:3113] mono_runtime_invoke
    16. 0x00007ff7962d2794 (Unity) scripting_method_invoke
    17. 0x00007ff7962b1f94 (Unity) ScriptingInvocation::Invoke
    18. 0x00007ff7962b207e (Unity) ScriptingInvocation::InvokeChecked
    19. 0x00007ff796347d56 (Unity) SerializableManagedRef::CallMethod
    20. 0x00007ff7962ac39b (Unity) MonoBehaviour::WillDestroyComponent
    21. 0x00007ff795c63944 (Unity) GameObject::WillDestroyGameObject
    22. 0x00007ff795f72961 (Unity) PreDestroyRecursive
    23. 0x00007ff795f708a7 (Unity) DestroyObjectHighLevel_Internal
    24. 0x00007ff795f704e4 (Unity) DestroyObjectHighLevel
    25. 0x00007ff795d8b47b (Unity) DelayedDestroyCallback
    26. 0x00007ff795d8cf64 (Unity) DelayedCallManager::Update
    27. 0x00007ff795f9f8f2 (Unity) `InitPlayerLoopCallbacks'::`2'::FixedUpdateScriptRunDelayedFixedFrameRateRegistrator::Forward
    28. 0x00007ff795f868ba (Unity) ExecutePlayerLoop
    29. 0x00007ff795f86a46 (Unity) ExecutePlayerLoop
    30. 0x00007ff795f8cb29 (Unity) PlayerLoop
    31. 0x00007ff796efffb9 (Unity) PlayerLoopController::UpdateScene
    32. 0x00007ff796efe15b (Unity) Application::TickTimer
    33. 0x00007ff79735323a (Unity) MainMessageLoop
    34. 0x00007ff797357b0b (Unity) WinMain
    35. 0x00007ff798736cbe (Unity) __scrt_common_main_seh
    36. 0x00007ffa94727614 (KERNEL32) BaseThreadInitThunk
    37. 0x00007ffa958c26a1 (ntdll) RtlUserThreadStart
    Going by this you would think it was a bug with LeanPool, but I've tried other pooling methods with similar results. I can't see anything in the OnDestroy stacktrace that relates to my own code, and the OnDisable stacktrace looks to function correctly by Despawing OnTriggerEnter.

    Also, worth noting is that if I do a gameobject.activeself check before calling LeanPool.Despawn, the errors go away. I'm so confused as to what else could possibly be disabling the projectiles!
     
    Last edited: May 13, 2023