J
Jamie Burns
Hello,
I am writing a client / server application. There is 1 server, and many
clients. The server processes requests from each client, and typically
creates and manipulates C++ objects on their behalf.
Now, when a client requests for an object to be created, I pass back a
pointer to the object (from server memory address scope) as a "long
integer". To the client, this is just an ID for the object they wish to
access. From then on, when clients wish an operation to be performed on an
object, they pass back the appropriate "long integer" and the manipulation
they wish to perform. The server then casts the "long integer" into the
object it expects, and performs the manipulation.
This is working quite well, only it is obviously quite dangerous. If a
client was to pass back a different "long integer", and this was casted into
an object, and this object was accessed, the application would crash hard or
potentially exhibit strangeness from memory corruption.
How can I get my "long integer" back into an object in a safe manner? I
tried dynamic_cast but it wouldn't work.
Quick example of what I mean:
------------------
class A {
// ...
virtual foo() {}
}
A* a = new A();
long objectID = (long) a;
A* a2 = dynamic_cast<A*>((void *) objectID);
I am writing a client / server application. There is 1 server, and many
clients. The server processes requests from each client, and typically
creates and manipulates C++ objects on their behalf.
Now, when a client requests for an object to be created, I pass back a
pointer to the object (from server memory address scope) as a "long
integer". To the client, this is just an ID for the object they wish to
access. From then on, when clients wish an operation to be performed on an
object, they pass back the appropriate "long integer" and the manipulation
they wish to perform. The server then casts the "long integer" into the
object it expects, and performs the manipulation.
This is working quite well, only it is obviously quite dangerous. If a
client was to pass back a different "long integer", and this was casted into
an object, and this object was accessed, the application would crash hard or
potentially exhibit strangeness from memory corruption.
How can I get my "long integer" back into an object in a safe manner? I
tried dynamic_cast but it wouldn't work.
Quick example of what I mean:
------------------
class A {
// ...
virtual foo() {}
}
A* a = new A();
long objectID = (long) a;
A* a2 = dynamic_cast<A*>((void *) objectID);