cruffenach / CRToast

A modern iOS toast view that can fit your notification needs
MIT License
4.17k stars 463 forks source link

Memory Leak #195

Closed gabosgab closed 8 years ago

gabosgab commented 8 years ago

Hello,

We've been noticing a pretty large memory leak that causes our app to crash pretty often when a number of these are shown during a session user session. We have an app that shows roughly 80-100 of these in a single session and probably stressing it quite a bit. We've seen that this eventually causes the device to hard reset due to either memory leaking or excessive page allocation. We see that roughly 4-8MB are allocated and freed when a notification is shown from the top.

We've profiled this in Instruments and aren't able to catch much because it seems to be rather elusive. We've pulled the library and replaced with a our own and all crashes have stopped.

Here's our usage that resulted in the memory leaks:

`// // POTPopupManager.m // Potion // // Created by Gabe Brown on 10/15/15. // Copyright © 2015 Potion Incorporated. All rights reserved. //

import "POTPopupManager.h"

import "CRToast.h"

@implementation POTPopupManager

@synthesize staticOptions;

/**

}

/**

}

/**

+(void)showSuccessPopupWithMessage:(NSString*)message completion:(void (^)(void))completion {

NSMutableDictionary *options = [POTPopupManager getOptions];

[CRToastManager dismissAllNotifications:YES];
options[kCRToastTextKey] = message;
options[kCRToastBackgroundColorKey] = [UIColor colorWithRed:0.20 green:0.69 blue:0.20 alpha:1.0];
options[kCRToastTextColorKey] = [UIColor whiteColor];
[POTPopupManager showPopupWithCompletion:completion options:options];

}

/**

/**

}

/**

+(void)showWarningPopupWithMessage:(NSString*)message completion:(void (^)(void))completion {

NSMutableDictionary *options = [POTPopupManager getOptions];

[CRToastManager dismissAllNotifications:NO];
options[kCRToastTextKey] = message;
options[kCRToastBackgroundColorKey] = [UIColor colorWithRed:0.59 green:0.04 blue:0.04 alpha:1.0];
options[kCRToastTextColorKey] = [UIColor whiteColor];
[POTPopupManager showPopupWithCompletion:completion options:options];

}

/**

}

@end`

dmiedema commented 8 years ago

Does the leak reproduce in the demo app by chance?

gabosgab commented 8 years ago

I haven't tried, but I imagine it does. I can try later.

gabosgab commented 8 years ago

Yes, I was able to reproduce the crash that resets the phone. I setup the demo app to open the drawer 161 times and then my iPhone 6 rebooted.

I modified the demo to continuously show to make it easy to test.

- (IBAction)btnShowNotificationPressed:(UIButton *)sender {
        [CRToastManager showNotificationWithOptions:[self options]
                                     apperanceBlock:^(void) {
                                         NSLog(@"Appeared");
                                     }
                                    completionBlock:^(void) {
                                        NSLog(@"Completed");
                                        [self btnShowNotificationPressed:sender];
                                    }];
}

Image of Yaktocat

chrisrjones commented 8 years ago

Thanks for finding this, I was about to use this library but recreated this crash easily. I can't believe it crashes the whole phone and not just the app.

gabosgab commented 8 years ago

Welcome, I think it crashes the phone because it leaks memory pages which is more severe since each app is given a finite amount. Good luck!

chrisrjones commented 8 years ago

Sorry, haven't had time to look at it until this morning. Fixed it with changing the CRToastView.h file, there is a strong reference to CRToast and it's causing it to not get released. Changing it to a weak reference fixed it.

@property (nonatomic, weak) CRToast *toast;

Before I couldn't get past ~160 times viewed before it crashed the whole phone but now just ran a test over 1k.

gabosgab commented 8 years ago

That was probably it, thanks!

On Fri, Apr 22, 2016 at 8:41 AM Chris Jones notifications@github.com wrote:

Also, I couldn't get past ~160 times viewed before but now just ran a test over 1k.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/cruffenach/CRToast/issues/195#issuecomment-213479584