Board index » cppbuilder » Assignment not working correctly

Assignment not working correctly


2006-02-26 05:37:38 AM
cppbuilder18
Hi there,
Not sure if this is the correct newsgroup for this one. I've got an application in Borland C++Builder 5, and when I run a component I've written, it doesn't seem to be correctly assigning a variable! The code looks something like:
bool TControl::Method(AnsiString var1) {
var2 = var1;
return true;
}
When I trace through the above with the de{*word*81}, I can see the right info coming into the "var1" variable in the function parameter, but when I assign that var1 to var2, var2 is remaining unchanged.
I've spent about four hours trying to pull this code apart, but it's driving me round the bend! Any idea why this might not be working, or what I could try to find the bug?
Many thanks in advance,
Russ
 
 

Re:Assignment not working correctly

On 25 Feb 2006 14:37:38 -0700, Russell Allen wrote:
Quote
bool TControl::Method(AnsiString var1) {
var2 = var1;
return true;
}
where is var2 defined?
--
liz
 

Re:Assignment not working correctly

At 22:37:38, 25.02.2006, Russell Allen wrote:
Quote
bool TControl::Method(AnsiString var1) {
var2 = var1;
return true;
}

When I trace through the above with the de{*word*81}, I can see the right
info coming into the "var1" variable in the function parameter, but
when I assign that var1 to var2, var2 is remaining unchanged.
If var2 is not used anywhere else, it could be the optimizer simply
ignores the code. What is var2?
--
Rudy Velthuis [TeamB] rvelthuis.de/
"Humor is always based on a modicum of truth. Have you ever heard a joke
about a father-in-law?" --{*word*106} Clark
 

{smallsort}

Re:Assignment not working correctly

"Rudy Velthuis [TeamB]" < XXXX@XXXXX.COM >wrote:
Quote
At 22:37:38, 25.02.2006, Russell Allen wrote:

>bool TControl::Method(AnsiString var1) {
>var2 = var1;
>return true;
>}
>
>When I trace through the above with the de{*word*81}, I can see the right
>info coming into the "var1" variable in the function parameter, but
>when I assign that var1 to var2, var2 is remaining unchanged.

If var2 is not used anywhere else, it could be the optimizer simply
ignores the code. What is var2?
--
Rudy Velthuis [TeamB] rvelthuis.de/

"Humor is always based on a modicum of truth. Have you ever heard a joke
about a father-in-law?" --{*word*106} Clark
var2 is defined as a public variable within the component, so the header file is something like:
TControl {
public:
AnsiString var2;
bool Method(AnsiString var1);
}
Hope that helps, really I'm wondering if there's any de{*word*81} tool I can use to watch the actual memory that variable is (allegedly) occupying?
Russ
 

Re:Assignment not working correctly

Isn't TControl a Borland object or have you derived your control off it ?
 

Re:Assignment not working correctly

"Russell Allen" < XXXX@XXXXX.COM >writes:
Quote
Hi there,

Not sure if this is the correct newsgroup for this one. I've got an
application in Borland C++Builder 5, and when I run a component I've
written, it doesn't seem to be correctly assigning a variable! The
code looks something like:

bool TControl::Method(AnsiString var1) {
var2 = var1;
return true;
}

When I trace through the above with the de{*word*81}, I can see the
right info coming into the "var1" variable in the function
parameter, but when I assign that var1 to var2, var2 is remaining
unchanged.
Not to ask the obvious, but are you sure that Method is exactly the
code you posted? Perhaps you accidently are doing something like
this:
bool TControl::Method(AnsiString var1) {
AnsiString var2 = var1; // OOPS
return true;
}
This declares a local var2 that receives var1 for its
initialization... and doesn't touch the member.
--
Chris (TeamB);
 

Re:Assignment not working correctly

Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >wrote:
Quote
"Russell Allen" < XXXX@XXXXX.COM >writes:

>Hi there,
>
>Not sure if this is the correct newsgroup for this one. I've got an
>application in Borland C++Builder 5, and when I run a component I've
>written, it doesn't seem to be correctly assigning a variable! The
>code looks something like:
>
>bool TControl::Method(AnsiString var1) {
>var2 = var1;
>return true;
>}
>
>When I trace through the above with the de{*word*81}, I can see the
>right info coming into the "var1" variable in the function
>parameter, but when I assign that var1 to var2, var2 is remaining
>unchanged.

Not to ask the obvious, but are you sure that Method is exactly the
code you posted? Perhaps you accidently are doing something like
this:

bool TControl::Method(AnsiString var1) {
AnsiString var2 = var1; // OOPS
return true;
}

This declares a local var2 that receives var1 for its
initialization... and doesn't touch the member.

--
Chris (TeamB);
I've just checked, in the whole control there's only one declaration of the variable. When I'm tracing through in the de{*word*81}, I can see the variable evaluates to NULL, then after the assignment it's still NULL.
What actually happens behind the scenes when you assign one AnsiString to another? Is memory copied, are pointers reassigned, what happens?
Russ
 

Re:Assignment not working correctly

"Russell Allen" < XXXX@XXXXX.COM >writes:
Quote
I've just checked, in the whole control there's only one declaration
of the variable. When I'm tracing through in the de{*word*81}, I can
see the variable evaluates to NULL, then after the assignment it's
still NULL.
Could you show a complete section of code that we could run to
reproduce this behavior? Describing it is not reaching any
conclusions, and probably the error is something wrong in the code,
but you're not describing it--probably because it's subtle.
Quote
What actually happens behind the scenes when you assign one
AnsiString to another? Is memory copied, are pointers reassigned,
what happens?
It's reference counted, so the receiving string's internal pointer is
adjusted to point to the data, and the data's refcount is incremented.
Did you step into the assignment operation? If so, you should have
seen this. Are you sure you're not using == instead of =, or
something else that may make your code do something different than
what you claim?
--
Chris (TeamB);
 

Re:Assignment not working correctly

Russell Allen wrote:
Quote
Hi there,

Not sure if this is the correct newsgroup for this one. I've got an application in Borland C++Builder 5, and when I run a component I've written, it doesn't seem to be correctly assigning a variable! The code looks something like:

bool TControl::Method(AnsiString var1) {
var2 = var1;
return true;
}

When I trace through the above with the de{*word*81}, I can see the right info coming into the "var1" variable in the function parameter, but when I assign that var1 to var2, var2 is remaining unchanged.

I've spent about four hours trying to pull this code apart, but it's driving me round the bend! Any idea why this might not be working, or what I could try to find the bug?

Many thanks in advance,

Russ
Also this does happens to me . Probably of corrupted vcl60.csm and
vcl60.#01 files. Don't get annoyed. The de{*word*81} cant show but it is
doing the right. I suggest to delete this files. Build the project then
debug it. It will show, the correct values.
Sabetay