Board index » cppbuilder » Data Braekpoints

Data Braekpoints


2007-05-24 05:43:10 AM
cppbuilder19
Dear,
I' am using BCB6, Update 4.
My application is multi-thread.
In a method of a my class, I use an (Ansi)String (allocated on the stack,
that is without "new")
and sometimes I gets an Access Violation on the end of the method.
I could see, analyzing some run step by step, that it is because some part
of my metohd (or some other thread) changes
the char * associated to the String. I could see that such char*, in
practice, can be obtained by *(int*)(&MyString).
In practice it is the value written on the stack and in fact i could see
that it is corrupted (it becames 0x0E instead of
somthing like 0xAE9140).
To find out what part of my code generates the problem, I tried to use a
Data Breakpoint.
First I used a Code Braekpoint to find out the location on the stack and
then I set the properties
of my Data Breakpoint to refers to that location.
I also used an int variable that is 1 when the run is inside my method.
I used the value of such int variable in the condition associated to the
Data Breakpoint.
I did it because that stack location could be used also other times while
the run.
My problem is that i do not see the Data Breakpoint working. I mean, also if
i use an always "true" condition,
the Data breakpoint never stops me when that location is written.
Is it because of the multi-threading?
Really, the IDE gives a me a message, about the possibility to make the
system unstable by using a Data Breakpoint
on a stack location. I thought I could avoid the problems by using that int
variable which I have explained above.
Note that the Access violation happens only in some of the times that method
is executed and so I can not
just debug it step by step. I need a method to stop the whole execution just
whn the stack location is
corrupted. I used the condition (MyIntVariable == 1) &&
(*AddressOfMyStackLocation < 100). In fact,
as I told you above, when the problem happens, typically the value on the
stack location is 0x0E or, however,
a very little value.
Some one can help me?
Regards,
Mauro Russo.
 
 

Re:Data Braekpoints

...Please
someone helping me?
I really need very much to understand what gets corruption of my stack
variable.
Thank you.
"mauro" < XXXX@XXXXX.COM >ha scritto nel messaggio
Quote
Dear,

I' am using BCB6, Update 4.
My application is multi-thread.

In a method of a my class, I use an (Ansi)String (allocated on the stack,
that is without "new")
and sometimes I gets an Access Violation on the end of the method.
I could see, analyzing some run step by step, that it is because some part
of my metohd (or some other thread) changes
the char * associated to the String. I could see that such char*, in
practice, can be obtained by *(int*)(&MyString).
In practice it is the value written on the stack and in fact i could see
that it is corrupted (it becames 0x0E instead of
somthing like 0xAE9140).
To find out what part of my code generates the problem, I tried to use a
Data Breakpoint.
First I used a Code Braekpoint to find out the location on the stack and
then I set the properties
of my Data Breakpoint to refers to that location.
I also used an int variable that is 1 when the run is inside my method.
I used the value of such int variable in the condition associated to the
Data Breakpoint.
I did it because that stack location could be used also other times while
the run.

My problem is that i do not see the Data Breakpoint working. I mean, also
if i use an always "true" condition,
the Data breakpoint never stops me when that location is written.

Is it because of the multi-threading?
Really, the IDE gives a me a message, about the possibility to make the
system unstable by using a Data Breakpoint
on a stack location. I thought I could avoid the problems by using that
int variable which I have explained above.

Note that the Access violation happens only in some of the times that
method is executed and so I can not
just debug it step by step. I need a method to stop the whole execution
just whn the stack location is
corrupted. I used the condition (MyIntVariable == 1) &&
(*AddressOfMyStackLocation < 100). In fact,
as I told you above, when the problem happens, typically the value on the
stack location is 0x0E or, however,
a very little value.

Some one can help me?

Regards,
Mauro Russo.

 

Re:Data Braekpoints

Dear Bob Gonder,
Can you help me?
Thanks for your help.
"mauro" < XXXX@XXXXX.COM >ha scritto nel messaggio
Quote
Dear,

I' am using BCB6, Update 4.
My application is multi-thread.

In a method of a my class, I use an (Ansi)String (allocated on the stack,
that is without "new")
and sometimes I gets an Access Violation on the end of the method.
I could see, analyzing some run step by step, that it is because some part
of my metohd (or some other thread) changes
the char * associated to the String. I could see that such char*, in
practice, can be obtained by *(int*)(&MyString).
In practice it is the value written on the stack and in fact i could see
that it is corrupted (it becames 0x0E instead of
somthing like 0xAE9140).
To find out what part of my code generates the problem, I tried to use a
Data Breakpoint.
First I used a Code Braekpoint to find out the location on the stack and
then I set the properties
of my Data Breakpoint to refers to that location.
I also used an int variable that is 1 when the run is inside my method.
I used the value of such int variable in the condition associated to the
Data Breakpoint.
I did it because that stack location could be used also other times while
the run.

