Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Essential · Nov 21, 2012 at 09:33 AM · instancenullsingletononapplicationquit

Is it necessary to null an instance on quit?

 static var instance : LevelData;
 
 function Awake ()
 {
     // Find itself
     instance = FindObjectOfType(LevelData);
     if (instance == null)
         Debug.Log ("Could not locate a singleton object in the scene.");
         
     // Retain between scenes
     DontDestroyOnLoad(gameObject);
 }
 
 // Ensure that the instance is destroyed when the game is stopped in the editor.
 function OnApplicationQuit() {
     instance = null;
 }

I'm trying to understand a JS singleton… I've condensed the basics down — from the unity wiki — into the above code and it works well but I have a niggling question. It has that function: OnApplicationQuit, null the instance — Isn't that redundant? Wouldn't it be emptied upon game quit anyway or am I missing something?

Comment
Add comment · Show 4
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 Kryptos · Nov 21, 2012 at 10:59 AM 0
Share

I don't know if it is necessary but it is certainly not a bad idea.

That way you can make sure that any resource/memory is freed, regarding of execution order or other constraints.

avatar image Berenger · Nov 21, 2012 at 11:31 AM 0
Share

The garbage collector is supposed to take care of it. But it's not hurting aanyone to do it :)

avatar image Essential · Nov 21, 2012 at 11:33 AM 0
Share

But quitting the application would be inherently deleting all instances from being in memory, wouldn't it? o_0

Or do some things get left over in memory when quitting the application? What systems does that happen on?

avatar image Kryptos · Nov 21, 2012 at 12:08 PM 1
Share

@Berenger No it doesn't. If a reference is kept to an object it cannot be collected.

@Essential When your run your game inside the editor you share the same memory as the editor, therefore some objects may be leaked. Of course in a standalone, this is not necessary since the virtual machine will release all allocated memory.

2 Replies

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

Answer by IT_Criminal · Nov 21, 2012 at 12:21 PM

when the editor closes game simulation the garbage collector takes care of everything anyway ... so there is absolutely no reason for this ... i've never done this and i've never had any problems

Comment
Add comment · Show 3 · 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 Kryptos · Nov 21, 2012 at 01:08 PM 2
Share

This is not true for static variables, which is the case here.

avatar image IT_Criminal · Nov 21, 2012 at 02:21 PM 0
Share

i see ... but anyway it's just editor lag ... on the real application everything gets deallocated when the application closes ... you can null stuff if you want ... but it doesn't really help

avatar image Essential · Nov 21, 2012 at 09:24 PM 0
Share

That's good info to be aware of kryptos. I think rather than going back through all my code and making sure I deallocate every static variable, I'll just keep it simple and leave it out.

avatar image
0

Answer by MarkFinn · Nov 21, 2012 at 10:02 AM

I certainly can't see any reason for it.

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

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

13 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

Related Questions

Object reference not set to an instance of an object 0 Answers

Static singleton is (not nullref -empty-) but assigned to "null" in the other Scenes 2 Answers

How to subtract variable from singleton instance? 0 Answers

Accessing Singleton Instance Methods Directly 2 Answers

DontDestroyOnLoad & Singleton 2 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