Skip to content

Commit

Permalink
Resolved invalid pointer issue by converting vectors from raw Object …
Browse files Browse the repository at this point in the history
…to unique_ptr<Object>.
  • Loading branch information
sigmareaver committed Oct 2, 2022
1 parent a6448e5 commit ac16cd8
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 28 deletions.
34 changes: 23 additions & 11 deletions src/sources/ims_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void ImStudio::BufferWindow::drawall(int *select)
{
for (auto i = objects.begin(); i != objects.end(); ++i)
{
Object &o = *i;
Object &o = **i;

if (o.state == false)
{
Expand Down Expand Up @@ -53,8 +53,9 @@ void ImStudio::BufferWindow::drawall(int *select)

ImStudio::Object *ImStudio::BufferWindow::getobj(int id)
{
for (Object &o : objects)
for (auto &ptr : objects)
{
auto& o = *ptr;
if (o.id == id)
{
return &o;
Expand All @@ -65,16 +66,18 @@ ImStudio::Object *ImStudio::BufferWindow::getobj(int id)

ImStudio::BaseObject *ImStudio::BufferWindow::getbaseobj(int id)
{
for (Object &o : objects)
for (auto &ptr : objects)
{
auto& o = *ptr;
if (o.id == id)
{
return &o;
}
if (!o.child.objects.empty())
{
for (BaseObject &cw : o.child.objects)
for (auto &cptr : o.child.objects)
{
auto& cw = *cptr;
if (cw.id == id)
{
return &cw;
Expand All @@ -90,21 +93,30 @@ void ImStudio::BufferWindow::create(std::string type_)
idvar++;
if (!current_child)
{
Object widget(idvar, type_);
objects.push_back(widget);
//Object widget(idvar, type_);
//objects.push_back(widget);
//objects.emplace_back(std::move(Object(idvar, type_)));
auto widget = std::make_unique<Object>(idvar, type_);
objects.push_back(std::move(widget));
}
else
{
if (!current_child->child.open)
{
Object widget(idvar, type_);
objects.push_back(widget);
//Object widget(idvar, type_);
//objects.push_back(widget);
//objects.emplace_back(std::move(Object(idvar, type_)));
auto widget = std::make_unique<Object>(idvar, type_);
objects.push_back(std::move(widget));
}
else
{
BaseObject childwidget(idvar, type_, current_child->id);
childwidget.parent = current_child;
current_child->child.objects.push_back(childwidget);
//BaseObject childwidget(idvar, type_, current_child->id);
//childwidget.parent = current_child;
//current_child->child.objects.push_back(childwidget);
auto childwidget = std::make_unique<BaseObject>(idvar, type_, current_child->id);
(*childwidget).parent = current_child;
current_child->child.objects.push_back(std::move(childwidget));
}
}
}
3 changes: 2 additions & 1 deletion src/sources/ims_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <string>
#include <vector>
#include <memory>

#include "imgui_docking/imgui.h"

Expand All @@ -21,7 +22,7 @@ namespace ImStudio

bool staticlayout = false; //

std::vector<Object> objects = {}; //
std::vector<std::unique_ptr<Object>> objects = {}; //

void drawall (int *select);
Object * getobj (int id);
Expand Down
4 changes: 2 additions & 2 deletions src/sources/ims_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ void ImStudio::GenerateCode(std::string* output, BufferWindow* bw)
*output += "if (ImGui::Begin(\"window_name\", &window))\n{\n\n";
for (auto i = bw->objects.begin(); i != bw->objects.end(); ++i)
{
Object &o = *i;
Object &o = **i;

if (o.type != "child")
{
Expand All @@ -368,7 +368,7 @@ void ImStudio::GenerateCode(std::string* output, BufferWindow* bw)
*output += fmt::format("\tImGui::BeginChild({}, ImVec2({},{}), {});\n\n", o.child.id, o.child.freerect.GetSize().x, o.child.freerect.GetSize().y, o.child.border);
for (auto i = o.child.objects.begin(); i != o.child.objects.end(); ++i)
{
BaseObject &cw = *i;// child widget
BaseObject &cw = **i;// child widget

Recreate(cw, output, bw->staticlayout);

Expand Down
4 changes: 2 additions & 2 deletions src/sources/ims_generator_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ void ImStudio::GenerateCodeLua(std::string* output, BufferWindow* bw)
*output += "if (window = ImGui.Begin(\"window_name\", window)) then\n\n";
for (auto i = bw->objects.begin(); i != bw->objects.end(); ++i)
{
Object& o = *i;
Object& o = **i;

if (o.type != "child")
{
Expand All @@ -389,7 +389,7 @@ void ImStudio::GenerateCodeLua(std::string* output, BufferWindow* bw)
*output += fmt::format("\tImGui.BeginChild({}, {}, {}, {})\n\n", o.child.id, o.child.freerect.GetSize().x, o.child.freerect.GetSize().y, o.child.border);
for (auto i = o.child.objects.begin(); i != o.child.objects.end(); ++i)
{
BaseObject& cw = *i;// child widget
BaseObject& cw = **i;// child widget

RecreateLua(cw, output, bw->staticlayout);

Expand Down
15 changes: 10 additions & 5 deletions src/sources/ims_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,13 +401,15 @@ void ImStudio::GUI::ShowProperties()
{
//SECTION CREATE PROPARRAY
allvecsize = 0;
for (Object &o : bw.objects) // Calc total objects created in all vectors [bw.objects+all(child.objects)]
for (auto &ptr : bw.objects) // Calc total objects created in all vectors [bw.objects+all(child.objects)]
{
auto& o = *ptr;
allvecsize++;
if (!o.child.objects.empty())
{
for (BaseObject &cw : o.child.objects)
for (auto &cptr : o.child.objects)
{
auto& cw = *cptr;
(void)cw;//for compiler warning
allvecsize++;
}
Expand All @@ -417,8 +419,9 @@ void ImStudio::GUI::ShowProperties()
std::vector<const char*> items; // contains identifiers ex: child1::button2
std::vector<int> idarr; // contains id associated with ^
int i = 0;
for (Object &o : bw.objects) // Fill both arrays with contents from bw.objects [Object]
for (auto &ptr : bw.objects) // Fill both arrays with contents from bw.objects [Object]
{
auto& o = *ptr;
char* identifier = const_cast<char *>(o.identifier.c_str());
items.push_back(identifier);
idarr.push_back(o.id);
Expand All @@ -439,13 +442,15 @@ void ImStudio::GUI::ShowProperties()
}
i++;
}
for (Object &o : bw.objects) // cpy-grp
for (auto &ptr : bw.objects) // cpy-grp
{
auto& o = *ptr;
if (!o.child.objects.empty())
{
for (BaseObject &cw :
for (auto &cptr :
o.child.objects) // Fill both arrays with contents from child.objects [BaseObject]
{
auto& cw = *cptr;
char* identifier = const_cast<char *>(cw.identifier.c_str());
items.push_back(identifier);
idarr.push_back(cw.id);
Expand Down
3 changes: 1 addition & 2 deletions src/sources/ims_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ ImStudio::BaseObject::BaseObject(int idvar_, std::string type_, int parent_id_)
type = type_;
identifier = "child" + std::to_string(parent_id_) + "::" + type_ + std::to_string(idvar_);
value_s = type_ + std::to_string(idvar_);
parent = nullptr;
}

ImStudio::Object::Object(int idvar_, std::string type_) : BaseObject()
Expand Down Expand Up @@ -730,7 +729,7 @@ void ImStudio::ContainerChild::drawall(int *select, bool staticlayout)
ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoBringToFrontOnFocus);
for (auto i = objects.begin(); i != objects.end(); ++i)
{
BaseObject &o = *i;
BaseObject &o = **i;
if (o.state == false)
{
i = objects.erase(i);
Expand Down
3 changes: 2 additions & 1 deletion src/sources/ims_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cstdlib>
#include <string>
#include <vector>
#include <memory>

#include "imgui_docking/imgui.h"
#include "imgui_docking/imgui_stdlib.h"
Expand Down Expand Up @@ -76,7 +77,7 @@ namespace ImStudio
ImVec2 grab2 = ImVec2(200, 200); // | stuff
bool grabinit = false; //--

std::vector<BaseObject> objects = {};
std::vector<std::unique_ptr<BaseObject>> objects = {};
void drawall (int *select, bool staticlayout);
};

Expand Down
10 changes: 6 additions & 4 deletions src/sources/ims_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ int ImStudio::Serializer::SaveObject(YAML::Emitter& out, ImStudio::Object& obj)

std::vector<BaseObject> objects = {};
BEGIN_BSEQ(out, "Objects");
for (auto& baseobj : child.objects)
for (auto& cptr : child.objects)
{
SaveBaseObject(out, baseobj);
auto& cw = *cptr;
SaveBaseObject(out, cw);
}
END_SEQ(out);

Expand All @@ -149,10 +150,11 @@ int ImStudio::Serializer::SaveBuffer(YAML::Emitter& out, ImStudio::BufferWindow&
KEY(out, "StaticLayout", bw.staticlayout);

BEGIN_BSEQ(out, "Objects");
for (Object& obj : bw.objects)
for (auto& ptr : bw.objects)
{
auto& o = *ptr;
out << YAML::BeginMap;
SaveObject(out, obj);
SaveObject(out, o);
out << YAML::EndMap;
}
END_SEQ(out);
Expand Down

0 comments on commit ac16cd8

Please sign in to comment.