Skip to content

Fix potential memory leak in KAboutData::registerPluginData

Milian Wolff requested to merge work/fix-leak-KAboutData_registerPluginData into master

When this function is called multiple times for the same component, the KAboutData would be leaked. This can be observed in KDevelop for example, where I found this originally. The new test added in this patch also triggers the leak:

==145593==ERROR: LeakSanitizer: detected memory leaks

Indirect leak of 184 byte(s) in 1 object(s) allocated from:
    #0 0x7fe4e3316f41 in operator new(unsigned long) /build/gcc/src/gcc/libsanitizer/asan/asan_new_delete.cpp:99
    #1 0x7fe4e23fb40c in KAboutData::KAboutData(KAboutData const&) /home/milian/projects/kf5/src/frameworks/kcoreaddons/src/lib/kaboutdata.cpp:573
    #2 0x7fe4e2402c6d in KAboutData::registerPluginData(KAboutData const&) /home/milian/projects/kf5/src/frameworks/kcoreaddons/src/lib/kaboutdata.cpp:1136

Instead of using pointers, we now use KAboutData values and just overwrite them in the registry. As QHash is a node-based container, we can just hand out pointers to the entries and they'll stay stable even when we insert new entries later on.

Generally though, this API should probably not use pointers in the first place but instead use something like std::optional.

Merge request reports