gpt4 book ai didi

ios - UIWebView 中的内存泄漏

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:41:18 24 4
gpt4 key购买 nike

我这两天头(屁)痛。狡猾的内存泄漏使我成为绿巨人。在模拟器上分析代码时,在自定义 UIWebView 中加载任何 url 请求后会出现大量泄漏。但是,如果我使用 iPhone 5 之类的设备,则只有一个泄漏。最大的问题是 Instruments 不显示任何行 my 代码。我记得我看过一个很棒的视频教程来定位这些泄漏,但是谷歌搜索了两天没有结果:(这是一个简单的 project对于 Xcode 5,它会在 webView 属性加载 url 请求时泄漏。

截图:one , two .

UPD:添加了整个代码。UPD2:微小的重构。

#import "AKViewController.h"

@interface AKViewController ()

@property (nonatomic, strong, readonly) UIWebView *webView;

@end

@implementation AKViewController

@synthesize webView = _webView;

#define MARGIN_WEB_VIEW_X 15.0f
#define MARGIN_WEB_VIEW_TOP 30.0f
#define MARGIN_WEB_VIEW_BOTTOM 25.0f

#pragma mark - Private methods

- (CGRect)makeRectForWebView {
CGRect appFrame = UIScreen.mainScreen.applicationFrame;
CGRect rectWebView = CGRectMake(MARGIN_WEB_VIEW_X,
MARGIN_WEB_VIEW_TOP,
appFrame.size.width - MARGIN_WEB_VIEW_X * 2,
appFrame.size.height - MARGIN_WEB_VIEW_BOTTOM);

return rectWebView;
}
- (void)presentViews {
[self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

self.webView.frame = [self makeRectForWebView];
[self.view addSubview:self.webView];
}
- (NSURLRequest *)makeLoginURLRequest {
NSString *stringUrl = @"http://google.com/";
NSURL *url = [NSURL URLWithString:[stringUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

return request;
}

#pragma mark - Properties

- (UIWebView *)webView {
if (!_webView) {
_webView = [[UIWebView alloc] initWithFrame:UIScreen.mainScreen.applicationFrame];
_webView.scalesPageToFit = YES;
}

return _webView;
}

#pragma mark - Lifecycle

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

[self presentViews];
}

- (void)viewDidLoad {
[super viewDidLoad];

self.view.backgroundColor = [UIColor grayColor];
[self.webView loadRequest:[self makeLoginURLRequest]];
}

@end

帮助!

最佳答案

我不知道您是否显示了所有代码,所以我猜测您可能不会释放 Web View 的委托(delegate)。 UIWebView delegate 属性的文档说:

Important: Before releasing an instance of UIWebView for which you have set a delegate, you must first set its delegate property to nil. This can be done, for example, in your dealloc method.

关于ios - UIWebView 中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19315332/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com