Skip to content
This repository has been archived by the owner on Jul 6, 2019. It is now read-only.

Memory leaks due to unsafe swizzling? #11

Closed
rsanchezsaez opened this issue Oct 16, 2014 · 9 comments
Closed

Memory leaks due to unsafe swizzling? #11

rsanchezsaez opened this issue Oct 16, 2014 · 9 comments

Comments

@rsanchezsaez
Copy link
Contributor

Hi,

Under some circumstances, Xcode's Memory Leaks Instrument seems to report leaks when using AnimatedGIFImageSerialization in combination with Crashlytics. I don't know if those are actual leaks or not. All of these have in common Crashlytics and AnimatedGIFImageSerialization:

screen shot 2014-10-16 at 12 39 15

The leaks seem to go away if you replace AnimatedGIFImageSerialization's swizzling by JRSwizzle, which seems to be safer for subclassing and inheritance:

[self jr_swizzleMethod:@selector(imageNamed:) withMethod:@selector(animated_gif_imageNamed:) error:nil];
[self jr_swizzleMethod:@selector(imageWithData:) withMethod:@selector(animated_gif_imageWithData:) error:nil];
[self jr_swizzleMethod:@selector(imageWithData:scale:) withMethod:@selector(animated_gif_imageWithData:scale:) error:nil];
[self jr_swizzleMethod:@selector(imageWithContentsOfFile:) withMethod:@selector(animated_gif_imageWithContentsOfFile:) error:nil];
[self jr_swizzleMethod:@selector(initWithContentsOfFile:) withMethod:@selector(animated_gif_initWithContentsOfFile:) error:nil];
[self jr_swizzleMethod:@selector(initWithData:) withMethod:@selector(animated_gif_initWithData:) error:nil];
[self jr_swizzleMethod:@selector(initWithData:scale:) withMethod:@selector(animated_gif_initWithData:scale:) error:nil];
@mattt
Copy link
Owner

mattt commented Oct 16, 2014

Swizzling doesn't cause leaks. It's not magic. JRSwizzle isn't doing anything differently than what's being used here (the real utility of that library is providing consistent support across versions of the Objective-C runtime).

Notice also that the leaked bytes correspond to a stack trace for creating PNG images (initImagePng) when Crashalytics fetches application icon assets.

Whatever the problem is, I don't see any evidence that it's caused by AnimatedGIFImageSerialization. The only reason it's in the stack trace at all is that it swizzled the implementation of UIImage +imageWithData:.

@mattt mattt closed this as completed Oct 16, 2014
@rsanchezsaez
Copy link
Contributor Author

Ok, I trust you.

But still, isn't it weird that these leaks go away when I replace swizzling by JRSwizzle on AnimatedGIFImageSerialization? Maybe it's just an Instruments bug.

@bwhiteley
Copy link

I'm seeing the same thing as @rsanchezsaez. Every image is leaked unless I switch to jr_swizzleMethod. I have an image-intensive app, and the OOM-killer says it's so in addition to Instruments.

@kostiadombrovsky
Copy link

I have the same issue - imageWithData = leaks, UIImageWithAnimatedGIFData no leaks.

@kostiadombrovsky
Copy link

screen shot 2016-01-29 at 16 19 50

@kostiadombrovsky
Copy link

@rsanchezsaez @bwhiteley it seems that the reason why JRSwizzle helped is that you're using it wrong.

[self jr_swizzleMethod:@selector(imageNamed:) withMethod:@selector(animated_gif_imageNamed:) error:nil];
[self jr_swizzleMethod:@selector(imageWithData:) withMethod:@selector(animated_gif_imageWithData:) error:nil];
[self jr_swizzleMethod:@selector(imageWithData:scale:) withMethod:@selector(animated_gif_imageWithData:scale:) error:nil];
[self jr_swizzleMethod:@selector(imageWithContentsOfFile:) withMethod:@selector(animated_gif_imageWithContentsOfFile:) error:nil];

jr_swizzleMethod fails actually if you try to use it for class methods. If you use jr_swizzleClassMethod: then it leaks as does @mattt 's implementation.

The whole thing works because [UIImage imageWithData:] is actually [[UIImage alloc] initWithData:] under the hood.

Still weird why it leaks 😓

@pionl
Copy link

pionl commented Apr 22, 2016

Still getting huge leaks, every image is leaked. When I remove the library, no leaks.

@dmitry-miller-jodel
Copy link

dmitry-miller-jodel commented Apr 22, 2016

I switched pull to refresh control (UzysAnimatedGifPullToRefresh) to use YYImage for gifs

@pionl
Copy link

pionl commented Apr 22, 2016

@dmitry-ivanov-rlt Hi Dmitry, thanks for the advice, will try think about switch. Unfortunately I need to enable saving GIF to a file and downloading it via AFNetworking (probably i can change the usage to get NSData instead of UIImage)

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

No branches or pull requests

6 participants