zxingify / zxingify-objc

An Objective-C Port of ZXing
Apache License 2.0
3.06k stars 752 forks source link

Crash during startup #6

Closed AnoxySoftware closed 11 years ago

AnoxySoftware commented 11 years ago

Hi, I'm trying to add the library into my project.

I was trying to test the library with the sample code, but as soon as the [self.capture start]; is called, I receive a crash of EXC_BAD_ACCESS that backtraces to this:

Does this have anything to do with my project using ARC ?

Ok, further investigation points to the crash reason being the Autorelease pool drain call. Any hints on why ?

jsdasse commented 11 years ago

I had a similar problem this morning, an object being released too many times. In ZXCGImageLuminanceSource.m line 99, try to remove the call to the autorelease method

NSMakeCollectable(result);

instead of [NSMakeCollectable(result) autorelease];

I don't think that a method named "createImageFromBuffer:left:top:width:height:" should returned an autoreleased object.

in ZXCapture.m line 499, the object returned by this method is release one more time : CGImageRelease(videoFrameImage); and I think it's better to keep the release there and remove the one in ZXCGImageLuminanceSource.m

AnoxySoftware commented 11 years ago

Also even the BarcodeScanner sample will not work. As it is, it will crash as soon as it opens on the phone...

renetik commented 11 years ago

I tried jsdasse change and it do not crash , but scanner do not appear to work, testing barcode scanner example...

jsdasse commented 11 years ago

According to the XCode static analyzer, it seems that it is a better idea to remove the release from ZXCapture.m and keep the autorelease in ZXCGImageLuminanceSource.m

createImageFromBuffer:left:width:height: should return an autoreleased object as it's name does not begin with new, alloc, copy, mutableCopy

anyway, after any of those modifications, the BarcodeScanner sample does work with QR Codes or Codes 39 (XCode 4.3.3 with an iPhone 4 iOS 4.0 or Xcode 4.5 with an iPhone 4 iOS 6.0 GM)

renetik commented 11 years ago

I tested bar code EAN-13 scanning and it crashed after memory warning … What should I do to fix it ? It worked for some time...

jsdasse commented 11 years ago

I have just downloaded the whole zip file containing the sources again, open the BarcodeScanner.xcodeproj and applied the following modifications : in ZXCapture.m

and I am able to scan the following code : EAN 13

but if you are not quick enough, you could have a memory leak serious enough to crash the app. In the Xcode output window, you should see Received memory warning. Level=1 Received memory warning. Level=2 and then the OS just kill the app

to fix that, in ZXHybridBinarizer.m, add the following code on line 79 :

free(_luminances);
renetik commented 11 years ago

I will try it tomorrow , many thanx . What you use to find such solutions , I am dev. for ios just shortly , everything I use is ARC , and I have no memory problems even in larger applications.

Thanks again .

On Mon, Sep 17, 2012 at 5:55 PM, jsdasse notifications@github.com wrote:

I have just downloaded the whole zip file containing the sources again, open the BarcodeScanner.xcodeproj and applied the following modifications : in ZXCapture.m

  • comment line 499 : // CGImageRelease(videoFrameImage);
  • comment line 506 : // CGImageRelease(rotatedImage);
  • Modify line 107 : [NSMakeCollectable(rotatedImage) autorelease];

and I am able to scan the following code : EAN 13http://www.google.fr/imgres?q=EAN-13&um=1&hl=en&client=safari&sa=N&rls=en&biw=1890&bih=1320&tbm=isch&tbnid=JMvFVNRzWlBuvM:&imgrefurl=http://en.wikipedia.org/wiki/International_Article_Number_(EAN)&docid=ZM6Rbp3D_PKVkM&imgurl=http://upload.wikimedia.org/wikipedia/commons/thumb/a/a2/EAN-13-5901234123457.svg/220px-EAN-13-5901234123457.svg.png&w=220&h=160&ei=-0NXUPvOAoqRhQeF1IDwAw&zoom=1&iact=hc&vpx=234&vpy=200&dur=24&hovh=128&hovw=176&tx=49&ty=44&sig=108394685424203175328&page=1&tbnh=128&tbnw=176&start=0&ndsp=59&ved=1t:429,r:0,s:0,i:73

but if you are not quick enough, you could have a memory leak serious enough to crash the app. In the Xcode output window, you should see Received memory warning. Level=1 Received memory warning. Level=2 and then the OS just kill the app

to fix that, in ZXHybridBinarizer.m, add the following code on line 79 :

free(_luminances);

— Reply to this email directly or view it on GitHubhttps://github.com/TheLevelUp/ZXingObjC/issues/6#issuecomment-8619734.

Best Regards / S Pozdravom *Rene Dohan

*http://creative-software.appspot.com/ http://renkosoft.appspot.com/ http://www.qualityunit.com/

Gamas27 commented 11 years ago

Managed to run it with jsdasse corrections. Still after a while there is a memory leak. would be nice to use this in a reliable way soon.

renetik commented 11 years ago

Hi , I didi exactly what you discribed , and I got :

2012-09-18 18:54:13.057 RegiofoodIOS[792:307] Received memory warning. Level=1 2012-09-18 18:54:49.892 RegiofoodIOS[792:307] Received memory warning. Level=2 2012-09-18 18:55:07.866 RegiofoodIOS[792:307] Received memory warning. Level=1 2012-09-18 18:55:34.515 RegiofoodIOS[792:307] Received memory warning. Level=1 2012-09-18 18:55:35.356 RegiofoodIOS[792:307] Received memory warning. Level=1 2012-09-18 18:55:46.916 RegiofoodIOS[792:307] Received memory warning. Level=2

