0

We have static and global objects allocated on stack. These objects would be destroyed during process's main (…) function exit. But I would like to manage destruction of these before main's exit. To destroy these I will have to move the creation logic of these objects to heap and deallocate them. Is there any tool which tells us what all the static and global objects are still in the memory so that I can go ahead and make the code changes to destroy them.

Use case: There is a desktop application which uses static and global extensively. I would like to do little bit changes to this and use this as a library which should be stateless. For this during terminateLibrary all these static and global should be destroyed so that we can do subsequent InitLibrary afresh without actually restarting the process. moving existing architecture to totally a new design is very difficult and time taking, as there are huge number of these objects scattered through out the code. that's why I wanted to find out those static and global which is there in memory even after terminateLibrary called.

Rajeev kumar
  • 163
  • 1
  • 15
  • What is the actual problem you have? Your description is not consistent. Do you have globals outside your main? (not the best design anyway). Or do you create them as local variables (on the stack) in main? Show a minimal reproducable bit of example code. In any case I would propose to redesign your code, to inject your "global" data into the code that needs it. (Create an abstract base class/interface with getter methods for your data, derive an implementation and pass that interface on to the code that needs the global data). – Pepijn Kramer Nov 03 '22 at 04:20
  • You can then manage the lifecylce of your global data with scopes/RAII. And you have an interface with which you can build unit tests to boot. Similar topic here : [c-should-i-use-global-variables-or-class-member-pointers](https://stackoverflow.com/questions/5161776/c-should-i-use-global-variables-or-class-member-pointers-to-communicate-betwe). Do it like this and you don't have to guess/debug but it will be right by design. – Pepijn Kramer Nov 03 '22 at 04:21
  • @PepijnKramer updated the use case. Please have a look. – Rajeev kumar Nov 03 '22 at 04:29
  • Interesting, seem you have some technical debt, I think refactoring step by step to dependency injection is still the way to go to get to stateless libraries. I think what you are trying to do is trying to avoid changing existing code, but in the end I think getting a stable product that way is harder. You could always make an impact analysis for changing one such variable and I do hope you have unit tests. So my advice : fix the root cause not the symptoms. – Pepijn Kramer Nov 03 '22 at 05:06
  • @PepijnKramer Searching for static and global keywork and doing refactoring step by step to dependency injection a mammoth task for these kind of application. moving culprits only to the new design is the first thing I should do. For that I would like to know whether any tool can tell us about these object which are still there in memory even after terminate and also how much memory they are holding. so that I can prioritize the work. – Rajeev kumar Nov 03 '22 at 05:44
  • Do not tag both C and C++ except when asking about differences or interactions between the two languages. – Eric Postpischil Nov 03 '22 at 11:18
  • 1
    Re “We have static and global objects allocated on stack”: Are you referring to the hardware stack? C implementations that have a stack do not put objects with static lifetime on the stack. (There is no “global” name space or scope in C, except for reserved words or identifiers. There is a file scope and external linkage. Objects whose identifiers have file scope or external linkage have static storage duration.) What do you mean by this sentence? – Eric Postpischil Nov 03 '22 at 11:22

0 Answers0