Creating OPERX xltypeRef - how secure memory to be freed

Apr 9, 2013 at 12:47 PM
Hello Keith,

let say I am creating B2 reference OPER in sheet1 which has some SHEETID and I wrap this into some class with
OPERX oCell;
base part of constructor can be:
oCell.val.mref.lpmref = (xlmref *)malloc(sizeof(xlmref));
oCell.val.mref.lpmref->count = 1;
oCell.xltype = xltypeRef;
oCell.val.mref.idSheet = SHEETID;
oCell.val.mref.lpmref->reftbl[0].rwFirst = 2;
oCell.val.mref.lpmref->reftbl[0].rwLast = 2;
oCell.val.mref.lpmref->reftbl[0].colFirst =2;
oCell.val.mref.lpmref->reftbl[0].colLast = 2;
but when I finish work with class I need to free memory allocated for oCell and in destructor I think:
   free(oCell.val.mref.lpmref);
   oCell.val.mref.lpmref = nullptr;
OR
can I instead of free(oCell.val.mref.lpmref) in destructor secure it immediately in constructor by:
o.xltype |= xlbitDLLFree;
Will this make Excel responsible to free(oCell.val.mref.lpmref) and will excel secure this???

Thank you again,
BR L.
Coordinator
Apr 9, 2013 at 12:59 PM
Do not use OPER's for xltypeRef. They can be used for xltypeSRef. Use XLOPER's if you want to handle the memory management yourself.
Apr 9, 2013 at 11:10 PM
so to understand your anser right, you are recomending that I shold not use OPER's for xltypeRef even without xlbitDLLFree if I will manage free process described above through my own simple class wrapper?

and can you give me please advice in example how to create xltypeRef and how to benefit from your library, for example if I will use XLOPER for xltypeRef creation can I use your ExcelX or switch XLOPER to xltypemulti OPERX?

many thanks,

I am little loosing hope to I can run with big dogs, at least the very last one :-(

all the best, Lukas
Coordinator
Apr 10, 2013 at 3:27 AM
You understand right. OPER's were designed to handle memory management and behave like variant data types for the most common cases, but I did not consider using them for xltypeRef. I just never needed that, but am happy to fold in any code you want to contribute.

One design goal was to not get in the way of the standard Excel C SDK. You can use the Microsoft XLOPER struct in the same way they document it's usage. I don't have Bill's pocketbook and just try to leverage off of what already exists.

I you set xlbitDLLFree the default behavior is to call delete on the XLOPER*. Calling new on the XLOPER and malloc on val.mref.lpmref will not lead to a good time.

You seem to be getting there. ExcelX works fine with xltypeRef once you construct it. It might even be okay to use it with OPER's since the destructor will do nothing in that case. C++ is hard, but it makes sense. Still getting up to speed with the new C++2011 standard. What an amazing world we live in now.