The principle of memory management is: who applies for and who releases.
However, in terms of convenience and efficiency, the memory management method adopted by COM is: The function dynamically requests memory based on actual needs, and the caller is responsible for releasing the memory.
Com provides three memory allocation and release functions:
| |
BSTR |
Imalloc Interface |
Com Library |
| Application |
Sysallocstring () |
Alloc () |
Cotaskmemalloc () |
| Apply again |
Sysreallocstring () |
Realloc () |
Cotaskrealloc () |
| Release |
Sysfreestring () |
Free () |
Cotaskmemfree () |
1. cotaskxxx () function family, which calls the library function (malloc...) of C language in essence ...);
2. The imalloc interface encapsulates the cotaskxxx () function family and enhances some functions. For example, imalloc: getsize () can be used to obtain the size. imallocspy can be used to monitor memory usage;
Example:
1 BSTR pstr; 2 progidfromclsid (clsid_mystringfactory, & pstr); // apply for space in the function 3 4 cotaskmemfree (pstr); // release space
Com Memory Management