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
0
Question by Sendatsu_Yoshimitsu · Jul 29, 2017 at 07:16 AM · memoryeventmemory-leak

Do events need to be manually un-allocated before destruction?

I'm working on using events to set up a cursory observer pattern, and let's say I do something like the following:

 public delegate void SomeNotification(GameObject invokingObject, ObserverEvent eventEnum);
 
 public class SubjectClass : MonoBehaviour
 {
     public event SomeNotification testNotification;
 }
 
 public class ObservingClass : MonoBehaviour {
 
     SubjectClass testSubject;
 
     public void OnNotify(GameObject invokingObject, ObserverEvent eventEnum)
     {
 
     }
 
     void Awake()
     {
         testSubject = new SubjectClass();
         testSubject.testNotification += OnNotify;
         Destroy(testSubject);
     }
 }

I'll never get a null reference, because SubjectClass was the one subscribed to ObservingClass's OnNotify, not vice-versa. However, I never explicitly un-subscribed testNotification from the things it was talking to before destroying its object. Can I trust Unity to clean this up after the Destroy call, or have I just created an inadvertent memory leak by deleting SubjectClass before calling testNotification -=OnNotify?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Hanoble · Jul 29, 2017 at 09:25 AM

Without manually unsubscribing you still have a reference and it will not be garbage collected. A common technique is to use the OnDestroy() of the listening class to unsubscribe, or just explicitly do it before calling destroy.

Comment
Add comment · Show 1 · Share
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 Bunny83 · Jul 29, 2017 at 09:51 AM 2
Share

Right, that means the answer is "Yes". A managed object actually can't be destroyed. Unity's Destroy method only destroys the native C++ part of the class. The managed part will be removed by the GC once all referenced to that class go out of extent.

@Sendatsu_Yoshimitsu:

$$anonymous$$onoBehaviour derived classes must not be created with "new". If the class should be a component you have to use AddComponent to create an instance dynamically.

ps: technically it's not a "memory leak" by definition since it's possible to retrieve the reference stored in the multicast delegate using reflection. A memory leak is only a memory leak if there's no way from inside the application to free the memory. Also if "SubjectClass" is destroyed and finally garbage collected all classes refered by the delegate are collected as well if that was the last reference to those classes.

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

70 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

m_Memory.renderTextureBytes<0 1 Answer

Memory Leak Help 2 Answers

Object references listed in profiler but not in hierarchy. 1 Answer

Memory leak when running Process() 0 Answers

Memory Allocation Continues to Grow? iOS. 4.6.3 0 Answers


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