FlutterViewController内存泄露问题

65 篇文章 1 订阅

用的Flutter是1.9。

发现FlutterViewController的dealloc方法不执行。

经查是注册channel的原因,引起FlutterViewController不释放。

参考:

https://www.jianshu.com/p/1173906e73b2

https://github.com/flutter/flutter/issues/26007

FlutterBinaryMessenger is strongly referenced in FlutterMethodChannel. If the FlutterBinaryMessenger is a FlutterViewController, then this channel should be strongly referenced so that it can work, as the result the FlutterViewController is strongly referenced too. There is no callback for the controller finally releasing so that I can remove this channel.

下面的self被强引用了,造成了循环引用。

[FlutterMethodChannel
                    methodChannelWithName:commonChannelName
                    binaryMessenger:self];

//如查设成nil的话,就可以释放。但是不可以设nil。
[FlutterMethodChannel
                    methodChannelWithName:commonChannelName
                    binaryMessenger:nil];

 

因为NSTimer也是强引用target产生循环引用,所以想到用解决NSTimer循环引用的方式解决。

参考:

https://www.jianshu.com/p/927449ff3117

解决方法如下:

使用:

NSString *commonChannelName = @"com.test/commmon_param";

FlutterMethodChannel *commonChannel = [FlutterMethodChannel

                                           methodChannelWithName:commonChannelName

                                           binaryMessenger:[WeakProxy proxyWithTarget:self]];

 

WeakProxy.h

@interface WeakProxy : NSProxy

@property (weak,nonatomic,readonly)id target;

+ (instancetype)proxyWithTarget:(id)target;

- (instancetype)initWithTarget:(id)target;

@end

 

WeakProxy.m

#import "WeakProxy.h"

@implementation WeakProxy

- (instancetype)initWithTarget:(id)target{

    _target = target;

    return self;

}

+ (instancetype)proxyWithTarget:(id)target{

    return [[self alloc] initWithTarget:target];

}

- (void)forwardInvocation:(NSInvocation *)invocation{

    SEL sel = [invocation selector];

    if ([self.target respondsToSelector:sel]) {

        [invocation invokeWithTarget:self.target];

    }

}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{

    return [self.target methodSignatureForSelector:aSelector];

}

- (BOOL)respondsToSelector:(SEL)aSelector{

    return [self.target respondsToSelector:aSelector];

}

@end

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值