bugfix: segfault due to use of uninitialized Vector class reference
This commit is contained in:
parent
d2e4791f5a
commit
02c512402e
2 changed files with 78 additions and 54 deletions
|
|
@ -50,8 +50,16 @@ PyClickCallable::PyClickCallable()
|
||||||
|
|
||||||
void PyClickCallable::call(sf::Vector2f mousepos, std::string button, std::string action)
|
void PyClickCallable::call(sf::Vector2f mousepos, std::string button, std::string action)
|
||||||
{
|
{
|
||||||
// Create a Vector object for the position
|
// Create a Vector object for the position - must fetch the finalized type from the module
|
||||||
PyObject* pos = PyObject_CallFunction((PyObject*)&mcrfpydef::PyVectorType, "ff", mousepos.x, mousepos.y);
|
PyObject* vector_type = PyObject_GetAttrString(McRFPy_API::mcrf_module, "Vector");
|
||||||
|
if (!vector_type) {
|
||||||
|
std::cerr << "Failed to get Vector type for click callback" << std::endl;
|
||||||
|
PyErr_Print();
|
||||||
|
PyErr_Clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
PyObject* pos = PyObject_CallFunction(vector_type, "ff", mousepos.x, mousepos.y);
|
||||||
|
Py_DECREF(vector_type);
|
||||||
if (!pos) {
|
if (!pos) {
|
||||||
std::cerr << "Failed to create Vector object for click callback" << std::endl;
|
std::cerr << "Failed to create Vector object for click callback" << std::endl;
|
||||||
PyErr_Print();
|
PyErr_Print();
|
||||||
|
|
|
||||||
120
src/UIGrid.cpp
120
src/UIGrid.cpp
|
|
@ -686,19 +686,23 @@ UIDrawable* UIGrid::click_at(sf::Vector2f point)
|
||||||
|
|
||||||
// Only fire if within valid grid bounds
|
// Only fire if within valid grid bounds
|
||||||
if (cell_x >= 0 && cell_x < this->grid_x && cell_y >= 0 && cell_y < this->grid_y) {
|
if (cell_x >= 0 && cell_x < this->grid_x && cell_y >= 0 && cell_y < this->grid_y) {
|
||||||
// Create Vector object for cell position
|
// Create Vector object for cell position - must fetch finalized type from module
|
||||||
PyObject* cell_pos = PyObject_CallFunction((PyObject*)&mcrfpydef::PyVectorType, "ff", (float)cell_x, (float)cell_y);
|
PyObject* vector_type = PyObject_GetAttrString(McRFPy_API::mcrf_module, "Vector");
|
||||||
if (cell_pos) {
|
if (vector_type) {
|
||||||
PyObject* args = Py_BuildValue("(O)", cell_pos);
|
PyObject* cell_pos = PyObject_CallFunction(vector_type, "ff", (float)cell_x, (float)cell_y);
|
||||||
Py_DECREF(cell_pos);
|
Py_DECREF(vector_type);
|
||||||
PyObject* result = PyObject_CallObject(on_cell_click_callable->borrow(), args);
|
if (cell_pos) {
|
||||||
Py_DECREF(args);
|
PyObject* args = Py_BuildValue("(O)", cell_pos);
|
||||||
if (!result) {
|
Py_DECREF(cell_pos);
|
||||||
std::cerr << "Cell click callback raised an exception:" << std::endl;
|
PyObject* result = PyObject_CallObject(on_cell_click_callable->borrow(), args);
|
||||||
PyErr_Print();
|
Py_DECREF(args);
|
||||||
PyErr_Clear();
|
if (!result) {
|
||||||
} else {
|
std::cerr << "Cell click callback raised an exception:" << std::endl;
|
||||||
Py_DECREF(result);
|
PyErr_Print();
|
||||||
|
PyErr_Clear();
|
||||||
|
} else {
|
||||||
|
Py_DECREF(result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -715,19 +719,23 @@ UIDrawable* UIGrid::click_at(sf::Vector2f point)
|
||||||
|
|
||||||
// Only fire if within valid grid bounds
|
// Only fire if within valid grid bounds
|
||||||
if (cell_x >= 0 && cell_x < this->grid_x && cell_y >= 0 && cell_y < this->grid_y) {
|
if (cell_x >= 0 && cell_x < this->grid_x && cell_y >= 0 && cell_y < this->grid_y) {
|
||||||
// Create Vector object for cell position
|
// Create Vector object for cell position - must fetch finalized type from module
|
||||||
PyObject* cell_pos = PyObject_CallFunction((PyObject*)&mcrfpydef::PyVectorType, "ff", (float)cell_x, (float)cell_y);
|
PyObject* vector_type = PyObject_GetAttrString(McRFPy_API::mcrf_module, "Vector");
|
||||||
if (cell_pos) {
|
if (vector_type) {
|
||||||
PyObject* args = Py_BuildValue("(O)", cell_pos);
|
PyObject* cell_pos = PyObject_CallFunction(vector_type, "ff", (float)cell_x, (float)cell_y);
|
||||||
Py_DECREF(cell_pos);
|
Py_DECREF(vector_type);
|
||||||
PyObject* result = PyObject_CallObject(on_cell_click_callable->borrow(), args);
|
if (cell_pos) {
|
||||||
Py_DECREF(args);
|
PyObject* args = Py_BuildValue("(O)", cell_pos);
|
||||||
if (!result) {
|
Py_DECREF(cell_pos);
|
||||||
std::cerr << "Cell click callback raised an exception:" << std::endl;
|
PyObject* result = PyObject_CallObject(on_cell_click_callable->borrow(), args);
|
||||||
PyErr_Print();
|
Py_DECREF(args);
|
||||||
PyErr_Clear();
|
if (!result) {
|
||||||
} else {
|
std::cerr << "Cell click callback raised an exception:" << std::endl;
|
||||||
Py_DECREF(result);
|
PyErr_Print();
|
||||||
|
PyErr_Clear();
|
||||||
|
} else {
|
||||||
|
Py_DECREF(result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Don't return this - no click_callable to call
|
// Don't return this - no click_callable to call
|
||||||
|
|
@ -2248,38 +2256,46 @@ void UIGrid::updateCellHover(sf::Vector2f mousepos) {
|
||||||
if (new_cell != hovered_cell) {
|
if (new_cell != hovered_cell) {
|
||||||
// Fire exit callback for old cell
|
// Fire exit callback for old cell
|
||||||
if (hovered_cell.has_value() && on_cell_exit_callable) {
|
if (hovered_cell.has_value() && on_cell_exit_callable) {
|
||||||
// Create Vector object for cell position
|
// Create Vector object for cell position - must fetch finalized type from module
|
||||||
PyObject* cell_pos = PyObject_CallFunction((PyObject*)&mcrfpydef::PyVectorType, "ff", (float)hovered_cell->x, (float)hovered_cell->y);
|
PyObject* vector_type = PyObject_GetAttrString(McRFPy_API::mcrf_module, "Vector");
|
||||||
if (cell_pos) {
|
if (vector_type) {
|
||||||
PyObject* args = Py_BuildValue("(O)", cell_pos);
|
PyObject* cell_pos = PyObject_CallFunction(vector_type, "ff", (float)hovered_cell->x, (float)hovered_cell->y);
|
||||||
Py_DECREF(cell_pos);
|
Py_DECREF(vector_type);
|
||||||
PyObject* result = PyObject_CallObject(on_cell_exit_callable->borrow(), args);
|
if (cell_pos) {
|
||||||
Py_DECREF(args);
|
PyObject* args = Py_BuildValue("(O)", cell_pos);
|
||||||
if (!result) {
|
Py_DECREF(cell_pos);
|
||||||
std::cerr << "Cell exit callback raised an exception:" << std::endl;
|
PyObject* result = PyObject_CallObject(on_cell_exit_callable->borrow(), args);
|
||||||
PyErr_Print();
|
Py_DECREF(args);
|
||||||
PyErr_Clear();
|
if (!result) {
|
||||||
} else {
|
std::cerr << "Cell exit callback raised an exception:" << std::endl;
|
||||||
Py_DECREF(result);
|
PyErr_Print();
|
||||||
|
PyErr_Clear();
|
||||||
|
} else {
|
||||||
|
Py_DECREF(result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fire enter callback for new cell
|
// Fire enter callback for new cell
|
||||||
if (new_cell.has_value() && on_cell_enter_callable) {
|
if (new_cell.has_value() && on_cell_enter_callable) {
|
||||||
// Create Vector object for cell position
|
// Create Vector object for cell position - must fetch finalized type from module
|
||||||
PyObject* cell_pos = PyObject_CallFunction((PyObject*)&mcrfpydef::PyVectorType, "ff", (float)new_cell->x, (float)new_cell->y);
|
PyObject* vector_type = PyObject_GetAttrString(McRFPy_API::mcrf_module, "Vector");
|
||||||
if (cell_pos) {
|
if (vector_type) {
|
||||||
PyObject* args = Py_BuildValue("(O)", cell_pos);
|
PyObject* cell_pos = PyObject_CallFunction(vector_type, "ff", (float)new_cell->x, (float)new_cell->y);
|
||||||
Py_DECREF(cell_pos);
|
Py_DECREF(vector_type);
|
||||||
PyObject* result = PyObject_CallObject(on_cell_enter_callable->borrow(), args);
|
if (cell_pos) {
|
||||||
Py_DECREF(args);
|
PyObject* args = Py_BuildValue("(O)", cell_pos);
|
||||||
if (!result) {
|
Py_DECREF(cell_pos);
|
||||||
std::cerr << "Cell enter callback raised an exception:" << std::endl;
|
PyObject* result = PyObject_CallObject(on_cell_enter_callable->borrow(), args);
|
||||||
PyErr_Print();
|
Py_DECREF(args);
|
||||||
PyErr_Clear();
|
if (!result) {
|
||||||
} else {
|
std::cerr << "Cell enter callback raised an exception:" << std::endl;
|
||||||
Py_DECREF(result);
|
PyErr_Print();
|
||||||
|
PyErr_Clear();
|
||||||
|
} else {
|
||||||
|
Py_DECREF(result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue