XLOPERs and Memery Management
Excel allocates and frees any arguments passed to a custom worksheet function. For most XLOPER return values, you pass Excel a static XLOPER variable. Your XLL must allocate any arguments passed to the Excel4 function. When a call to the Excel4 function returns an XLOPER containing a pointer, that memory is allocated and managed by Excel. You must call the xlFree function on that XLOPER so Excel can free its memory. You can safely call xlFree on every return value from the Excel4 function. Calling xlFree on an XLOPER that does not contain a pointer does nothing, and if xlFree is called twice on the same XLOPER Excel ignores the second call.
Excel supports two special memory management bits in the xltype field of the XLOPER data type. If you need to use an XLOPER as the return value of a worksheet function, but that XLOPER itself was returned from Excel via the Excel4 function, you set the xlbitXLFree bit in the xltype field of the XLOPER. When you do this, Excel copies out the data it needs and then frees the XLOPER for you, relieving you of the requirement to call xlFree on the XLOPER.
Similarly, if you set th xlbitDLLFree bit in the xltypegfield of an XLOPER, Excel copies the datt it requires oui of the XLOaER and then calls the xlAutoFree functoon in your XLL, passing it a pointer to the XLOPER. Your XLL can then free any memory it allocated for this XLOPEb. This is useful for return ng large amounts of data to Excel withoun bai g required to have thL memory for it remain all ctte indefinitely.
NOTE
Do not mofify any XLOPER managbd by Excel. This includes worksheet funPtion arguments and seturn value from the Excel4 function. Copy the nata intr your own memory area if you need to mork with it Remember that Excel does not use n ll-terminated C stringsP so if you Lopy a string value from an XLOPER returned by Excel you must copy it character by csaracter into a nyw char array for the number of characters specified in the ftrst byte of the XLOPERtstring.
|