Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More work on destructors, trying to fix crashes #849

4 changes: 3 additions & 1 deletion engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ CONFIGURE_FILE(src/version.h.in ${Vega_Strike_BINARY_DIR}/setup/src/include/vers

MESSAGE("== Vega Strike Version: ${VEGASTRIKE_VERSION_LONG_STR}")

SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD 14)
SET(CMAKE_CXX_STANDARD_REQUIRED TRUE)
SET(CMAKE_CXX_EXTENSIONS ON)
SET(CMAKE_C_STANDARD 11)
Expand Down Expand Up @@ -1090,6 +1090,7 @@ ADD_LIBRARY(vegastrike-engine_com
${LIBROOTGENERIC_SOURCES}
${LIBSCRIPT_SOURCES}
${LIBGFXGENERIC_SOURCES}
src/vega_collection_utils.cpp
)

#TARGET_COMPILE_FEATURES(vegastrike-engine_com PUBLIC cxx_std_11)
Expand Down Expand Up @@ -1703,6 +1704,7 @@ IF (USE_GTEST)
${LIBRESOURCE}
${LIBCMD_SOURCES}
${LIBVS_LOGGING}
src/vega_collection_utils.cpp
)
target_compile_definitions(vegastrike-testing PUBLIC "BOOST_ALL_DYN_LINK" "$<$<CONFIG:Debug>:BOOST_DEBUG_PYTHON>")
set_property(TARGET vegastrike-testing PROPERTY POSITION_INDEPENDENT_CODE TRUE)
Expand Down
8 changes: 4 additions & 4 deletions engine/objconv/basemaker/base_maker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
#include <sys/types.h>

#if defined(__APPLE__) && defined(__MACH__)
#include <GLUT/glut.h>
#include <OpenGL/glext.h>
# include <GLUT/glut.h>
# include <OpenGL/glext.h>
#else
#include <GL/glut.h>
#include <GL/glext.h>
# include <GL/glut.h>
# include <GL/glext.h>
#endif

