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 YoungDeveloper · Sep 22, 2015 at 11:02 AM · uieventlistener

Why UI event handlers dont get memory leaks if listener not removed

I've seen common practice that users usually just AddListener, but never remove if listening gameObject is being destroyed.

 myButton.onClick.AddListener(()=>MyFunk()); } 

Is this handled behind the scenes, or Listener removal should be a must in Unity too? Personally i always remove it, im just really wondering.

 OnDestroy()
     myButton.onClick.RemoveListener()...
 }

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
3
Best Answer

Answer by fafase · Sep 24, 2015 at 08:54 AM

I could be misunderstanding the wording of the docs but AddListener is meant to add a non.persistent listener.

But, the following shows a weird behaviour (at least until I get it explained):

 private float myFloat = 10f;
 [SerializeField] private Button button = null;
 void Start( )
 {
     button.onClick.AddListener(MyMethod);
     DestroyImmediate(this.gameObject);
 }
 public void MyMethod()
 {
     Debug.Log("Call " + this.myFloat);
     this.myFloat ++;
 }

the best part being that the object is surely gone, the this.myFloat still gets printed AND increased, and I don't have any persistent listener.

If I remove in Destroy then all goes fine. It seems the AddListener is creating a link to the object in memory and even triggering the GC will not collect the object. Using a weak reference would probably bring it back to life I am thinking.

What is even better is that if I create a reference to my script above and call for the myFloat value, I get it??!! Despite the inspector showing Missing(Type)??!!...

This tells me that we have here a memory leak if the programmer is not removing the listener manually, until I am proven wrong. obviously.

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 YoungDeveloper · Sep 24, 2015 at 09:06 AM 0
Share

That's what i was thinking, Unity UI docs and people who create tutorials are not clearly pushing this in correct bounds. I've seen several commercial games code where this is standard behavior.

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

33 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

Related Questions

Button.OnClic.AddListener(delegate{method(object);}), wrong object sent 1 Answer

How to add OnClick listener in script to see it in inspector? 1 Answer

How would I detect a right click with an event trigger? 3 Answers

Chain UI animations with Events? 1 Answer

[4.6 - UI] How to call an event with button when the pointer is up the button image. 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