I am trying to implement Stockfish to my own project by a static library. But I have encountered a problem. Stockfish uses some global variables in the namespace of "Stockfish" and at somepoint in my project I want to destroy everything that I use from Stockfish and re-initialize later.
For example, Stockfish stores the options in a global object at Stockfish::Options. When I initialzie Stockfish for the first time everything is okay. But what I want to achieve it ending Stockfish and delete all members of it from variable without closing my project and reinitialize it whenever I want.
As far as I understand, Stockfish is not designed for that. It is desigend for "Start the engine, end the engine when the main program is closed.".
So, I believe I had to go through every initialization and find the proper ways to end them. Before doing that I wanted to ask that three questions:
The tooling doesn’t make it easy to free the memory used by a specific library.
Static library boundaries are not recorded, and do not exist at runtime (except maybe in debug symbols).
The allocator does not generally record where an allocation was made.
The same is true of namespaces. Namespaces do not exist at runtime, except in debug symbols. You cannot refer to a namespace.
There are two straightforward solutions:
Run Stockfish in a separate process. When the process exits, all memory is freed. This is the easy way.
Modify Stockfish so you can free the memory. Find all global state and all allocations, and make sure that you can free those allocations and reset the global state to the initial value. This is the hard way.