public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
* Re: Breakpoints in constructor/destructor
@ 2003-06-24  2:58 Michael Elizabeth Chastain
  0 siblings, 0 replies; 3+ messages in thread
From: Michael Elizabeth Chastain @ 2003-06-24  2:58 UTC (permalink / raw)
  To: gdb, mjeng

I can shed a little light ...

When gcc compiles any constructor or any destructor, it generates *two*
functions in the object code (sometimes three, but I have never seen
three).  This is part of the implementation of virtual base classes in
C++.

These two object code functions have different names for the linker (or
else the program wouldn't even link properly), but gdb thinks that these
functions have the same name, and confusion ensues.  You set a
breakpoint -- it goes into one copy of the function -- but not the
other.  And then your program executes the other one.

Try this: compile your program with "-S" to get assembly code.
Then read the assembly code.  You will see two copies of
'MyNumber::MyNumber' and 'MyNumber::~MyNumber'.  To make the assembly
code easier to visualize, you can call 'APointLessFunction' in the
constructor, and then you can find two object-code functions with calls
to 'APointLessFunction'.

This won't help you set breakpoints in the constructor, but I hope it
will help explain gdb's behavior.

Michael C

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Breakpoints in constructor/destructor
  2003-06-23 19:57 MONWHEA JENG
@ 2003-06-23 20:11 ` Daniel Jacobowitz
  0 siblings, 0 replies; 3+ messages in thread
From: Daniel Jacobowitz @ 2003-06-23 20:11 UTC (permalink / raw)
  To: MONWHEA JENG; +Cc: gdb

On Mon, Jun 23, 2003 at 02:56:33PM -0500, MONWHEA JENG wrote:
> 
> Hi,
> 
> 	I just downloaded gdb onto cygwin. I found it pretty
> wonderful, but there's one thing I'm completely baffled by. I
> can't seem to set breakpoints in constructors or destructors
> in my C++ programs. I wrote a simple program that does nothing but
> create and destroy a simple object. When I step through it, I can see
> my cout statements in the constructors and destructors being called.
> But if I set breakpoints at these lines, the gdb doesn't stop there.
> The program is reproduced below, but it's pretty simple---there's
> not much to see. I read the gdb manual, but saw no mention of 
> any special issues related to constructors and destructors. And
> I see that there was a thread earlier this year on "breakpoints
> in constructors," but unfortunately, I found it incomprehensible.
> 
> 	Any help would be appreciated. Or, any suggestion on how
> I could have figured this out on my own, from the gdb manual
> or other source. 

Neither, sorry - this is still an open problem, and we're still trying
to find a good solution.

-- 
Daniel Jacobowitz
MontaVista Software                         Debian GNU/Linux Developer

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Breakpoints in constructor/destructor
@ 2003-06-23 19:57 MONWHEA JENG
  2003-06-23 20:11 ` Daniel Jacobowitz
  0 siblings, 1 reply; 3+ messages in thread
From: MONWHEA JENG @ 2003-06-23 19:57 UTC (permalink / raw)
  To: gdb


Hi,

	I just downloaded gdb onto cygwin. I found it pretty
wonderful, but there's one thing I'm completely baffled by. I
can't seem to set breakpoints in constructors or destructors
in my C++ programs. I wrote a simple program that does nothing but
create and destroy a simple object. When I step through it, I can see
my cout statements in the constructors and destructors being called.
But if I set breakpoints at these lines, the gdb doesn't stop there.
The program is reproduced below, but it's pretty simple---there's
not much to see. I read the gdb manual, but saw no mention of 
any special issues related to constructors and destructors. And
I see that there was a thread earlier this year on "breakpoints
in constructors," but unfortunately, I found it incomprehensible.

	Any help would be appreciated. Or, any suggestion on how
I could have figured this out on my own, from the gdb manual
or other source. 

Momo




------------ Program below ------------

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

// ----- START OF CLASS MyNumber -----

class MyNumber{
public:
	MyNumber(int y);
	~MyNumber();
private:
	int x;
};

MyNumber::MyNumber(int y)
{
   cout << "\nConstructor called with number " << y << endl;
   x=y;
}

MyNumber::~MyNumber()
{
   cout << "\nDestructor called for number" << x << endl;
}

// ----- END OF CLASS MyNumber -----

void APointlessFunction(void)
{
   MyNumber c(42);
}

int main()
{
   MyNumber a(100);
   APointlessFunction();
   return 0;
}

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-06-24  2:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-06-24  2:58 Breakpoints in constructor/destructor Michael Elizabeth Chastain
  -- strict thread matches above, loose matches on Subject: below --
2003-06-23 19:57 MONWHEA JENG
2003-06-23 20:11 ` Daniel Jacobowitz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).