My problem is that i do not see the Data Breakpoint working. I mean, also
if i use an always "true" condition,
the Data breakpoint never stops me when that location is written.

Is it because of the multi-threading?
Really, the IDE gives a me a message, about the possibility to make the
system unstable by using a Data Breakpoint
on a stack location. I thought I could avoid the problems by using that
int variable which I have explained above.

Note that the Access violation happens only in some of the times that
method is executed and so I can not
just debug it step by step. I need a method to stop the whole execution
just whn the stack location is
corrupted. I used the condition (MyIntVariable == 1) &&
(*AddressOfMyStackLocation < 100). In fact,
as I told you above, when the problem happens, typically the value on the
stack location is 0x0E or, however,
a very little value.

Some one can help me?

Regards,
Mauro Russo.

 

{smallsort}

Re:Data Braekpoints

mauro wrote:
Quote
Dear Bob Gonder,
I've never run BCB6.
I don't think I've ever tried to breakpoint stack memory.
Quote
>In a method of a my class, I use an (Ansi)String (allocated on the stack,
>that is without "new")
You have a string on the stack. What do you have on the stack _after_
the string? You might be overwritting the string.
Quote
>that it is corrupted (it becames 0x0E instead of
What is the actual string in the AnsiString?
Is it 14 characters long?
Quote
>and sometimes I gets an Access Violation on the end of the method.
What are you doing at the end of the method when this violation
happens?
 

Re:Data Braekpoints

"Bob Gonder" < XXXX@XXXXX.COM >wrote in message
Quote
mauro wrote:

>Dear Bob Gonder,

I've never run BCB6.
I don't think I've ever tried to breakpoint stack memory.

>>In a method of a my class, I use an (Ansi)String (allocated on the
>>stack,
>>that is without "new")

You have a string on the stack. What do you have on the stack _after_
the string? You might be overwritting the string.

>>that it is corrupted (it becames 0x0E instead of

What is the actual string in the AnsiString?
Is it 14 characters long?

>>and sometimes I gets an Access Violation on the end of the method.

What are you doing at the end of the method when this violation
happens?
Maybe he could just show us the function? Sounds like he's using
c_str() from an AnsiString after the AnsiString is gone. It's hard to
tell though with the "hints" about threads and such.
 

Re:Data Braekpoints

mauro wrote:
Quote
In a method of a my class, I use an (Ansi)String (allocated on the stack,
that is without "new")
and sometimes I gets an Access Violation on the end of the method.
Can you give us the entire code of the method?
Jon
 

Re:Data Braekpoints

"Jonathan Benedicto" wrote:
Quote
>In a method of a my class, I use an (Ansi)String (allocated on the stack,
>that is without "new")
>and sometimes I gets an Access Violation on the end of the method.
Can you give us the entire code of the method?
If you want I can.
Really I should give the whole class.
But I repeat that my problem is that the Data Breakpoint seems to don't
work.
If it does, i can find out who corrupts that stack value.
Quote
Jon
 

Re:Data Braekpoints

"Duane Hebert" wrote:
Quote

Maybe he could just show us the function? Sounds like he's using
c_str() from an AnsiString after the AnsiString is gone. It's hard to
tell though with the "hints" about threads and such.
It is not that. The String is an automatica variable and so its destruction
is added
by the compiler at the end of its scope.
I could verify the problem by looking the run in disassembly mode.
Note that my application is multi-thread and the acces violation happends
only sometimes.
When it does not happend, the c_str() value is correct.
When it happends, I can note the c_str() value became 0x0E.
I need the Data Breakpoint working correctly to find out what corrupts that
value,
but I am having problems in using the Data Breakpoint. It seems to don't
work.
Thanks for your help.
 

Re:Data Braekpoints

Quote
>Dear Bob Gonder,

I've never run BCB6.
I don't think I've ever tried to breakpoint stack memory.

>>In a method of a my class, I use an (Ansi)String (allocated on the
>>stack,
>>that is without "new")
You have a string on the stack. What do you have on the stack _after_
the string? You might be overwritting the string.
No, I do not overwrite the String. However, in this occasion I looked the
disassembled
code implementation for the String type. It is just a pointer [equivalent to
the result of c_str() method]
which points to the the byte where the first character is saved. I noted
that the implementation
tries to keep only one copy among equivalent strings. In fact, the two bytes
before the first charater
keep informations to manage this sharing.
I described this only to explain why I can surely tell you that the problem
is not about overwriting a string.
Moreover I just write this String in a statement as
MyString = FunctionReturningString();
at the beginning of my function.
Quote
>>that it is corrupted (it becames 0x0E instead of

What is the actual string in the AnsiString?
Is it 14 characters long?
No. As explained above that stack value represents the pointer to the first
char [allocated in the heap].
In fact, after the statement "MyString = FunctionReturningString();" I can
read the value with
debug windows and I can verify that the byte pointed and the following bytes
contain the correct char sequence.
Quote
>>and sometimes I gets an Access Violation on the end of the method.

What are you doing at the end of the method when this violation
happens?
My application is multi-thread. The access violation happends only sometime
and, when it happends,
I could note on the stack the value 0x0E instead that the correct pointer to
the first char [I saved it in a more variable to better debug the code].
The Access Violation is because the AnsiString destructor tries to
manage the char sequences sharing and try to access the two bytes before
that that having address 0x0E [instead of that correct being in the heap
memory].
I just wanted to use the Data Breakpoint to find out who writes on the stack
location. I wanted
to use some variable and a breakpoint-condition to be sure that the
breakpoint breaks the execution
only while that function is running.
My problem is that the Data Breakpoint does not work, that is [also if I use
a "true" condition] the
code is never broken, neither when the function self writes in that stack
location.
Thank you for your help.
 

Re:Data Braekpoints

"mauro" < XXXX@XXXXX.COM >wrote in message
Quote

"Duane Hebert" wrote:
>
>Maybe he could just show us the function? Sounds like he's using
>c_str() from an AnsiString after the AnsiString is gone. It's hard to
>tell though with the "hints" about threads and such.

It is not that. The String is an automatica variable and so its
destruction is added
by the compiler at the end of its scope.
I could verify the problem by looking the run in disassembly mode.
Note that my application is multi-thread and the acces violation happends
only sometimes.
When it does not happend, the c_str() value is correct.
When it happends, I can note the c_str() value became 0x0E.

Yes but c_str() is a temporary. If the AnsiString gets destroyed
then I would expect just what you describe. If the AnsiString is in
another thread from where you're using the temporary c_str(), this
isn't going to work.
Quote
I need the Data Breakpoint working correctly to find out what corrupts
that value,
but I am having problems in using the Data Breakpoint. It seems to don't
work.
I have no idea why your breakpoint isn't working.
My guess would be that the AnsiString is gone and you're c_str() is trashed.
Try this, for example:
AnsiString a("hello");
char* ptr = a.c_str();
a = "";
ptr[2] = 'q';
//inspect ptr here...
Quote

Thanks for your help.
You're going to need to show some code to give us a chance
to help you. The best would be to show the function that you're
having problems with for starters.
 

Re:Data Braekpoints

mauro wrote:
Quote
"Jonathan Benedicto" wrote:
>>In a method of a my class, I use an (Ansi)String (allocated on the stack,
>>that is without "new")
>>and sometimes I gets an Access Violation on the end of the method.

>Can you give us the entire code of the method?

If you want I can.
Really I should give the whole class.

But I repeat that my problem is that the Data Breakpoint seems to don't
work.
If it does, i can find out who corrupts that stack value.

>Jon


Mauro,
I haven't tested BCB6 but a Data breakpoint does work in BDS 20006. I
created a simple test project to demonstrate.
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int EnableBreak = 0;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// assign a value to a string
AnsiString s = "test";
// enable the data breakpoint
EnableBreak = 1;
// get the address of the string variable
AnsiString* p = &s;
// get the address of the string data
const char* c = s.c_str();
// some dummy code to break on
// when we break here we can set a data breakpoint on the
// address of the string i.e. p
Label1->Caption = s;
// write to the address of the string
// this should trigger the data breakpoint
*reinterpret_cast<int*>(p) = 0x0E;
// more dummy code to see if we break before destructor is called
Label1->Caption = "dummy";
// disable the data breakpoint condition variable
EnableBreak = 0;
// the string should be destroyed here
}
//---------------------------------------------------------------------------
I set a normal source break on the first label update. And then while
broken I set a data breakpoint of the address pointed to by p. I also
have a source break on the closing brace to stop the program in case the
data breakpoint doesn't fire. I then used F9 to continue running. The
event log below shows the first source break, and the subsequent data
breakpoint.
Source Breakpoint at $00401C21: C:\Documents and Settings\DennisC\My
Documents\Borland Studio Projects\test_data_breakpoint\Unit1.cpp line
38. Process Project1.exe (3668)
Data Breakpoint at $00401C3B: Object at address: $0012F550. Length: 4.
Process Project1.exe (3668)
I am going to modify this program to test that it triggers on a write
from another thread as well.
HTH
Dennis Cote
 

Re:Data Braekpoints

Hmm...
I also could use a Data Breakpoint in a little project built by BCB6.
But it does not work on my project.
I am going to see your code.
Thanks for help.
"Dennis Cote" < XXXX@XXXXX.COM >ha scritto nel messaggio
Quote
mauro wrote:
>"Jonathan Benedicto" wrote:
>>>In a method of a my class, I use an (Ansi)String (allocated on the
>>>stack, that is without "new")
>>>and sometimes I gets an Access Violation on the end of the method.
>
>>Can you give us the entire code of the method?
>
>If you want I can.
>Really I should give the whole class.
>
>But I repeat that my problem is that the Data Breakpoint seems to don't
>work.
>If it does, i can find out who corrupts that stack value.
>
>>Jon
>
>

Mauro,

I haven't tested BCB6 but a Data breakpoint does work in BDS 20006. I
created a simple test project to demonstrate.

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

int EnableBreak = 0;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
// assign a value to a string
AnsiString s = "test";

// enable the data breakpoint
EnableBreak = 1;

// get the address of the string variable
AnsiString* p = &s;

// get the address of the string data
const char* c = s.c_str();

// some dummy code to break on
// when we break here we can set a data breakpoint on the
// address of the string i.e. p
Label1->Caption = s;

// write to the address of the string
// this should trigger the data breakpoint
*reinterpret_cast<int*>(p) = 0x0E;

// more dummy code to see if we break before destructor is called
Label1->Caption = "dummy";

// disable the data breakpoint condition variable
EnableBreak = 0;

// the string should be destroyed here
}
//---------------------------------------------------------------------------



I set a normal source break on the first label update. And then while
broken I set a data breakpoint of the address pointed to by p. I also have
a source break on the closing brace to stop the program in case the data
breakpoint doesn't fire. I then used F9 to continue running. The event log
below shows the first source break, and the subsequent data breakpoint.

Source Breakpoint at $00401C21: C:\Documents and Settings\DennisC\My
Documents\Borland Studio Projects\test_data_breakpoint\Unit1.cpp line 38.
Process Project1.exe (3668)
Data Breakpoint at $00401C3B: Object at address: $0012F550. Length: 4.
Process Project1.exe (3668)

I am going to modify this program to test that it triggers on a write from
another thread as well.

HTH
Dennis Cote

 

Re:Data Braekpoints

Well, I read your code.
I did not try yet to produce a demo application to
check if the Data Breakpoint when the writing is done by an other thread,
but really in my application the Data Breakpoint does not work also while
the
running of the thread generating the Access Violation.
However I will wait your results.
Very thanks for your help.
"Dennis Cote" < XXXX@XXXXX.COM >ha scritto nel messaggio
Quote
mauro wrote:
>"Jonathan Benedicto" wrote:
>>>In a method of a my class, I use an (Ansi)String (allocated on the
>>>stack, that is without "new")
>>>and sometimes I gets an Access Violation on the end of the method.
>
>>Can you give us the entire code of the method?
>
>If you want I can.
>Really I should give the whole class.
>
>But I repeat that my problem is that the Data Breakpoint seems to don't
>work.
>If it does, i can find out who corrupts that stack value.
>
>>Jon
>
>

Mauro,

I haven't tested BCB6 but a Data breakpoint does work in BDS 20006. I
created a simple test project to demonstrate.

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

int EnableBreak = 0;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
// assign a value to a string
AnsiString s = "test";

// enable the data breakpoint
EnableBreak = 1;

// get the address of the string variable
AnsiString* p = &s;

// get the address of the string data
const char* c = s.c_str();

// some dummy code to break on
// when we break here we can set a data breakpoint on the
// address of the string i.e. p
Label1->Caption = s;

// write to the address of the string
// this should trigger the data breakpoint
*reinterpret_cast<int*>(p) = 0x0E;

// more dummy code to see if we break before destructor is called
Label1->Caption = "dummy";

// disable the data breakpoint condition variable
EnableBreak = 0;

// the string should be destroyed here
}
//---------------------------------------------------------------------------



