From f6a7bdf156c67f00998395c4e0a32f14edd2f9fa Mon Sep 17 00:00:00 2001 From: Thiemo Wiedemeyer Date: Thu, 9 Jul 2015 09:45:49 +0200 Subject: [PATCH 1/2] fixed memory leak due to unknown state of packet pipeline pointer. --- examples/protonect/src/libfreenect2.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/protonect/src/libfreenect2.cpp b/examples/protonect/src/libfreenect2.cpp index 9524fd6ca..e34da1b1b 100644 --- a/examples/protonect/src/libfreenect2.cpp +++ b/examples/protonect/src/libfreenect2.cpp @@ -678,6 +678,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline, if(idx >= num_devices) { std::cout << "[Freenect2Impl] requested device " << idx << " is not connected!" << std::endl; + delete pipeline; return device; } @@ -688,6 +689,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline, { std::cout << "[Freenect2Impl] failed to get device " << PrintBusAndDevice(dev.dev) << " (the device may already be open)" << std::endl; + delete pipeline; return device; } @@ -696,6 +698,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline, if(r != LIBUSB_SUCCESS) { std::cout << "[Freenect2Impl] failed to open Kinect v2 " << PrintBusAndDevice(dev.dev) << "!" << std::endl; + delete pipeline; return device; } @@ -733,6 +736,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline, else if(r != LIBUSB_SUCCESS) { std::cout << "[Freenect2Impl] failed to reset Kinect v2 " << PrintBusAndDevice(dev.dev) << "!" << std::endl; + delete pipeline; return device; } } @@ -765,11 +769,11 @@ Freenect2Device *Freenect2::openDevice(const std::string &serial, const PacketPi { if(impl_->enumerated_devices_[idx].serial == serial) { - device = openDevice(idx, pipeline); - break; + return openDevice(idx, pipeline); } } + delete pipeline; return device; } From 8924deb78000384549e8a4c0d58583cfe9b3f2b5 Mon Sep 17 00:00:00 2001 From: Thiemo Wiedemeyer Date: Thu, 9 Jul 2015 11:12:45 +0200 Subject: [PATCH 2/2] added note to header file. --- examples/protonect/include/libfreenect2/libfreenect2.hpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/protonect/include/libfreenect2/libfreenect2.hpp b/examples/protonect/include/libfreenect2/libfreenect2.hpp index d4c842f28..38f36728a 100644 --- a/examples/protonect/include/libfreenect2/libfreenect2.hpp +++ b/examples/protonect/include/libfreenect2/libfreenect2.hpp @@ -106,6 +106,11 @@ class LIBFREENECT2_API Freenect2 std::string getDeviceSerialNumber(int idx); std::string getDefaultDeviceSerialNumber(); + /* Important note: + * After passing a PacketPipeline object to libfreenect2 do not use or free the object, + * libfreenect2 will take care. If openDevice fails the PacketPipeline object will get + * deleted. A new PacketPipeline object has to be created each time a device is opened. + */ Freenect2Device *openDevice(int idx); Freenect2Device *openDevice(int idx, const PacketPipeline *factory); Freenect2Device *openDevice(const std::string &serial);