Board index » cppbuilder » Copying VCL Object at Runtime

Copying VCL Object at Runtime

Is it alright to copy VCL objects at runtime in this fashion (see last line
of code for copy operation):

TQuery* pA;
TQuery* pB;

// Create query A.
pA = new TQuery( NULL );

// Make the query cursor unidirectional.
pA->UniDirectional = true;

// Specify the database to be used by the query object.
pA->DatabaseName = "A Database";

// Create query B.
pB= new TQuery( NULL );
pB = pA;

 

Re:Copying VCL Object at Runtime


This is a memory leak, as you are allocating memory for a second object,
then assigning it's pointer to something else, thus you're losing your
pointer to that second object and cannot free it later.  What you are doing
is NOT copying objects, you're merely assigning pB to POINT to pA (remember
that you're using pointers when working with most VCL-based objects)

If you want to COPY pA's values to pB, you should use Assign(), ie:

// Create query B.
pB = new TQuery( NULL );
pB->Assign(pA);

Gambit

Quote
"Paul Machiaverna" <machi...@i-stat.com> wrote in message

news:3b4e142c$1_2@dnews...
Quote
> // Create query B.
> pB= new TQuery( NULL );
> pB = pA;

Re:Copying VCL Object at Runtime


Whoops!  What I meant was a dereference like so:
// Create query B.
pB= new TQuery( NULL );
*pB = *pA;

Of course, this is assuming that operator= is properly written to handle any
dynamic data member (pointers to buffers, etc.) copying.

Thanks for the tip about using the Assign method.  That's a great solution
since I actually have several other parameters that are set in the original
query object.  This will be copied 4 times for a total of 5 query objects.
Why so many?  I am performing intensive data processing that requires
accessing the values in database tables very frequently.  Hopefully, there
will be a performance increase from loading the data into datasets (TQuery
objects)  and reading the data from there.

Thank you for your help!
Paul

Quote
"Remy Lebeau" <gambi...@gte.net> wrote in message news:3b4e6268$1_1@dnews...
> If you want to COPY pA's values to pB, you should use Assign(), ie:

> // Create query B.
> pB = new TQuery( NULL );
> pB->Assign(pA);

> Gambit

Other Threads