Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Glurth · Oct 26, 2015 at 08:12 PM · destroy objectmemory managementmemory leak

Is this generalized Destroy function safe to use?

I’ve been using dynamic meshes and custom scriptable objects in my program. Sometimes these objects are stored as ASSETS, on disk, sometimes they are just dynamically created and never stored to disk. Unfortunately, my classes that USE/reference these objects are unaware if they are a disk stored asset, or dynamically created. Oftentimes these objects are used while in editor mode, as opposed to just when the game is running. (In this case the dynamically created objects are stored inside a scene’s GameObject.)

I do the following whenever I reassign one of these object references in my code. First, I confirm the new Object is actually different from the currently assigned object. If so, to keep the replaced objects from “leaking” I call Objet.Destroy() on them, just before reassignment. Of course, this generates an error, when I’m in edit mode, that I’m supposed to use DestroyImmidiate(), which in turn leads to messages about destroying saved assets.

So I wrote the super generalized Destroy function, shown below, to try and handle the various conditions where different destroy functions need to be called (or not called!). I get the feeling I’m doing something dangerous here, and there is SOME reason I should not use the below. But, I can’t put my finger on it.

Is there some reason I do NOT want to use the below, or does it seem perfectly safe?

  public static void DestroyObjectAppropriately(Object destroyme)
     {
         if (destroyme == null) return;
         if (Application.isPlaying)
         {
             Object.Destroy(destroyme);
             return;
         }
         else
         {  //if app is not playing, safe to assume we are in the editor.  Still, the below #if is needed to allow release builds to compile, or the use of AssetDatabase will generate an error.
             #if UNITY_EDITOR
             if (AssetDatabase.GetAssetPath(destroyme) == "") //saved assets will not have a black AssetPath
                 Object.DestroyImmediate(destroyme, false);
             #endif
         }
     }

In my code, I would use this function like so:

 if (objectFramedMeshFilter.sharedMesh != wireFrameSourceMeshAsset)
 {
    ResourceSystem.DestroyObjectAppropriately(wireFrameSourceMeshAsset);
    wireFrameSourceMeshAsset = objectFramedMeshFilter.sharedMesh;
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Addyarb · Oct 26, 2015 at 09:48 PM 1
Share

Looks pretty solid to me. You can use string.Empty on line 13 if you really want to be efficient - but other than that it should work fine!

Think I'll use this type of method myself, as I find myself getting annoyed with hard coding individual destroy methods :)

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to free memory of UnityWebRequest? 1 Answer

PathNameManager and BaseObjectManager filling memory in Profiler 2 Answers

Not destroy enemies in 2d game - memory impact 0 Answers

Is it bad to use static on [practically] everything? 2 Answers

editor script memory leak 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges