GithubHelp home page GithubHelp logo

Comments (15)

cassidyclawson avatar cassidyclawson commented on August 21, 2024

I posted a $100 bounty for a fix to this bug, available to anyone (including the original author).
https://www.bountysource.com/issues/31041391-gif-conversion-creates-memory-leak

from nsgif.

khappucino avatar khappucino commented on August 21, 2024

So I believe that in NSGIF.m if you replace this line

CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF , frameCount, NULL);

if (fileURL == nil)
return nil;

with....

if (fileURL == nil)
return nil;

CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF , frameCount, NULL);

you will return without ever allocating destination, running Analyzer complained about this line and two other points.

One is during a failure to finalize the file, we return without releasing destination
if (!CGImageDestinationFinalize(destination)) {
NSLog(@"Failed to finalize GIF destination: %@", error);
perhaps add this guy ---> CFRelease(destination);
return nil;
}

and then apparently ImageWithScale should be named CopyImageWithScale or CreateImageWithScale (I don't know if this really matters but xcode complains that the naming convention of methods that return an owned +1 retain count instance should have the word Create or Copy in the method name).

from nsgif.

cassidyclawson avatar cassidyclawson commented on August 21, 2024

Awesome work, @khappucino! If you prepare your suggested changes as a pull request, and I can confirm the memory leak is resolved, I will be happy to award the bounty!

from nsgif.

khappucino avatar khappucino commented on August 21, 2024

Ok the pull request is in flight. Let us know if that actually fixes anything. Those Analyzer findings are primarily suggestions but hopefully it works.

from nsgif.

cassidyclawson avatar cassidyclawson commented on August 21, 2024

Thanks much @khappucino. I will check out your pull request shortly.

@sebyddd: are you going to also review the pull request and confirm it solves the problem? If so, I can defer to your judgement or I can make a determination myself and award the bounty if its indeed fixed.

Thanks all!

from nsgif.

sebyddd avatar sebyddd commented on August 21, 2024

@cassidyclawson: Yes, I will definitely check the pull request first thing tomorrow and update you.

from nsgif.

khappucino avatar khappucino commented on August 21, 2024

I'll waive the bounty even if it works. I am going to use this for one of my apps and I'm cool without the bounty if it helps this component.

from nsgif.

cassidyclawson avatar cassidyclawson commented on August 21, 2024

Just tested, and unfortunately, in my implementation, the code still produces a memory leak of the same size as the gif.

I read the pull request pretty carefully, and I had a hard time understanding how any of the changes would make a meaningful impact on this issue, though they were indeed improvements in syntax and edge cases. Thanks for the work so far, khappucino.

Any other ideas, gang?

from nsgif.

khappucino avatar khappucino commented on August 21, 2024

So I just ran the allocation profiler in my use case and there is a transient allocation of large buffers but after the gif is written to the camera roll (which is my use case) there are no 'created and persistant' allocations that are anywhere near the size of the created GIF. (I am not seeing a memory leak in the below usage scenario, the generated GIF was approximately 9-10 megs)

Here is the scenario that I am using is effectively this. With this usage

NSGIF.createGIFfromURL(url, withFrameCount: 30, delayTime: 0, loopCount: 0,
completion: {
[weak self](url : NSURL!) -> Void in
PHPhotoLibrary.sharedPhotoLibrary().performChanges({ weak self -> Void in
PHAssetChangeRequest.creationRequestForAssetFromImageAtFileURL(url)
},
completionHandler: nil })

For this use style here are the Analyzer traces, one shows the Created and Destroyed elements, and then for the same time period there is a trace for Created and Persistant. The trace window takes place right before NSGIF creates and then ends after the image is written to the camera roll.

screen shot 2016-02-21 at 8 46 47 pm
screen shot 2016-02-21 at 8 46 56 pm

from nsgif.

cassidyclawson avatar cassidyclawson commented on August 21, 2024

Well, thanks to the good work of khappucino, I should acknowledge that me and the other bug reporter are probably mistaken.

I believe that my previously okay camera code is now suffering from this iOS 9 camera bug: https://forums.developer.apple.com/thread/17142

I will be able to confirm later this weekend when working with an AV Foundation expert. I will close the bug bounty if we confirm the bug is outside this library. However, in appreciation for your hard work @khappucino, I will make a donation to a cause of our mutual choosing. I suggest:

  • Bernie Sanders US Presidential Campaign
  • Electronic Frontier Foundation
  • Wildlife Conservation Network

from nsgif.

ksenia-lyagusha avatar ksenia-lyagusha commented on August 21, 2024

I've changed the code as @khappucino wrote above but memory warning appears on iPhone 6+ when I set framesPerSecond = 10. On the other devices are ok.

What's a solution?
Thanks!

from nsgif.

zenyuhao avatar zenyuhao commented on August 21, 2024

hi all

I found the memory leak only happend on gif scaled in IOS device

and I add the line to fix the memory leak

#pragma mark - Helpers

CGImageRef ImageWithScale(CGImageRef imageRef, float scale) {

    #if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
    CGSize newSize = CGSizeMake(CGImageGetWidth(imageRef)*scale, CGImageGetHeight(imageRef)*scale);
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (!context) {
        return nil;
    }

    // Set the quality level to use when rescaling
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

    CGContextConcatCTM(context, flipVertical);
    // Draw into the context; this scales the image

    CGContextDrawImage(context, newRect, imageRef);

    // release last image
    CGImageRelease(imageRef); //<-------- this image ref is copy from generator; should be released before next step creating;

    // Get the resized image from the context and a UIImage
    imageRef = CGBitmapContextCreateImage(context);

    UIGraphicsEndImageContext();
    #endif

    return imageRef;
}

from nsgif.

abrez avatar abrez commented on August 21, 2024

Hello Gang,

I am receiving memory leak on 4S in NSGIF at following line createGIFforTimePoints
if (!CGImageDestinationFinalize(destination))

any ideas ?

from nsgif.

smhjsw avatar smhjsw commented on August 21, 2024

iOS 10 fix this bug.

from nsgif.

sebyddd avatar sebyddd commented on August 21, 2024

I was aware of an actual memory leak in AVFoundation that caused the converting to fail in some cases. So far it looks like iOS 10 fixes this issue and conversions are working seamlessly.
Despite this, I will still keep the library >= iOS 7.

from nsgif.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.