I am testing on Iphone3g so it has lower memory but there is surely leak. It takse som two minutes to crash.

I am using this code from example:

self.capture = [[ZXCapture alloc] init]; self.capture.delegate = self; self.capture.rotation = 90.0f;

// Use the back camera
self.capture.camera = self.capture.back;

self.capture.layer.frame = self.view.bounds;
[self.view.layer addSublayer:self.capture.layer];

Do I have some possibilities to fix this ? I have very minimal memory handling experiences in IOS. Experienced in GC languages.

jsdasse commented 11 years ago

It seems that the image decoding in captureOutput:didOutputSampleBuffer:fromConnection: (ZXCapture.m) is not fast enough to process all the images. Images that can't be processed are just queued in the queue specified in setSampleBufferDelegate:queue: for the AVCaptureVideoDataOutput object.

Setting alwaysDiscardsLateVideoFrames to YES (it's already set in the original code) for this output object should get rid of all those images if one is already processed, but by monitoring the free memory I can see that it keeps decreasing. What's weird is that according to Instrument or by retrieving a vm_statistics_data_t object, the memory usage does not grow ! I guess the allocations in a queue are not well reported because I still get the memory warnings before the app is just killed.

I tried to limit the number of frames actually captured per second by setting output.minFrameDuration = CMTimeMake(1, MAX_NUMBER_OF_FRAMES_PER_SECOND); it works but with an iPhone 4 (in release), I have to set MAX_NUMBER_OF_FRAMES_PER_SECOND to 2 to keep the memory stable. But this settings also affect the preview on the screen and with 2 fps, it does not look good !

I haven't try to reduce the number of decoders (only the QRCode reader for example) , but it should help.

Other than that, I haven't found anything, Instrument does not report any leak or any increase of the data allocated in the heap ! Monitoring the memory in Xcode gives the same result, the size of the data in the heap stays almost constant but the free memory keeps decreasing.

tomorrow, I will try to start a new video capture project from scratch to check if I have the same kind of results...

renetik commented 11 years ago

Thank you for your effort btw. I did try ti use video streaming directly and even then when I used library just for decoding it was similar. But maybe I had memory leak elsewhere ,I just used simple modified examples that worked for others. When I commented decoding no memory problem and with decoding memory warnings and crash , so for me that leek seem to be in decoding part of code.

jsdasse commented 11 years ago

Ok this time, it should work ! The BarcodeScanner at least. A lot of my debug issues were caused by the option "Enable Zombie Objects" that I have enabled in the BarcodeScanner scheme. It did not help as it should... keep it disabled !

to make it work (from the version downloaded on september 20th 2012) :

In ZXingObjC/client/ZXCapture.m

In ZXingObjC/client/ZXCGImageLuminanceSource.m

In ZXingObjC/common/ZXHybridBinarizer.m :

the sample has been running for more that 1 hour now and the memory is still stable ! (iPhone 4) hope it helps, keep me informed if you still have some issues

JSD

renetik commented 11 years ago

I can confirm that memory problem is resolved now, but anyway I am not successfull with this library in scanning of some ean-13 codes where android zxing scanner is working perfectly. I am looking into its code to find differences in image handling and I see just some implementation of PlanarYUVLuminanceSource tha seems to do simmilar as ZXCGImageLuminanceSource , other what android is doing is some focusing stuf and some cropping, but even if I try hard form different distances I cannot scan some ean-13 code. So I dont know what to try now, partly functional scanner is unusable. Any ideas ? Thank you.

cwalcott commented 11 years ago

There were definitely some memory leaks affecting the BarcodeScanner app. These should now be fixed: watching the memory usage in Instruments, it no longer grows out of control.

ttdat commented 11 years ago

It seems that memory leaks still exist in BarcodeScanner app https://f.cloud.github.com/assets/3184182/42757/841301e4-564d-11e2-8c63-4d5e4b662258.jpg

https://f.cloud.github.com/assets/3184182/42758/91f52e04-564d-11e2-8bf0-4d33485f2b8f.jpg

mircaea commented 11 years ago

I have implemented the ZXingObjC library and am using it on an app to scan code 39 labels. I am using a file such as ScanViewController.mm to run the scans. The project uses a navigation bar.

Issue: Running [self.navigationController popViewControllerAnimated:NO] crashes the app with the same thing - EXC_BAD_ACCESS .

If you do get past that, going back to the Scanning controller throws: -[__NSCFType _removeFromEngine:]: unrecognized selector sent to instance 0x1f5e3130 *\ Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType _removeFromEngine:]: unrecognized selector sent to instance 0x1f5e3130'

I suppose it is related to arc optimization not working as supposed to but have no clue how to get past this.

gadregit commented 9 years ago

Hi Guys, Recently i integration ZXingObjC 3.0.3 alongwith cocoapods. And all is working fine for me. But i am not able to find How do I restrict ZXCapture to scan a single barcode multiple times? Sometimes it just scans 4-5 times in fraction of second. Thanks.

cwalcott commented 9 years ago

https://github.com/TheLevelUp/ZXingObjC/blob/master/examples/BarcodeScanner/ViewController.m#L135-L139 shows one way you can do this. You can call stop on ZXCapture when a scan succeeds, and then start again later.