What do two functions of the variantclear and VariantInit functions do? Recently, I have to seriously study the role of the program.
1.VariantClear () function
Clears the contents of a variant and sets the variant to VT_EMPTY.
How to do clears the contents of a variant (empty the contents of the variable). This is achieved by invoking the free space of the corresponding variable type. And he not only freed up space, but also initially changed the variables. The Simple English explanation is not translated.
Comments
VariantClear is used to clear the contents of initialized variants. Use VariantClear before a Variant variable goes out of scope (i.e. to release the contents of local variants before Leavin G A subroutine or function).
VariantClear sets the content of Vardest to Vt_empty. In addition, if Vardest had a reference to a bstring or Safe Array, and VariantClear calls Strfree to release the MEM Ory with the bstring or SafeArrayDestroy to destroy the contained Safe Array. If Vardest had a reference to a object, VariantClear calls the "object" method.
You don ' t need to call variantclear for variants, that contain values, are not dynamically allocated. For example, you don ' t need to call this subroutine to clear a Variant containing a VT_I4 (an integer) or a vt_r4 (real) v Alue. Note, however, which failure to call VariantClear on variants, contain dynamically allocated values may, Memor Y leaks, because the bstring or Safe Array referenced by the variant may never.
2.VariantInit function
Initializes a variant to Vt_empty
His role is very simple, that is, initially into VT_EMPTY. In fact, this variantclear () has been done, if you call it, you can not VariantInit ().
3. Tell me about the problems I've been having.
There has been a memory access violation error, I use the method of annotation, finally to locate a line of code.
SafeArrayDestroy (SA);
VariantClear (&SA);
Just call VariantClear (&SA), and then you have to delete it, it's okay. Check the function description to understand that the original is because the SA space released two times ah. and VariantClear (&SA); This can free space according to type, feel better, and finally put SafeArrayDestroy (SA); Solve the problem.