Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hasSharedInstance issue #15

Open
mrtristan opened this issue Apr 8, 2014 · 2 comments
Open

hasSharedInstance issue #15

mrtristan opened this issue Apr 8, 2014 · 2 comments

Comments

@mrtristan
Copy link

so perhaps i'm not properly nullifying my shared instances... i'm doing so by calling setSharedInstance:nil on a class. is this correct?

my issue here is after doing the above, hasSharedInstance returns true.

I drilled down in to the code and after expanding

+ (BOOL)hasSharedInstance
{
    return objc_getAssociatedObject(self, BMSharedInstanceKey) != nil;
}

to

+ (BOOL)hasSharedInstance
{
    id obj = objc_getAssociatedObject(self, BMSharedInstanceKey);
    return obj != nil;
}

before that return statement, that obj is set to an instance of the class it is called upon with all properties of the class as nil which of course means that that evaluation at the end yields true. is this by design? my expectation was that it would return false.

@nicklockwood
Copy link
Owner

I don't really understand what you mean by this. objc_getAssociatedObject(self, BMSharedInstanceKey) will return nil if the value associated to the BMSharedInstanceKey is nil, which it should be if you setSharedInstance:nil.

@mrtristan
Copy link
Author

sorry, never saw the response on this.

i think this might be a clearer way to explain, here is the flow of the issue:

[MyClass setSharedInstance:nil];
[MyClass hasSharedInstance]; //evaluates to true

when debugging the above example, i expanded your method as mentioned in the original post and observed id to not actually be nil, but as being an instantiated instance of the object with all properties set to nil.

does that help or make the issue more clear?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants