Derived classes storing Base class records, with shared_ptr / forward declarations / shared_from_this()

Seem to have gotten myself into quite a mess with pointers and memory allocation on a UI addon I’m creating . What I’ve been attempting to do is;

  1. All elements are subclasses of a single base class
  2. Each element stores a reference to the original base class object
  3. Base class object has a vector or map of all the derived class objects

Instead II think ’m creating new memory at an exponential rate - ie. with each subclass being added it’s multiplied , so a single dropdown or button in a sketch is ~20mb, and a full UI is ~500mb.

Here is a simplified example of the pattern for adding objects … I’m in too deep so that it’s hard to experiment with shared_ptrs without changing around 200 lines of code each time!

My guess is that the initial FluidUi needs to set master with a shared_from_this?

#pragma once
#include "ofMain.h"

class FluidGroup;
class FluidElement;

class FluidUi {
public:
    
    FluidUi * master; // This gets passed down to children
    vector<FluidUi *> all; // Store of all elements added
    string name;
    
    FluidUi();
    FluidGroup & addGroup(string name = "FluidGroup");
    FluidElement & addElement(string name = "FluidElement");
};

class FluidGroup : public FluidUi {
public:
    FluidUi * master;
    string name;
    FluidGroup(FluidUi * input, string n) {
        master = input;
        name = n;
    }
};

class FluidElement : public FluidUi {
public:
    FluidUi * master;
    string name;
    FluidElement(FluidUi * input, string n) {
        master = input;
        name = n;
    }
};

class ofApp : public ofBaseApp{

public:
    void setup();
    void update();
    void draw();

    
    FluidUi * ui;
    FluidGroup * group1, * group2, * group3;
    FluidElement * element1, * element2, * element3;
		
};




//--------------------------------------------------------------
void ofApp::setup(){

    ui = new FluidUi();
    group1 = &ui->addGroup();
    group2 = &ui->addGroup();
    group3 = &ui->addGroup();
    element1 = &group1->addElement();
    element2 = &group2->addElement();
    element3 = &group3->addElement();
    
    ofLog() << ui->name << " " << ui->master->name << " Should be 6: " << ui->master->all.size();
    ofLog() << group1->name << " " << group1->master->name << " hould be 6: " << group1->master->all.size();
    ofLog() << element1->name << " " << element1->master->name << " Should be 6: " << element1->master->all.size();
    
}

//--------------------------------------------------------------
void ofApp::update(){

}

//--------------------------------------------------------------
void ofApp::draw(){

}

Lol, I think I’ve solved what the problem was…

The Base constructor was being called on Derived constructors, so the debugger called FluidRow constructor, then immediately the FluidUi constructor again which contained memory heavy definitions.

Strange behaviour because discussion online is people asking how to call a Base constructor from a Derived constructor, rather than the other way round!

Anyway, memory down to 30mb / 20% CPU for an entire UI :slight_smile: