diff --git a/src/imvue.cpp b/src/imvue.cpp
index ed7a780..ad0f038 100644
--- a/src/imvue.cpp
+++ b/src/imvue.cpp
@@ -371,13 +371,18 @@ namespace ImVue {
void Component::configure(rapidxml::xml_node<>* node, Context* ctx, ScriptState::Context* sctx, Element* parent)
{
Context* child = newChildContext(ctx);
- child->root = this;
- if(child->script) {
- child->script->initialize(mData);
- child->script->lifecycleCallback(ScriptState::BEFORE_CREATE);
- }
+ try {
+ child->root = this;
+ if(child->script) {
+ child->script->initialize(mData);
+ child->script->lifecycleCallback(ScriptState::BEFORE_CREATE);
+ }
- mScriptState = ctx->script;
+ mScriptState = ctx->script;
+ } catch(...) {
+ delete child;
+ throw;
+ }
Element::configure(node, child, sctx, parent);
if(mConfigured) {
fireCallback(ScriptState::CREATED);
diff --git a/src/imvue_element.cpp b/src/imvue_element.cpp
index ab919f8..8575717 100644
--- a/src/imvue_element.cpp
+++ b/src/imvue_element.cpp
@@ -516,7 +516,7 @@ namespace ImVue {
void Element::fireCallback(ScriptState::LifecycleCallbackType cb, bool schedule)
{
- if(!mCtx->script) {
+ if(!mCtx || !mCtx->script) {
return;
}
diff --git a/tests/resources/bad.imv b/tests/resources/bad.imv
new file mode 100644
index 0000000..192c6a7
--- /dev/null
+++ b/tests/resources/bad.imv
@@ -0,0 +1,18 @@
+
+
+
+
diff --git a/tests/resources/components.xml b/tests/resources/components.xml
index a9ead3d..53dedb0 100644
--- a/tests/resources/components.xml
+++ b/tests/resources/components.xml
@@ -3,7 +3,10 @@
current count {{ self.count }}
-
+
+
+
+
diff --git a/tests/unit/state.cpp b/tests/unit/state.cpp
index b7198f7..1d1a5d5 100644
--- a/tests/unit/state.cpp
+++ b/tests/unit/state.cpp
@@ -679,6 +679,29 @@ TEST_F(LuaScriptStateTest, TestComponentReactivity)
EXPECT_STREQ(local[0]->text, "updated");
}
+TEST_F(LuaScriptStateTest, TestComponentCreationError)
+{
+ ImVue::LuaScriptState* state = new ImVue::LuaScriptState(L);
+ ImVue::Context* ctx = ImVue::createContext(
+ ImVue::createElementFactory(),
+ state
+ );
+ luaL_dostring(L, "widget = 'bad'");
+ ImVue::Document document(ctx);
+ char* data = ctx->fs->load("components.xml");
+ ASSERT_NE((size_t)data, 0);
+ EXPECT_THROW({
+ try{
+ document.parse(data);
+ renderDocument(document);
+ } catch(ImVue::ElementError e) {
+ std::cout << e.what() << "\n";
+ throw;
+ }
+ }, ImVue::ScriptError);
+ ImGui::MemFree(data);
+}
+
TEST_F(LuaScriptStateTest, TestComponentLifecycle)
{
ImVue::LuaScriptState* state = new ImVue::LuaScriptState(L);