0
///************************** // MyClass.m #import "MyClass.h" static MyClass *myClass = NULL; @implementation MyClass (MyClass *)sharedMyClass { myClass = NULL; myClass = [[MyClass alloc] init]; return myClass; } @end 

Hi Experts, I just want to know myClass is a static ObjC object, it will be automatically released by ARC when no one is referencing it anymore, so no memory leak? is it correct?

6
  • You should look at the dispatch_once pattern. Here note you have a strong pointer to the class in static MyClass *myClass so unless you set that to nil it will never be released. But this is not really a leak as, presumably, for this kind of a thing you need some instance of the class ready throughout the life of the app? Commented Oct 8, 2020 at 4:03
  • Thanks, skaak. I intend to write this code instead of dispatch_one, I just want to test whether ARC would release the static variable automatically if it's nil. e.g. sharedMyClass will be called multiple times... Commented Oct 8, 2020 at 4:42
  • so you said "so unless you set that to nil it will never be released", that means the ARC should release myClass right? Commented Oct 8, 2020 at 4:43
  • Hi DT ... yes if you explicitly set it to nil and nothing else has a (strong) hold on it then it will be released. I mention dispatch_once as it seems you need a singleton. This is quite normal but typically happens a bit differently to how you do it. Now what you do is fine (and common) also, of course, but sometimes you create a single instance of your class and access it e.g. like the default file manager or standard user preferences or the default notification center. I trust you know which classes I am talking about. Commented Oct 8, 2020 at 6:51
  • 1
    ... All of these singletons needed to go a bit further than just a static variable and it is good to be aware of how to do so. I'll post an example just to make it clear. Commented Oct 8, 2020 at 6:52

2 Answers 2

1

Here is an example of a singleton class instance that you can use throughout your app if you need to. It never gets released and is not intended to be released.

@interface MyClass : NSObject + ( MyClass * ) singleton; @end 

That is the interface, so you'll access it as MyClass.singleton for example. The implementation is like this. The reason for the dispatch_once fluff is to make it safe in an environment where it might be accessed from multiple places all at once.

@implemenation MyClass () + ( MyClass * ) singleton { static MyClass * a; static dispatch_once_t t; dispatch_once ( & t, ^ { a = [[MyClass alloc] init]; } ); return a; } @end 

Note that this also rely on a static variable. That variable is retained from creation time until forever or the app quits.

Sign up to request clarification or add additional context in comments.

Comments

0

static MyClass *myClass creates a reference to the pointer stored into that value. As long as myClass will hold a valid object address, that object will not be deallocated. Basically this variable adds a +1 to the reference count of the stored object. If you want for that object to have a change to deallocate, you'll have to change the value of this variable to point to something else (or back to nil).

As a side note, static in Objective-C has no relation to the static modifier of class members, in C/Objective-C static means a symbol that is not visible to other files, e.g. a fileprivate one from Swift.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.