This time, because the project is divided into multiple DLL modules, and the new memory block in a DLL module may traverse other DLL modules, in the end, it may not be destroyed in the DLL it was born, which may cause memory allocation recovery problems.
After consideration, it is decided that a DLL module can independently apply for and release the maintenance memory. Any other module that requires dynamic memory application can call the interface in the DLL. However, in practice, there is a problem:
I originally wanted to reload new and delete in the DLL, so that other modules can only link to the DLL and reuse the code to the maximum extent.
But in fact, it is impossible to experiment. It is okay to expose the function as an export function after the new overload, but the delete operation won't work. Because Global Delete cannot have other parameters (unlike new, new, you can reload them if you comply with the Rules), links will fail due to repeated definitions when published for export, there is no good solution yet. As I know, it may be impossible to solve this problem. Only the DLL can be used for memory management. All modules use a set of source code files that reload new/Delete. In this way, the overloaded code of new/delete cannot be implemented in the dll library. This is not the case, but it may not be elegant enough.
Another idea is to record: when allocating a memory of N, actually allocating N + M bytes and M bytes are used for some cookies to record the length and flag, prepare for memory management. In addition, you can add a flag to check whether the memory is recycled incorrectly or as a basis for diagnosing Memory leakage in the future.
The last two tips are as follows:
Concepts of new and delete overloading: First, new and delete cannot be overloaded, and you cannot change the native semantics of new and delete. What you can reload, it is actually operator new and operator Delete. Note that this is only two operators. However, due to the name, it is easy to confuse them. New will call the operator new operator to complete the function, and delete will call the operator Delete operator to complete the function. Therefore, we can reload the function as operator rather than new and delete itself, although the effect is the same, however, there is a clear difference in concept.
Priority of new, new [], delete, and delete:
If you only reload operator new and operator Delete, calls to new [] and delete [] will be included in your overloaded function.
If you load operator new and new [], operator Delete and delete [] At the same time, when new [] and delete [] are used, the new [] and delete [] That you overload will be ignored, and the new and delete
If you only reload new [] and delete [], the use of new and delete will not be affected and will be implemented by default. For new [] and delete [], the new [] and delete [] That you reload will be entered.
As you can see, new [] and delete [] are included by new/delete, but you are allowed to perform selective overloading when you only want to reload new [] and delete.