dayjaby / zebra-scanner

Read barcodes in python with zebra barcode scanner
MIT License
24 stars 18 forks source link

Segmentation fault (core dumped) #22

Closed gnsgswgc closed 1 year ago

gnsgswgc commented 3 years ago

I have tried to install via pip3 and also tried to manual install via master branch with setup.py Please help, thank you:)

ubuntu@ubuntu:~/pip3-zebra-scanner/zebra-scanner-master/examples$ python3 test.py 
cat: write error: Broken pipe
New scanner found: <19175523700054>
{'datatype': 'F', 'id': 0, 'permission': 7, 'value': True}
Registering scanner -19175523700054-
Scanned:
765756931182 8
Segmentation fault (core dumped)

Zebra Scanner Model: DS9308-SR00004ZCWW Raspberry PI4B Ubuntu 20.04 SDK installed: SDK_for_RaspberryPi_v4.4.1-19_Debian_Packages_ARM_64bit

dayjaby commented 3 years ago

Can you verify that this PR is part of your zebra-scanner clone: https://github.com/dayjaby/zebra-scanner/pull/21

cat: write error: Broken pipe got nothing to do with that error, right?

gnsgswgc commented 3 years ago

This is the BoostPythonCoreScanner.cpp file i cloned from the repo. I think it should be the latest version.

#include "BoostPythonCoreScanner.h"
#include <stdlib.h>
#include <sstream>
#include <boost/python/make_function.hpp>

template<class a>
py::object call_python(py::object& fn, a& arg1, bool ensure_gil=true) {
        py::object ret;
        PyGILState_STATE gstate;
        if (ensure_gil) {
                gstate = PyGILState_Ensure();
        }
 PyObject* v_obj = PyObject_Repr(v);
                std::string e_str = PyUnicode_AsUTF8(e_obj);
                std::string v_str = PyUnicode_AsUTF8(v_obj);
                cout << "Error occured: " <<  e_str << "\n";
                cout << v_str << "\n";
                PyErr_Restore(e, v, t);
                throw;
        }
        if (ensure_gil) {
                PyGILState_Release(gstate);
        }
        return ret;
}
void Scanner::OnBarcodeDecorator(py::object& obj) {
        on_barcode.push_back(obj);
}

void Scanner::OnBarcode(py::object& obj) {
        for(std::vector<py::object>::iterator i=on_barcode.begin();i!=on_barcod>
            call_python(*i, obj, false);
        }
}

void Scanner::PullTrigger()
                Barcode b;
                b.code = result;
                b.type = std::stoi(scanData.child_value("datatype"));
                PyGILState_STATE state = PyGILState_Ensure();
                py::object o = py::cast(b);
                s.OnBarcode(o);
                PyGILState_Release(state);
        }
}
siadajpan commented 3 years ago

I have the same issue with Ubuntu 18.04 with SDK_for_Linux_v4.4.1-19_Debian_Packages_x86_64bit_C11. I tried to install the zebra-scanner package from the branch fix_seg_fault but the same problem appeared.

Oakman-Dev commented 3 years ago

I can confirm that this issue still exist, encountered on both Raspberry Pi 4 32-bit debian and X86 Ubuntu.

MisterErwin commented 2 years ago

I was able to reproduce this and the line causing the seg faults appears to be py::cast(b); (the seg fault occurs after exiting the scope in which py::object o has been defined tho - in this case exiting the loop).

A workaround is adding a o.inc_ref()method call (pybind11 docs), which is not recommended and to my understanding introduces a memory leak :sweat_smile: (a fork with this change added)

Sadly I don't know pybind11 enough to provide a good fix (not leaking memory), but I hope this information might help someone else in achieving this.

dayjaby commented 2 years ago

@MisterErwin interesting insight! Could it be that o gets destructed after the gil state is released? In this case we could just wrap the stuff between the gil ensure/release in some extra function/ some extra curly braces?

msw1998 commented 2 years ago

Is this issue resolved?? I the module using sudo apt-get install libboost-dev libboost-python-dev libpugixml-dev sudo pip3 install pybind11 sudo pip3 install zebra-scanner

The example code works fine for one barcode As soon as it reads the barcode it displays the barcode and the program exits with a segmentation fault error.

Is there any fix to this issue??

MikaelBox commented 2 years ago

I tried to use your zebra-scanner. test.py works only for read one barcode and closes with a segmentation fault. Can you fix it?

P.S. Debian 10, Python 3.7.3, pybind11 2.9.1, zebra-scanner 0.2.5, zebra SDK 4.4.1 x64

msw1998 commented 2 years ago

I fixed this with @MisterErwin solution by adding o.inc_ref(); line in source file. It is working so far no issues with it

MikaelBox commented 2 years ago

@msw1998 thx a lot!

to all:
in src/BoostPythonCoreScanner.cpp

void CoreScanner::OnBarcodeEvent(...)
...
PyGILState_STATE state = PyGILState_Ensure();
py::object o = py::cast(b);
o.inc_ref();  // you need to add this string into this place
s.OnBarcode(o);
...