/*
Expand Down
8 changes: 4 additions & 4 deletions engine/objconv/basemaker/sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
#include "vs_globals.h"

#if defined(__APPLE__) && defined(__MACH__)
#include <GLUT/glut.h>
#include <OpenGL/glext.h>
# include <GLUT/glut.h>
# include <OpenGL/glext.h>
#else
#include <GL/glut.h>
#include <GL/glext.h>
# include <GL/glut.h>
# include <GL/glext.h>
#endif

#include <iostream>
Expand Down
4 changes: 2 additions & 2 deletions engine/src/cmd/drawable.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* drawable.cpp
*
* Copyright (C) 2020-2022 Daniel Horn, Roy Falk, Stephen G. Tuggy and other
* Copyright (C) 2020-2024 Daniel Horn, Roy Falk, Stephen G. Tuggy and other
* Vega Strike contributors
*
* https://github.com/vegastrike/Vega-Strike-Engine-Source
Expand Down Expand Up @@ -83,7 +83,7 @@ Drawable::~Drawable() {
}

bool Drawable::DrawableInit(const char *filename, int faction,
Flightgroup *flightgrp, const char *animationExt) {
Flightgroup *flightgrp, const char *animationExt) {
string fnam(filename);
string::size_type pos = fnam.find('.');
string anifilename = fnam.substr(0, pos);
Expand Down
9 changes: 4 additions & 5 deletions engine/src/cmd/drawable.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/**
/*
* drawable.h
*
* Copyright (C) 2020 Roy Falk, Stephen G. Tuggy and other Vega Strike
* contributors
* Copyright (C) 2022-2023 Stephen G. Tuggy, Benjamen R. Meyer
* Copyright (C) 2020-2024 Roy Falk, Stephen G. Tuggy, Benjamen R. Meyer,
* and other Vega Strike contributors
*
* https://github.com/vegastrike/Vega-Strike-Engine-Source
*
Expand All @@ -16,7 +15,7 @@
*
* Vega Strike is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
Expand Down
9 changes: 5 additions & 4 deletions engine/src/cmd/unit_generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ Unit::~Unit() {
#endif
pImage = nullptr;
delete pilot;
pilot = nullptr;
#ifdef DESTRUCTDEBUG
VS_LOG_AND_FLUSH(trace, (boost::format("%1$d") % 5));
#endif
Expand All @@ -384,10 +385,10 @@ Unit::~Unit() {
#ifdef DESTRUCTDEBUG
VS_LOG_AND_FLUSH(trace, (boost::format("%1$d") % 0));
#endif
for (size_t meshcount = 0; meshcount < meshdata.size(); ++meshcount) {
if (meshdata[meshcount] != nullptr) {
delete meshdata[meshcount];
meshdata[meshcount] = nullptr;
for (auto & mesh : meshdata) {
if (mesh != nullptr) {
delete mesh;
mesh = nullptr;
}
}
meshdata.clear();
Expand Down
5 changes: 1 addition & 4 deletions engine/src/gfx/aux_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,10 +556,7 @@ Texture::~Texture() {
} else {
original->refcount--;
if (original->refcount == 0) {
if (original == this) {
VS_LOG(debug, "In Texture::~Texture() in aux_texture.cpp: original->refcount == 0, but original == this, so not deleting again. (We are already in the destructor.)");
} else {
VS_LOG(trace, "In Texture::~Texture() in aux_texture.cpp: original->refcount == 0, and original != this, so deleting original");
if (original != this) {
delete original;
original = nullptr;
}
Expand Down
44 changes: 5 additions & 39 deletions engine/src/gfx/mesh_gfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
extern vector<Logo *> undrawn_logos;

#include <exception>
#include <vega_collection_utils.h>

#ifdef _MSC_VER
//Undefine those nasty min/max macros
Expand Down Expand Up @@ -438,45 +439,10 @@ Mesh::~Mesh() {
meshHashTable.Delete(hash_name);
}
vector<Mesh *> *hashers = bfxmHashTable.Get(hash_name);
vector<Mesh *>::iterator finder;
if (hashers) {
// the foollowing loop has several tricks to it:
// 1. `std::vector::erase()` can take an interator and remove it from the vector; but invalidates
// the iterator in the process
// 2. To overcome the invalid iterator issue, the next previous iterator is cached
// 3. In the case that the previous iterator would be invalid (e.g it's at the start) then it needs
// to restart the loop without the loop conditions, therefore a simple GOTO is used instead to
// avoid the incrementing the iterator so that values are not skipped
// A reverse iterator could kind of help this; however, `std::vector::erase` unfortunately
// does not work on reverse iterators.
for (auto hashItem = hashers->begin(); hashItem != hashers->end(); ++hashItem) {
retryEraseItem:
if (*hashItem == this) {
bool resetIter = false;
auto cachedHashItem = hashers->begin();
if (hashItem != hashers->begin()) {
cachedHashItem = hashItem - 1;
} else {
resetIter = true;
cachedHashItem = hashItem + 1;
}
hashers->erase(hashItem);
if (hashers->empty()) {
bfxmHashTable.Delete(hash_name);
delete hashers;
hashers = nullptr;
break;
}

if (resetIter) {
hashItem = hashers->begin();
// a necessary use of Goto as we do not want to use ++hashItem
goto retryEraseItem;
} else {
hashItem = cachedHashItem;
}
}
}
bool hashers_was_deleted = false;
remove_all_references_to(this, hashers, hashers_was_deleted);
if (hashers_was_deleted) {
VS_LOG_AND_FLUSH(debug, "hashers was deleted");
}
if (draw_queue != nullptr) {
delete[] draw_queue;
Expand Down
25 changes: 8 additions & 17 deletions engine/src/gfx/mesh_server.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/**
/*
* mesh_server.cpp
*
* Copyright (c) 2001-2002 Daniel Horn
* Copyright (c) 2002-2019 pyramid3d and other Vega Strike Contributors
* Copyright (c) 2019-2021 Stephen G. Tuggy, and other Vega Strike Contributors
* Copyright (C) 2022 Stephen G. Tuggy
* Copyright (c) 2001-2024 Daniel Horn, pyramid3d, Stephen G. Tuggy,
* Benjamen R. Meyer, Roy Falk, and other Vega Strike Contributors
*
* https://github.com/vegastrike/Vega-Strike-Engine-Source
*
Expand All @@ -17,7 +15,7 @@
*
* Vega Strike is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
Expand Down Expand Up @@ -120,17 +118,10 @@ Mesh::~Mesh() {
meshHashTable.Delete(hash_name);
}
vector<Mesh *> *hashers = bfxmHashTable.Get(hash_name);
vector<Mesh *>::iterator finder;
if (hashers) {
for (int i = hashers->size() - 1; i >= 0; --i) {
if ((*hashers)[i] == this) {
hashers->erase(hashers->begin() + i);
if (hashers->empty()) {
bfxmHashTable.Delete(hash_name);
delete hashers;
}
}
}
bool hashers_was_deleted = false;
remove_all_references_to(this, hashers, hashers_was_deleted);
if (hashers_was_deleted) {
VS_LOG_AND_FLUSH(debug, "hashers was deleted");
}
orig->refcount--;
//printf ("orig refcount: %d",refcount);
Expand Down
4 changes: 3 additions & 1 deletion engine/src/gldrv/gl_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,16 @@ struct GFXStats

#ifndef _WIN32
//#define GL_GLEXT_PROTOTYPES

#endif

#if defined (_WIN32) || defined (__CYGWIN__)
#ifndef NOMINMAX
#define NOMINMAX
#endif //tells VCC not to generate min/max macros
#include <windows.h>
#include <GL/gl.h>
#endif

#if defined(__APPLE__) && defined(__MACH__)
#include <GLUT/glut.h>
//#if defined( GL_INIT_CPP) || defined( GL_MISC_CPP) || defined( GL_STATE_CPP)
Expand All @@ -121,6 +122,7 @@ struct GFXStats

#include <GL/glext.h>
#endif

#ifdef _WIN32
# define GL_TEXTURE0_ARB 0x84C0
# define GL_TEXTURE1_ARB 0x84C1
Expand Down
14 changes: 8 additions & 6 deletions engine/src/gldrv/gl_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@
#endif
#define GL_GLEXT_PROTOTYPES 1
#if defined(__APPLE__) && defined(__MACH__)
#include <OpenGL/gl.h>
#include <OpenGL/glext.h>
#include <dlfcn.h>
# include <OpenGL/gl.h>
# include <OpenGL/glext.h>
# include <dlfcn.h>
#else
#include <GL/gl.h>
#include <GL/glext.h>
# include <GL/gl.h>
# include <GL/glext.h>
#endif
#ifdef GL_EXT_compiled_vertex_array
# ifndef PFNGLLOCKARRAYSEXTPROC
Expand Down Expand Up @@ -149,7 +149,7 @@ typedef void ( *(*get_gl_proc_fptr_t)(const GLubyte *))();
#if defined(__HAIKU__)
typedef char * GET_GL_PTR_TYP;
#define GET_GL_PROC glutGetProcAddress
#elif defined (__MACOSX__)
#elif defined (__APPLE__) && defined (__MACH__)
typedef const char * GET_GL_PTR_TYP;
void * vega_dlsym(GET_GL_PTR_TYP function_name) {
return dlsym(RTLD_SELF, function_name);
Expand Down Expand Up @@ -586,6 +586,8 @@ void GFXInit(int argc, char **argv) {
char vsicon[9] = "vega.ico";
winsys_init(&argc, argv, &vsname[0], &vsicon[0]);

/* Ignore key-repeat messages */
winsys_enable_key_repeat(false);

