Is there a way to make sure my function will be the last function?

I intercepted the global new/deleteone to catch a memory leak. When the process ends, I need to call assertNoMemoryLeakto bring all the allocated memory to a new one.

But it seems that I cannot make the assertNoMemoryLeaklast function called in my process, because the last global variable destructor will be the last.

  • atexitdoes not work for the following reason. AFAIK, the global destructor generated by gcc, will be redirected to the list atexitwhen the constructor is called, and I also cannot snippet my push file assertNoMemoryLeak before this at startup, so mine assertNoMemoryLeakwill not be launched as the last function anyway.

  • Another work around is to write the new/deleteinfo file to a file, and then after the process exits, analyze the file (using a script). I do not want to do it this way, because it is difficult to do this automatically for each developer.

So, any way to make the assertNoMemoryLeaklast function called?

+4
source share
1 answer

If the search and management of all global static objects is practical, you can create one global static object whose task is to build and destroy all other global objects in the desired order. If you do not have control over all of them or they have too many different places in different libraries, this will not be practical.

, . , , , assert , .

0

Source: https://habr.com/ru/post/1547118/


All Articles