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

[Crash] NSInvalidArgumentException at +[UIImage(Additions) add_cachedImageWithDescriptors:] #9

Closed
Bluezen opened this issue Jun 28, 2016 · 2 comments

Comments

@Bluezen
Copy link

Bluezen commented Jun 28, 2016

Hello,
First of all thank you for the nice library!

I have got an app that use it in production but unfortunately Fabric reports many crashes from users at line 413:

+ (UIImage*)add_cachedImageWithDescriptors:(NSDictionary*)descriptors
{
    return [[self add_cache] objectForKey:[self add_keyForImageWithDescriptors:descriptors]];
}

with the exception:

Fatal Exception: NSInvalidArgumentException

-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x148027b50

How come does [self add_cache] could be an NSString? Do you have any idea how to prevent this crash to happen ? Should we re-think the way _imageCache is initialized?

+ (NSCache*)add_cache
{
    if (!_imageCache)
        _imageCache = [[NSCache alloc] init];

    return _imageCache;
}

To give you more insight, I make use of

[UIImage add_imageWithGradient:(...) size:(...) direction:ADDImageGradientDirectionVertical];

in a background thread, could that be the reason why the crash is random? (I can't seem to reproduce it reliably). Should I make the call only from the main thread and would that be enough to never encounter the crash again?

@vilanovi
Copy link
Owner

Hello,

Thanks for reporting the issue. This problem has never happened to me before. However, I guess that the current implementation is not thread safe when creating the NSCache instance.

Therefore, I've replaced the current implementation for a dispatch_once block. This should prevent any thread issue when accessing the +add_cache method.

You will find the changes in version 2.1.1 (already available in Cocoa Pods).

For now I will close the issue, however if the problem reappears, please let me know and I'll reopen it.

Thanks!

@Bluezen
Copy link
Author

Bluezen commented Jun 29, 2016

Thank you! I will let you know if the problem reappears!

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