Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/imvue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,27 @@ namespace ImVue {
: mDocument(0)
, mRawData(NULL)
, mMounted(false)
, mRefs((int*)ImGui::MemAlloc(sizeof(int)))
{
*mRefs = 1;
}

ComponentContainer::~ComponentContainer()
{
if((*mRefs) > 1) {
mChildren.clear();
}
destroy();
}

void ComponentContainer::destroy()
{
if(!mRefs || --(*mRefs) > 0) {
return;
}

ImGui::MemFree(mRefs);

fireCallback(ScriptState::BEFORE_DESTROY);
removeChildren();

Expand Down
35 changes: 35 additions & 0 deletions src/imvue.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ namespace ImVue {

protected:

void destroy();

virtual bool build();

/**
Expand All @@ -119,10 +121,43 @@ namespace ImVue {
char* mRawData;
bool mMounted;

/**
* Override copy constructor and assignment
*/
ComponentContainer& operator=(ComponentContainer& other)
{
if(mRefs != other.mRefs) {
destroy();
}

ComponentContainer tmp(other);
swap(*this, tmp);
return *this;
}

ComponentContainer(ComponentContainer& other)
: ContainerElement(other)
, mDocument(other.mDocument)
, mRawData(other.mRawData)
, mMounted(other.mMounted)
, mRefs(other.mRefs)
{
(*mRefs)++;
}

private:

friend void swap(ComponentContainer& first, ComponentContainer& second) // nothrow
{
std::swap(first.mRefs, second.mRefs);
std::swap(first.mDocument, second.mDocument);
std::swap(first.mRawData, second.mRawData);
std::swap(first.mMounted, second.mMounted);
}

typedef std::unordered_map<ImU32, ComponentFactory> ComponentFactories;
ComponentFactories mComponents;
int* mRefs;

};

Expand Down
6 changes: 5 additions & 1 deletion src/lua/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,10 @@ extern "C" {

void LuaScriptState::eval(const char* str, std::string* retval, Fields* fields, ScriptState::Context* ctx)
{
if(!mImVue) {
return;
}

StackGuard g(mLuaState);
std::stringstream script;
std::string data(str);
Expand Down Expand Up @@ -1180,7 +1184,7 @@ extern "C" {
Object LuaScriptState::getObject(const char* str, Fields* fields, ScriptState::Context* ctx)
{
StackGuard g(mLuaState);
if(str[0] == '\0') {
if(str[0] == '\0' || !mImVue) {
return Object();
}

Expand Down
21 changes: 21 additions & 0 deletions tests/unit/parser.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include <gtest/gtest.h>
#include "imvue.h"
#include "imvue_generated.h"
#include "imvue_errors.h"
#include "utils.h"

Expand Down Expand Up @@ -70,3 +71,23 @@ TEST(DocumentParser, BadScript)
document.parse(badScript);
renderDocument(document);
}

/**
* Create document and copy it, should delete, ctx and other pointers only once
*/
TEST(DocumentParser, DocumentCopy)
{
ImVue::Context* ctx = ImVue::createContext(
ImVue::createElementFactory()
);
ImVue::Document document(ctx);
document.parse(simple);
renderDocument(document);

ImVue::Document copied(document);
renderDocument(document);
renderDocument(copied);

ImVue::Document duplicate = document;
renderDocument(duplicate);
}