ifm / ifm3d

Library and Utilities for working with ifm pmd-based 3D ToF Cameras
https://api.ifm3d.com
Apache License 2.0
110 stars 69 forks source link

[Known issue] RuntimeError when custom python LogWriter is dropped. #448

Open BigBoot opened 6 months ago

BigBoot commented 6 months ago

Describe the bug

If a custom python LogWriter object is set and dropped, the next logging output will lead to a RuntimeError: Tried to call pure virtual function "ifm3d::LogWriter::write" error. To ensure the logging works as expected the custom python LogWriter instance needs to be kept alive for the whole Lifetime of the program.

To Reproduce

# Define a custom LogWriter
class MyLogWriter(LogWriter):
    def write(self, entry: LogEntry) -> None:
        print("Logging from python: ", LogFormatterText.format(entry))

if __name__ == "__main__":
    # The python instance will be dropped after the set_writer method returns leading to a RuntimeError on the next logging call
    Logger.set_writer(MyLogWriter())

Workaround

Ensure the python LogWriter instance stays valid for the whole runtime of the application:

# Define a custom LogWriter
class MyLogWriter(LogWriter):
    def write(self, entry: LogEntry) -> None:
        print("Logging from python: ", LogFormatterText.format(entry))

# Create a global MyLogWriter instance
MY_LOGGER = MyLogWriter()

if __name__ == "__main__":
  # Assign the LogWriter instance
  Logger.set_writer(MY_LOGGER)