]> git.sesse.net Git - bmusb/commitdiff
Stop leaking memory allocators.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 2 May 2016 23:58:02 +0000 (01:58 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 2 May 2016 23:58:44 +0000 (01:58 +0200)
bmusb.cpp
bmusb.h

index 48bdcae3c41516815cb31a9d1cafb9a015d28f1c..3aac81e5e34e5de7459585630635eab8a460d25a 100644 (file)
--- a/bmusb.cpp
+++ b/bmusb.cpp
@@ -782,10 +782,12 @@ libusb_device_handle *open_card(int card_index, string *description)
 void BMUSBCapture::configure_card()
 {
        if (video_frame_allocator == nullptr) {
 void BMUSBCapture::configure_card()
 {
        if (video_frame_allocator == nullptr) {
-               set_video_frame_allocator(new MallocFrameAllocator(FRAME_SIZE, NUM_QUEUED_VIDEO_FRAMES));  // FIXME: leak.
+               owned_video_frame_allocator.reset(new MallocFrameAllocator(FRAME_SIZE, NUM_QUEUED_VIDEO_FRAMES));
+               set_video_frame_allocator(owned_video_frame_allocator.get());
        }
        if (audio_frame_allocator == nullptr) {
        }
        if (audio_frame_allocator == nullptr) {
-               set_audio_frame_allocator(new MallocFrameAllocator(65536, NUM_QUEUED_AUDIO_FRAMES));  // FIXME: leak.
+               owned_audio_frame_allocator.reset(new MallocFrameAllocator(65536, NUM_QUEUED_AUDIO_FRAMES));
+               set_audio_frame_allocator(owned_audio_frame_allocator.get());
        }
        dequeue_thread_should_quit = false;
        dequeue_thread = thread(&BMUSBCapture::dequeue_thread_func, this);
        }
        dequeue_thread_should_quit = false;
        dequeue_thread = thread(&BMUSBCapture::dequeue_thread_func, this);
diff --git a/bmusb.h b/bmusb.h
index 9ee6617a6e7874164a8a53603a47d974acf01694..4c7254bcb5dea6bb773381c510565cea37035839 100644 (file)
--- a/bmusb.h
+++ b/bmusb.h
@@ -177,6 +177,9 @@ class BMUSBCapture : public CaptureInterface {
        void set_video_frame_allocator(FrameAllocator *allocator) override
        {
                video_frame_allocator = allocator;
        void set_video_frame_allocator(FrameAllocator *allocator) override
        {
                video_frame_allocator = allocator;
+               if (owned_video_frame_allocator.get() != allocator) {
+                       owned_video_frame_allocator.reset();
+               }
        }
 
        FrameAllocator *get_video_frame_allocator() override
        }
 
        FrameAllocator *get_video_frame_allocator() override
@@ -188,6 +191,9 @@ class BMUSBCapture : public CaptureInterface {
        void set_audio_frame_allocator(FrameAllocator *allocator) override
        {
                audio_frame_allocator = allocator;
        void set_audio_frame_allocator(FrameAllocator *allocator) override
        {
                audio_frame_allocator = allocator;
+               if (owned_audio_frame_allocator.get() != allocator) {
+                       owned_audio_frame_allocator.reset();
+               }
        }
 
        FrameAllocator *get_audio_frame_allocator() override
        }
 
        FrameAllocator *get_audio_frame_allocator() override
@@ -251,6 +257,8 @@ class BMUSBCapture : public CaptureInterface {
 
        FrameAllocator *video_frame_allocator = nullptr;
        FrameAllocator *audio_frame_allocator = nullptr;
 
        FrameAllocator *video_frame_allocator = nullptr;
        FrameAllocator *audio_frame_allocator = nullptr;
+       std::unique_ptr<FrameAllocator> owned_video_frame_allocator;
+       std::unique_ptr<FrameAllocator> owned_audio_frame_allocator;
        frame_callback_t frame_callback = nullptr;
 
        std::thread dequeue_thread;
        frame_callback_t frame_callback = nullptr;
 
        std::thread dequeue_thread;