Game Development Community

Copying Argv values is driving me crazy

by Andy Rollins · in Torque Game Engine · 06/05/2007 (10:59 pm) · 2 replies

Allo - hopefully one of you lovely people here can tell me where I am being stupid here, C++ is very rusty these days and I'm tearing my hair out (the hair I have left anyway)...

Ok so I have two different classes a request class that takes a query in from script, posts an event which is processed and then the reply class is created before a Con::execute() call is made to pass the results back to script.

Now I need to store the parameters passed in so that they get pushed all the way through and eventually back to the script again (via the con::execute call).

I've defined them both as:
char **mArgv;

the request values are set correctly, I've debugged and checked but when I try and perform a memcpy as below nothing gets moved over to the reply.

dMemcpy(reply.mArgv, pRequest->mArgv, sizeof(pRequest->mArgv));

is that the right way to be doing this? if so what am I missing?

Thanks in advance, Andy

#1
06/06/2007 (12:17 am)
I think all you are copying in your example is the original pointer to pRequest->mArgv, not the actual data in the array.
It's equivalent to:
reply.mArgv = pRequest->mArgv;
The passed-in array might be getting deleted (or cleared) somewhere outside your current method.

I'd copy each argument individually, so you can copy the actual data in the arguments. Something like this:
//argc needs to be set to the total number of arguments
for( int i = 0; i < argc; i++ )
{
   FrameAllocatorMarker frameAlloc;
   reply.mArgv[i] = (char *) frameAlloc.alloc(dStrlen(pRequest->mArgv[i]) + 1 );
   dStrcpy( (char*)reply.mArgv[i], (char*)pRequest->mArgv[i]);
}
This way your reply.mArgv doesn't have a dependence on the passed in array pointer (pRequest->mArgv) sticking around.
#2
06/07/2007 (11:05 am)
Thanks Brian - I'd tried that before and no luck with it either - it kept crashing the engine whilst using the dStrlen function, actually I am thinking it's quite possibly to do with the fact I'm trying to do this as part of the event-driven database resource on here which runs multi-threaded - have a feeling that was what was crashing the engine but my knowledge of thread-safety and re-entrant functions is sparse to say the least.

Thanks for the reply though