在C++中,对于局部变量,当局部变量的生命周期结束时,会自动调用析构函数。
但是对于全局变量和静态变量来说,其生命周期较长,其析构函数将会在什么时机进行调用呢?让我们来探究一下。
我们先看下面的一段程序:
#include <iostream>
#include <cstdlib>
#include <string>
class Static {
public:
Static()
{
std::cout << "Static constructor\n";
}
~Static()
{
std::cout << "Static destructor\n";
}
};
class Global {
public:
Global()
{
std::cout << "Global constructor\n";
}
~Global()
{
std::cout << "Global destructor\n";
}
};
class Local {
public:
Local(std::string var_name)
{
__var_name = var_name;
std::cout << "Local:\"" << __var_name << "\" constructor\n";
}
~Local()
{
std::cout << "Local:\"" << __var_name << "\" destructor\n";
}
private:
std::string __var_name;
};
static Static static_var;
Global global_var;
void func(Local local)
{
Local local_in_func("local_in_func");
return;
}
int main()
{
std::cout << "first line in main\n";
Local local_var_in_main("local_var_in_main");
func(local_var_in_main);
{
Local local_in_bracket("local_in_bracket");
}
std::cout << "last line in main\n";
}
执行结果:
Static constructor
Global constructor
first line in main
Local:"local_var_in_main" constructor
Local:"local_in_func" constructor
Local:"local_in_func" destructor
Local:"local_var_in_main" destructor
Local:"local_in_bracket" constructor
Local:"local_in_bracket" destructor
last line in main
Local:"local_var_in_main" destructor
Global destructor
Static destructor
这里可以清楚的看到各种类型变量的构造函数和析构函数的执行时机,基本上是符合预期的,这里有一个有意思的是local_var_in_main的析构函数被执行了两次,但是其构造函数仅被执行了一次,如果我们的场景是构造函数申请一块内存,析构函数释放这块内存,目前这种方式是否会有问题?该如何解决?
我们知道,我们可以通过atexit来注册退出函数,我们可以在atexit中注册一些清理函数,在程序退出时能够将一些资源释放掉。那么,退出函数和全局变量的析构函数谁先执行呢?
我们通过以下一段代码进行验证:
#include <iostream>
#include <cstdlib>
#include <string>
void exitfunc()
{
std::cout << "exitfunc\n";
}
class Static {
public:
Static()
{
std::cout << "Static constructor\n";
}
~Static()
{
std::cout << "Static destructor\n";
}
};
class Global {
public:
Global()
{
std::cout << "Global constructor\n";
}
~Global()
{
std::cout << "Global destructor\n";
}
};
class Local {
public:
Local(std::string var_name)
{
__var_name = var_name;
std::cout << "Local:\"" << __var_name << "\" constructor\n";
}
~Local()
{
std::cout << "Local:\"" << __var_name << "\" destructor\n";
}
private:
std::string __var_name;
};
static Static static_var;
Global global_var;
void func(Local local)
{
Local local_in_func("local_in_func");
return;
}
int main()
{
std::cout << "first line in main\n";
atexit(exitfunc);
Local local_var_in_main("local_var_in_main");
func(local_var_in_main);
{
Local local_in_bracket("local_in_bracket");
}
std::cout << "last line in main\n";
}
执行结果:
Static constructor
Global constructor
first line in main
Local:"local_var_in_main" constructor
Local:"local_in_func" constructor
Local:"local_in_func" destructor
Local:"local_var_in_main" destructor
Local:"local_in_bracket" constructor
Local:"local_in_bracket" destructor
last line in main
Local:"local_var_in_main" destructor
exitfunc
Global destructor
Static destructor
可以看到退出函数的执行顺序是先于全局变量和静态变量的析构函数的。
那么如果我们在全局变量和静态变量的构造函数中注册exit函数的话,退出函数和析构函数的执行时机将会是怎样的呢?
通过下面一段代码验证:
#include <iostream>
#include <cstdlib>
#include <string>
void exitfunc_global()
{
std::cout << "exitfunc in global\n";
}
void exitfunc_static()
{
std::cout << "exitfunc in static\n";
}
class Static {
public:
Static()
{
atexit(exitfunc_static);
std::cout << "Static constructor\n";
}
~Static()
{
std::cout << "Static destructor\n";
}
};
class Global {
public:
Global()
{
atexit(exitfunc_global);
std::cout << "Global constructor\n";
}
~Global()
{
std::cout << "Global destructor\n";
}
};
class Local {
public:
Local(std::string var_name)
{
__var_name = var_name;
std::cout << "Local:\"" << __var_name << "\" constructor\n";
}
~Local()
{
std::cout << "Local:\"" << __var_name << "\" destructor\n";
}
private:
std::string __var_name;
};
static Static static_var;
Global global_var;
void func(Local local)
{
Local local_in_func("local_in_func");
return;
}
int main()
{
std::cout << "first line in main\n";
Local local_var_in_main("local_var_in_main");
func(local_var_in_main);
{
Local local_in_bracket("local_in_bracket");
}
std::cout << "last line in main\n";
}
执行结果:
Static constructor
Global constructor
first line in main
Local:"local_var_in_main" constructor
Local:"local_in_func" constructor
Local:"local_in_func" destructor
Local:"local_var_in_main" destructor
Local:"local_in_bracket" constructor
Local:"local_in_bracket" destructor
last line in main
Local:"local_var_in_main" destructor
Global destructor
exitfunc in global
Static destructor
exitfunc in static
这说明一个问题,析构函数的注册和atexit的注册应该是同一种机制,这些退出函数应该是被注册在一条链表中,先注册的先执行,后注册的后执行。全局变量和静态变量的析构函数大概是什么时候注册的?这我想应该比较明显了吧。
如果现在,我的需求是我们所注册的退出函数的执行时机必须晚于所有析构函数。有人可能会说我们在开发过程中不太会遇到这种场景,不,这是很有可能的。
谷歌编码规范明确指出,避免声明全局或静态的类,就是为了规避不确定的析构函数以及其他退出函数的调用顺序。
如何做到呢?我们可以通过gcc的attribute destructor来实现,且可以指定优先级。
更多精彩文章请关注知乎:系统编程-白质。 csdn一般是看心情发。