mailesn
Topic Author
Posts: 2
Joined: 08 Apr 2022, 11:13

Memory Leak With Observable List

01 Dec 2023, 10:16

I get a memory leak error on shutdown when, elements are added to an ObservableList that is set as Source for a ListBox

These are the relevant parts of the code:


//Declaration
Noesis::ObservableCollection<PlaylistFile> playlist;

//Adding of elments
playlist.Add(Noesis::MakePtr<PlaylistFile>(path.c_str(),duration));

//Element Class
class PlaylistFile : public Noesis::BaseComponent
{
    public:
        PlaylistFile();
        PlaylistFile(std::filesystem::path path);
        PlaylistFile(std::filesystem::path path, int64_t dur_s);
        Noesis::String ToString() const override;
        std::filesystem::path get_path();
        int64_t get_duration();
        
    private:
        std::filesystem::path path;
        Noesis::String name;
        int64_t duration;        
        NS_IMPLEMENT_INLINE_REFLECTION(PlaylistFile, Noesis::BaseComponent) {
            NsProp("Name", &PlaylistFile::name);
        };
};

//Destruction Call
playlist.Clear();
Noesis::GUI:Shutdown();

 
User avatar
jsantos
Site Admin
Posts: 3925
Joined: 20 Jan 2012, 17:18
Contact:

Re: Memory Leak With Observable List

01 Dec 2023, 11:17

Are you able to reproduce this in one of our examples? If so, please create a ticket and send us the patch. Thanks
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: Memory Leak With Observable List

01 Dec 2023, 20:26

What is happening is that the internal vector used to store the items by the collection has allocated memory, and as the variable is created in the stack, it didn't release the vector memory yet when Shutdown is called. If you try with a collection created in the heap it should clean everything when it gets destroyed:
//Declaration
Noesis::Ptr<Noesis::ObservableCollection<PlaylistFile>> playlist = Noesis::MakePtr<Noesis::ObservableCollection<PlaylistFile>>();

//Adding of elments
playlist->Add(Noesis::MakePtr<PlaylistFile>(path.c_str(), duration));
...
//Destruction Call
playlist.Reset();
Noesis::GUI:Shutdown();

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests