Object Handler with std::vector<double> as class member

Apr 30, 2015 at 4:10 PM
Hi Keith,
Hope you would be so kind as to help me out.
I've a bug and can't spot it after several hours.
I have a Class with a std::vector<double as a private member. When recovering the Object via the handle the contents of the vector are not there.
Below my code (essentially).

class A
{
public:
A(void){}
~A(void){}

A(std::vector<double>* pX){mPtrX = pX ;}
protected:
 std::vector<double>* mPtrX;
};

static AddInX qAddInX_Inter(
_T("?qAddInX_inter"),
XLL_HANDLE      
XLL_FPX         
XLL_UNCALCEDX,
_T("QInter"),
_T("Range "),
_T("QA"),
_T("Inter..")   
);
HANDLEX WINAPI
qAddInX_inter(const _FP* pa)
{

pragma XLLEXPORT

static xll::FP a;

a = *pa;

std::vector<double> vX;
for (WORD i = 0; i < a.size() ; ++i)
{   
    double d =  (a.array())[i]; 
    vX.push_back(d);
}

A* ptrA = new A(&vX);

// Create handle
handle<A> h(ptrA);
HANDLEX hx = h.get();
return hx;
}

static AddInX qAddIn_Func(
_T("?qAddIn_func"),
XLL_LPXLOPERX   
XLL_HANDLE,     
_T("QANT_Func"),
_T("Handle"),           
_T("QA"),           
_T("Info here..")
);
LPXLOPERX WINAPI
qAddIn_func(HANDLEX hx)
{
#pragma XLLEXPORT
static OPERX x;

A* ptrA = h2p<A>(hx);

    // the pointer:  mPtrX in the class A has the correct memory address, but no contents.

    x = 0;
return &x;
}
Coordinator
Apr 30, 2015 at 4:59 PM
Your vector vX is getting is getting destroyed. Use a vector instead of a pointer in your class.
May 1, 2015 at 10:50 AM
Thanks Keith