I set a normal source break on the first label update. And then while
broken I set a data breakpoint of the address pointed to by p. I also have
a source break on the closing brace to stop the program in case the data
breakpoint doesn't fire. I then used F9 to continue running. The event log
below shows the first source break, and the subsequent data breakpoint.

Source Breakpoint at $00401C21: C:\Documents and Settings\DennisC\My
Documents\Borland Studio Projects\test_data_breakpoint\Unit1.cpp line 38.
Process Project1.exe (3668)
Data Breakpoint at $00401C3B: Object at address: $0012F550. Length: 4.
Process Project1.exe (3668)

I am going to modify this program to test that it triggers on a write from
another thread as well.

HTH
Dennis Cote

 

Re:Data Braekpoints

Something about my problem:
All my threads access to a map<int,String>and get strings from this map.
They receive copies of the strings by a function where the map is accessed
inside an
Acquire()-Leave() block of a TCriticalSection.
I believe that who wrote the implementation of the AnsiString method was
carefull to keep it
working in these conditions. I mean: as I noted by debuggning the
disassembly code, the
implementation tries to share equivalent char arrays. Of course, if I did
not study the
disassembly, I could not know that and, for me (as user of AnsiString), if I
generate a copy of the String,
for me it is something 'different' from the original String.
However, by looking the disassembly code, I could see that the two bytes
(used to manage the
char arrays sharing) are managed by assembly instruction wich 'lock' the
variables.
For example the first of these two bytes is a counter of how many String are
sharing that char array
and it is decremented by a "dec lock" assembly instruction while the
~AnsiString destructor.
Some of you have some motivation to think that I should not trust the
AnsiString implementation
working when an application use String copies among different threads?
"mauro" < XXXX@XXXXX.COM >ha scritto nel messaggio
Quote
Dear,

