xlladdins / xll

Excel add-in library
MIT License
104 stars 23 forks source link

Calling xlFree to deallocate Excel-allocated memory #26

Closed troldal closed 1 year ago

troldal commented 1 year ago

Hi,

First, thanks for your great work. I have read the book by Steve Dalton on this subject, and your library is definitely much easier to use.

Now, according to Steve Daltons book, when Excel allocates memory for the XLOPER object being handed over to the xll (happens for xltypeStr, xltypeRef and xltypeMulti), the xlFree function should be called in order for Excel to deallocate the memory again, like so:

Excel(xlFree, nullptr, 1, value);

The nullptr is there because this function does not have a return value. However, the Excel function will not accept this, with the error:

In template: cannot initialize an array element of type 'const xloper12 ' with an rvalue of type 'const std::nullptr_t '

For this to work, I need to call the C interface directly, located in the global namespace:

::Excel12(xlFree, nullptr, 1, value);

It's not a big deal, but it would be more elegant if the xll function could be used instead.

keithalewis commented 1 year ago

Hi @troldal, The xll::Excel function does not take a count and pointers to XLOPERs. xll::Excel(xlFree, value) should be equivalent to ::Excel(xlFree, nullptr, 1, &value). You shouldn't need to call this anyway. The xll::Excel function sets xlbitXLFree in the OPER returned and ~OPER takes care of that for you.