Tuesday, February 21, 2012

One possible solution about a c++ static object destruction problem

 

I just found an interesting post from http://www.missdeer.com/articles/1560 (sorry, a post written in Chinese, you can google-translate itSmile).

In the post, they had a problem in their products, when process enters _exit() call, the CLR cleanup codes calls into an global object which has been destroyed, and it crashes.

Though I don’t know what kind of situation they may have, but as far as I know, we can choose “#pragma init_seg()” family to control the global static object construction and destroy order. For instance, “#pragma init_seg(compiler)”  can guarantee objects defined in it will be constructed before any other objects, and destroyed after all others. Like cin and cout in CRT is implemented with that option.

The init_seg family also include:

#pragma init_seg(lib)

#pragma init_seg(user)

#pragma init_seg(“user defined segment”)

The construction order follows the above sequence, the compiler group constructs before lib group, which created before user group, and so on.

The destroy order is reverse to the construction.

Though have not tried their case, but may be the solution.

No comments:

Post a Comment