ZBar memory leak on iOS?

橙三吉。 提交于 2019-12-03 11:55:58

I found exactly the same problem with the current stable version of ZBar 0.10, and fixed it by subclassing ZBarReaderViewController and overriding the loadView method. I can now run the scanner over and over without memory going crazy.

Here's the full code:

... header

#import <Foundation/Foundation.h>
#import "ZBarReaderViewController.h"

@interface CVZBarReaderViewController : ZBarReaderViewController
@end

.. and the implementation

#import "CVZBarReaderViewController.h"

@implementation CVZBarReaderViewController 
- (void) loadView
{
    self.view = [[[UIView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)] autorelease];
}
@end

Remember to mark the .m file as being non-arc. Go to Project Settings / Target / Build Phases / Compile Sources and mark CVZBarReaderViewController.m with -fno-objc-arc.

Actually here is the code for the patch

diff --git a/iphone/ZBarReaderViewController.m b/iphone/ZBarReaderViewController.m
--- a/iphone/ZBarReaderViewController.m
+++ b/iphone/ZBarReaderViewController.m
@@ -320,8 +320,8 @@

 - (void) loadView
 {
-    self.view = [[UIView alloc]
-                    initWithFrame: CGRectMake(0, 0, 320, 480)];
+    self.view = [[[UIView alloc]
+                  initWithFrame: CGRectMake(0, 0, 320, 480)] autorelease];
 }

 - (void) viewDidLoad

it's a .diff file, if you had access to the repo you could easily apply the patch but since this is a short one, you can also do that manually. You just have to add an autorelease call in loadView method of ZBarReaderViewController class

I found temporary (I hope it is temporary) workaround. Instead of using ZBarReaderViewController I am using ZBarReaderView directly and also another delegate to handle the results.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!