Board index » cppbuilder » automation object referenced via pointer problems

automation object referenced via pointer problems

Hello,

I have a set of classes which operate on an automation object. All those
classes use one name to reference the automation object (via reference).
However, it turned out that I need to use those classes on more than one
object (it's a database helper, which binds to specified directory
(database) and operates on it, and I have to use two databases
simultaneously). So I changed the reference to a pointer in all the
classes, so I could assign different pointers to objects to it, and the
classes would work with the current object.

However, things mess up terribly in this scenario. First, I created my
objects in a regular, non-dynamic way (without the 'new' operator), and
I tried to assign their addresses to the referencing pointers:

IServerDisp* server;

IServerDisp obj1,obj2;
//bind default, etc.
server=(IServerDisp*)&obj1;

With this solution my program crashed every time I wanted to use any of
the functions provided by the classes (I've tested them all with the
reference, and they work with no problems). So I changed it to this:

IServerDisp* obj1,* obj2,* server;

obj1=new IServerDisp();
obj2=new IServerDisp();

server=obj1;

This causes different problems. It looks like one instance works fine -
e.g. server=obj1;
server->do_something();    //works

But after I switch to the other one (server=obj2;), it crashes in the
IsBound function, saying that there was access violation, presumably
when accessing the m_Dispatch member of the object.

If you know what might be the solution (or the cause) of this, please,
let me know.
Thank you,

Martin

 

Re:automation object referenced via pointer problems


I have traced the lines a little deeper - and this is what happens:

server->IsBound();    //here server has a legitimate value, a non-NULL
pointer

Inside the IsBound, though, the 'this' value is NULL. I don't quite
understand how can this be - if I call the server->IsBound, then inside the
class the 'this' value should be equal to 'server' value. It's not, however,
and I don't know where could it be changed ?

Martin

Re:automation object referenced via pointer problems


Never mind - found the problem.

Other Threads