[Bug]: LV2 host leaks the scale point collection of every port (lilv_port_get_scale_points never freed)

Open Beginner friendly
#1,790 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp

Research direction

Start in juce_LV2PluginFormatImpl.h at ScalePoints and Port::getScalePoints(), then trace how ParameterInfo::getScalePoints() reads the collection. The issue includes a LeakSanitizer reproduction using an LV2 plug-in with scale points; run it with leak detection enabled and confirm the collection is freed after port information is read.

Written by the indexing model from the issue text.

Description

Detailed steps on how to reproduce the bug

lv2_host::Port::getScalePoints() wraps the result of lilv_port_get_scale_points() in lv2_host::ScalePoints, which stores it as a raw const LilvScalePoints* and never frees it. lilv documents that the returned collection "must be freed by caller with lilv_scale_points_free()" (lilv.h, lilv_port_get_scale_points).

ParameterInfo::getScalePoints() calls this for every port while a plug-in instance is created (Ports::getPorts → ParameterInfo::getInfoForPort), so each instance of a plug-in with enumerated ports leaks one collection per such port.

Repro: build a host with AddressSanitizer (leak detection on), instantiate an LV2 plug-in that has ports with scale points (we see it with sfizz.lv2), quit. LeakSanitizer reports e.g. "1913 byte(s) leaked in 72 allocation(s)" with this path:

#1 zix_tree_new lilv/src/zix/tree.c:74
#2 lilv_collection_new lilv/src/collections.c:52
#3 lilv_scale_points_new lilv/src/collections.c:88
#4 lilv_port_get_scale_points lilv/src/port.c:242
#5 juce::lv2_host::Port::getScalePoints() const juce_LV2PluginFormatImpl.h:526
#6 juce::lv2_host::ParameterInfo::getScalePoints(juce::lv2_host::Port const&) juce_LV2PluginFormatImpl.h:1593
#7 juce::lv2_host::ParameterInfo::getInfoForPort(...) juce_LV2PluginFormatImpl.h:1610
#8 juce::lv2_host::Ports::getPorts(...) juce_LV2PluginFormatImpl.h:1951
#9 juce::LV2PluginFormatHeadless::Pimpl::createPluginInstance<...>(...) juce_LV2PluginFormatImpl.h:4466

How we use it: our app (a looper built on a Giada fork) hosts sfizz and DrumGizmo through JUCE's LV2 host and runs its app tests under ASan; this leak was the only one left in our code paths. JUCE 9.0.4; the same code is on develop today.

The fix we carry: let ScalePoints own the collection.

@@ -413,19 +413,25 @@ using NonOwningNodes = NodesImpl<NonOwningPtrTraits<LilvNodes>>;
 class ScalePoints
 {
 public:
-    explicit ScalePoints (const LilvScalePoints* pt)
+    // lilv_port_get_scale_points returns a collection the caller must free.
+    explicit ScalePoints (LilvScalePoints* pt)
         : points (pt) {}
 
     ScalePointsIterator begin() const noexcept
     {
         return points == nullptr ? ScalePointsIterator{}
-                                 : ScalePointsIterator { points };
+                                 : ScalePointsIterator { points.get() };
     }
 
     ScalePointsIterator end() const noexcept { return {}; }
 
 private:
-    const LilvScalePoints* points = nullptr;
+    struct Free
+    {
+        void operator() (LilvScalePoints* ptr) const noexcept { lilv_scale_points_free (ptr); }
+    };
+
+    std::unique_ptr<LilvScalePoints, Free> points;
 };
 
 class ScalePoint
What is the expected behaviour?

The scale point collection is freed after the port's info has been read (no LeakSanitizer report).

Operating systems

Linux

What versions of the operating systems?

Arch Linux (Omarchy), kernel 7.2, GCC 16

Architectures

x86_64

Plug-in formats (if applicable)

LV2

Testing on the develop branch

The bug is present on the develop branch (same ScalePoints class, checked today).

Dominant language
C++
Stars
8.9k
Forks
2.2k
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from juce-framework/JUCE

All issues in juce-framework/JUCE

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.