Как вызвать деструктор одноэлементных классов без CRTC++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Как вызвать деструктор одноэлементных классов без CRT

Сообщение Anonymous »


I am developing a program without the C runtime library (CRT) with C++ as a fun little challenge. As part of this challenge I ended up writing my own atexit() function which is called whenever main returns.

This function works as expected and calls the destructor of most classes, with a caveat: I have to manually run all destructor functions (they are stored in a linked list, and executed in a last-in, first-out (LIFO) order) before my main function terminates. If anybody knows a better way, do let me know, but that's a separate issue.

My main problem is that while debugging, I found out that the destructor of my singleton classes are not being called, and I'm trying to figure out why. I am new to C++, please don't bully me.

Code example of singleton:

/*socket_manager.h*/ class SocketManager { // static instance variable here static SocketManager* instance; ~SocketManager() { // close socket/do cleanup, this is what I'm trying to get to run } public: static SocketManager* getInstance(); } /*socket_manager.cpp*/ // global variable for instance, set to nullptr SocketManager* SocketManager::instance = nullptr; // creates new SocketManager if instance is null, else returns instance SocketManager *SocketManager::getInstance() { if (instance == nullptr) { instance = new SocketManager(); } return instance; } Essentially: What am I doing wrong? How do I make sure the destructor for this singleton is executed on program termination, without the STL/CRT?


Источник: https://stackoverflow.com/questions/781 ... ithout-crt
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C++»