www.digitalmars.com         C & C++   DMDScript  

c++.stlsoft - VOLE - dereferencing of out-of-scope object

reply Duncan McQueen <dwmcqueen gmail.com> writes:
have an object that is return from an invoke_method that needs to be
put globalling in a dll I am working on.

After declaring a global like this:

object *g_pTradeDesk2a = NULL;


I basically do this:

object g_pTradeDesk2 = oCore.invoke_method<object>(L"CreateTradeDesk",
L"trader");

g_pTradeDesk2.get()->AddRef(); // VERY IMPORTANT!

g_pTradeDesk2a = &g_pTradeDesk2; //assign pointer

However, future references to the g_pTradeDesk2a variable fail with an
access violation.

From what I can gather, it seems that the objects returned from
invoke_method function hold weak references and deallocate themselves
no matter what when they go out of scope.  Is there a good way around
this to keep that object allocated?
Sep 21 2009
parent "Matt Wilson" <matthewwilson acm.org> writes:
"Duncan McQueen" <dwmcqueen gmail.com> wrote in message 
news:h97qib$21og$1 digitalmars.com...
 have an object that is return from an invoke_method that needs to be
 put globalling in a dll I am working on.

 After declaring a global like this:

 object *g_pTradeDesk2a = NULL;


 I basically do this:

 object g_pTradeDesk2 = oCore.invoke_method<object>(L"CreateTradeDesk",
 L"trader");

 g_pTradeDesk2.get()->AddRef(); // VERY IMPORTANT!

 g_pTradeDesk2a = &g_pTradeDesk2; //assign pointer

 However, future references to the g_pTradeDesk2a variable fail with an
 access violation.

 From what I can gather, it seems that the objects returned from
 invoke_method function hold weak references and deallocate themselves
 no matter what when they go out of scope.  Is there a good way around
 this to keep that object allocated?

First, sorry for the late reply. It's highly likely this answer's well past being useful to the solution, but at it'll explain the problem The problem is that you're attempting to hold a pointer to an instance of the *facade*, which has a lifetime limited by the laws of C++. I think what you're trying to do is hold onto the underlying COM object regardless of what happens to the facade instance. This requires a different approach. btw, holding globals is almost always a bad idea. I'd suggest you find a better way of sharing the trading desk instance. HTH Matt
May 23 2010