I' am using BCB6, Update 4.
My application is multi-thread.

In a method of a my class, I use an (Ansi)String (allocated on the stack,
that is without "new")
and sometimes I gets an Access Violation on the end of the method.
I could see, analyzing some run step by step, that it is because some part
of my metohd (or some other thread) changes
the char * associated to the String. I could see that such char*, in
practice, can be obtained by *(int*)(&MyString).
In practice it is the value written on the stack and in fact i could see
that it is corrupted (it becames 0x0E instead of
somthing like 0xAE9140).
To find out what part of my code generates the problem, I tried to use a
Data Breakpoint.
First I used a Code Braekpoint to find out the location on the stack and
then I set the properties
of my Data Breakpoint to refers to that location.
I also used an int variable that is 1 when the run is inside my method.
I used the value of such int variable in the condition associated to the
Data Breakpoint.
I did it because that stack location could be used also other times while
the run.

My problem is that i do not see the Data Breakpoint working. I mean, also
if i use an always "true" condition,
the Data breakpoint never stops me when that location is written.

Is it because of the multi-threading?
Really, the IDE gives a me a message, about the possibility to make the
system unstable by using a Data Breakpoint
on a stack location. I thought I could avoid the problems by using that
int variable which I have explained above.

Note that the Access violation happens only in some of the times that
method is executed and so I can not
just debug it step by step. I need a method to stop the whole execution
just whn the stack location is
corrupted. I used the condition (MyIntVariable == 1) &&
(*AddressOfMyStackLocation < 100). In fact,
as I told you above, when the problem happens, typically the value on the
stack location is 0x0E or, however,
a very little value.

Some one can help me?

Regards,
Mauro Russo.

 

Re:Data Braekpoints

mauro wrote:
Quote
"Jonathan Benedicto" wrote:
>>In a method of a my class, I use an (Ansi)String (allocated on the stack,
>>that is without "new")
>>and sometimes I gets an Access Violation on the end of the method.

>Can you give us the entire code of the method?

If you want I can.
Really I should give the whole class.

But I repeat that my problem is that the Data Breakpoint seems to don't
work.
If it does, i can find out who corrupts that stack value.

>Jon


Mauro,
When I changed the test program so that the write was done from a
different thread, the data breakpoint did not work! :-(
I could see that the string variable had been over written by the the
code in the other thread, but the data breakpoint didn't fire.
It looks like you will have to find another way to locate the source of
your stack overwrite. I wish you luck, those are {*word*193} bugs to find.
HTH
Dennis Cote