我們已經繼承了大致的結構是這樣大的遺留應用程序:爲「應用程序生命週期」的清理工作的必要性對象
class Application
{
Foo* m_foo;
Bar* m_bar;
Baz* m_baz;
public:
Foo* getFoo() { return m_foo; }
Bar* getBar() { return m_bar; }
Baz* getBaz() { return m_baz; }
void Init()
{
m_foo = new Foo();
m_bar = new Bar();
m_baz = new Baz();
// all of them are singletons, which can call each other
// whenever they please
// may have internal threads, open files, acquire
// network resources, etc.
SomeManager.Init(this);
SomeOtherManager.Init(this);
AnotherManager.Init(this);
SomeManagerWrapper.Init(this);
ManagerWrapperHelper.Init(this);
}
void Work()
{
SomeManagerWrapperHelperWhateverController.Start();
// it will never finish
}
// no destructor, no cleanup
};
所有的經理曾經創造了在那裏呆上整個應用程序生命週期。應用程序沒有關閉或關閉方法,管理員也沒有這些方法。所以,複雜的相互依賴關係從來沒有處理過。
問題是:如果對象生命週期與應用程序生命週期緊密結合,那麼它是否被認爲是根本不需要清理?操作系統(我們的例子中的Windows)是否能夠在流程結束時清除所有內容(終止線程,關閉打開的文件句柄,套接字等)(通過在任務管理器中結束或通過調用諸如ExitProcess,Abort,等等。)?上述方法可能存在什麼問題?
或者更通用的問題:絕對必要的全局對象的析構函數(在main之外聲明)?
謝謝,會議的審議似乎是我正在尋找的答案。 – Bazurbat 2012-08-20 12:19:02