ZBar memory leak on iOS?
Asked Answered
K

3

10

I am very satisfied with ZBar scanning performance, however I ran into a big problem on a project that runs under ARC if that counts at all.

Namely, it seems that there is a serious memory leak that rises exponentially with each and every new display of the readerView. after some 10x memory usage starts to rise exponentially and around 20x scan program become unusable.

I have seen example that are build with same version and there is no problem with it. I also tried method flushCache on readerView, but it doesn't help.

THere is one patch: http://sourceforge.net/p/zbar/patches/36/, but I don't see any code for it. Since I don't have full source code I cannot find it …

Has anybody had same problem?

Karb answered 5/9, 2013 at 13:56 Comment(0)
W
28

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.

Wallin answered 20/9, 2013 at 8:49 Comment(3)
By far the simplest solution. Didn't have to rebuild the library myself. This helped me get an important build out by the deadline. Thank you!Kahaleel
How do we need to launch the viewer? Still using ZBarReaderViewController *reader = [[[ZBarReaderViewController alloc] init] autorelease]; or using CVZBarReaderViewController?Claytor
Use CVZBarViewController. :-)Wallin
A
7

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

Agna answered 5/9, 2013 at 14:1 Comment(2)
Thanks for help, but this is the problem - I have no Objective C source code except for .h header files, so I can't apply this patch. All I could get on developer's page was ZBarSDK filed that contain header files and static library.Karb
I see :( Then how are you supposed to make a workaround for this?Agna
K
1

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.

Karb answered 6/9, 2013 at 7:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.