Skip to content

Commit

Permalink
improve: migrate Thing and derivatives to shared_ptr (#1592)
Browse files Browse the repository at this point in the history
This is a comprehensive update affecting nearly the entire codebase, driven by a focus on memory safety.

I've scrutinized constructors and destructors to identify areas incompatible with smart pointers—such as the inability to use shared_from_this() within constructors or destructors—and made appropriate changes.

Notable Points:

• Cylinder relationships were complex. To avoid cyclical dependencies that would hamper memory release, I've adopted a unidirectional reference strategy. Specifically, containers use std::shared_ptr for their itemList, whereas cylinders use std::weak_ptr for their parent, mitigating potential cycles. This alone is a safer alternative to using raw pointers.

• "Browse Field" posed another challenge. These are dynamically generated containers that need to be player-associated but also globally tracked by the Game class. The resolution was to use std::weak_ptr within Game, allowing the reference count to drop to zero when all players exit the browse field. This necessitated a cleanup loop to remove expired weak_ptr instances, which, conveniently, could be incorporated directly into the existing Game class.
  • Loading branch information
luan authored Sep 23, 2023
1 parent b16bd63 commit e63dc63
Show file tree
Hide file tree
Showing 195 changed files with 6,910 additions and 6,960 deletions.
76 changes: 39 additions & 37 deletions src/canary_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,57 +52,59 @@ CanaryServer::CanaryServer(
}

int CanaryServer::run() {
g_dispatcher().addTask([this] {
try {
loadConfigLua();
g_dispatcher().addTask(
[this] {
try {
loadConfigLua();

logger.info("Server protocol: {}.{}{}", CLIENT_VERSION_UPPER, CLIENT_VERSION_LOWER, g_configManager().getBoolean(OLD_PROTOCOL) ? " and 10x allowed!" : "");
logger.info("Server protocol: {}.{}{}", CLIENT_VERSION_UPPER, CLIENT_VERSION_LOWER, g_configManager().getBoolean(OLD_PROTOCOL) ? " and 10x allowed!" : "");

rsa.start();
initializeDatabase();
loadModules();
setWorldType();
loadMaps();
rsa.start();
initializeDatabase();
loadModules();
setWorldType();
loadMaps();

logger.info("Initializing gamestate...");
g_game().setGameState(GAME_STATE_INIT);
logger.info("Initializing gamestate...");
g_game().setGameState(GAME_STATE_INIT);

setupHousesRent();
setupHousesRent();

IOMarket::checkExpiredOffers();
IOMarket::getInstance().updateStatistics();
IOMarket::checkExpiredOffers();
IOMarket::getInstance().updateStatistics();

logger.info("Loaded all modules, server starting up...");
logger.info("Loaded all modules, server starting up...");

#ifndef _WIN32
if (getuid() == 0 || geteuid() == 0) {
logger.warn("{} has been executed as root user, "
"please consider running it as a normal user",
STATUS_SERVER_NAME);
}
if (getuid() == 0 || geteuid() == 0) {
logger.warn("{} has been executed as root user, "
"please consider running it as a normal user",
STATUS_SERVER_NAME);
}
#endif

g_game().start(&serviceManager);
g_game().setGameState(GAME_STATE_NORMAL);
g_game().start(&serviceManager);
g_game().setGameState(GAME_STATE_NORMAL);

g_webhook().sendMessage("Server is now online", "Server has successfully started.", WEBHOOK_COLOR_ONLINE);
g_webhook().sendMessage("Server is now online", "Server has successfully started.", WEBHOOK_COLOR_ONLINE);

loaderDone = true;
loaderSignal.notify_all();
} catch (FailedToInitializeCanary &err) {
loadFailed = true;
logger.error(err.what());
loaderDone = true;
loaderSignal.notify_all();
} catch (FailedToInitializeCanary &err) {
loadFailed = true;
logger.error(err.what());

logger.error("The program will close after pressing the enter key...");
logger.error("The program will close after pressing the enter key...");

if (isatty(STDIN_FILENO)) {
getchar();
}
if (isatty(STDIN_FILENO)) {
getchar();
}

loaderSignal.notify_all();
}
},
"CanaryServer::run");
loaderSignal.notify_all();
}
},
"CanaryServer::run"
);

loaderSignal.wait(loaderUniqueLock, [this] { return loaderDone || loadFailed; });

Expand Down Expand Up @@ -350,7 +352,7 @@ void CanaryServer::loadModules() {

g_game().loadBoostedCreature();
g_ioBosstiary().loadBoostedBoss();
g_ioprey().InitializeTaskHuntOptions();
g_ioprey().initializeTaskHuntOptions();
}

void CanaryServer::modulesLoadHelper(bool loaded, std::string moduleName) {
Expand Down
Loading

0 comments on commit e63dc63

Please sign in to comment.