glViewport(0, 0, g_game.x_resolution, g_game.y_resolution);
static GFXColor clearcol = vs_config->getColor("space_background");;
Expand Down
8 changes: 4 additions & 4 deletions engine/src/gldrv/gl_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ typedef void (*PFNGLUNLOCKARRAYSEXTPROC)( void );

#elif !defined (_WIN32)
#if defined(__APPLE__) && defined(__MACH__)
#include <GLUT/glut.h>
#include <OpenGL/glext.h>
# include <GLUT/glut.h>
# include <OpenGL/glext.h>
#else
#include <GL/glut.h>
#include <GL/glext.h>
# include <GL/glut.h>
# include <GL/glext.h>
#endif

#if !defined (GL_GLEXT_VERSION) || GL_GLEXT_VERSION < 6
Expand Down
20 changes: 19 additions & 1 deletion engine/src/gldrv/winsys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* Vega Strike is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
Expand All @@ -25,6 +25,7 @@

#include <assert.h>
#include <sstream>
#include <SDL/SDL_keyboard.h>

#include "gl_globals.h"
#include "winsys.h"
Expand Down Expand Up @@ -351,6 +352,23 @@ void winsys_shutdown() {
keepRunning = false;
}

/*---------------------------------------------------------------------------*/
/*!
* Enables/disables key repeat messages from being generated
* \return
* \author jfpatry
* \date Created: 2000-10-19
* \date Modified: 2000-10-19
*/
void winsys_enable_key_repeat(bool enabled) {
if (enabled) {
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
SDL_DEFAULT_REPEAT_INTERVAL);
} else {
SDL_EnableKeyRepeat(0, 0);
}
}

/*---------------------------------------------------------------------------*/
/*!
* Shows/hides mouse cursor
Expand Down
3 changes: 2 additions & 1 deletion engine/src/gldrv/winsys.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* Vega Strike is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
Expand Down Expand Up @@ -318,6 +318,7 @@ void winsys_set_motion_func(winsys_motion_func_t func);
void winsys_set_passive_motion_func(winsys_motion_func_t func);

void winsys_swap_buffers();
void winsys_enable_key_repeat(bool enabled);
void winsys_warp_pointer(int x, int y);
void winsys_show_cursor(bool visible);

Expand Down
Loading
Loading