Discussion:
CButton in a CView Problem
(too old to reply)
Roger Lakner
2005-12-18 01:39:47 UTC
Permalink
In my CView-derived class in my SDI app, I wish to embed a button. So,
in MyView.h I have:

CButton PassButton;

In MyView.cpp, I create it:

PassButton.Create(_T("Test"), WS_CHILD | WS_VISIBLE |
BS_DEFPUSHBUTTON, CRect(362,441,362+100,441+30), this, ID_PASSBUTTON);

I, of course, have a message map to a function that handles the button
press. Everything works fine, button displays, clicking on the button
starts the expected handler, etc., except that the data associated
with many of my other variables is now garbage. If I comment out the
Create statement -- no problems. Is this a faulty implementation of a
button on my part or a bug in the CButton code or something else? Any
ideas will be greatly appreciated.
I'm using Visual .NET 2003 on XP SP2.

Roger Lakner
Scott McPhillips [MVP]
2005-12-18 03:57:56 UTC
Permalink
Post by Roger Lakner
In my CView-derived class in my SDI app, I wish to embed a button. So,
CButton PassButton;
PassButton.Create(_T("Test"), WS_CHILD | WS_VISIBLE |
BS_DEFPUSHBUTTON, CRect(362,441,362+100,441+30), this, ID_PASSBUTTON);
I, of course, have a message map to a function that handles the button
press. Everything works fine, button displays, clicking on the button
starts the expected handler, etc., except that the data associated
with many of my other variables is now garbage. If I comment out the
Create statement -- no problems. Is this a faulty implementation of a
button on my part or a bug in the CButton code or something else? Any
ideas will be greatly appreciated.
I'm using Visual .NET 2003 on XP SP2.
Roger Lakner
Just some ideas...

Maybe the code in the handler touches memory it shouldn't? Does the
problem happen with an empty handler function?

Maybe the message map macro and handler function signature are
incorrect? (This would destroy the stack.)

Maybe you need a full rebuild?
--
Scott McPhillips [VC++ MVP]
Roger Lakner
2005-12-18 16:26:16 UTC
Permalink
Scott,
Thanks for the ideas to try. A complete rebuild I've done several
times already. The message map macro and handler signature are below:

BEGIN_MESSAGE_MAP(CTestView, CView)

ON_BN_CLICKED(ID_PASSBUTTON, OnPassButtonClicked)

END_MESSAGE_MAP()

protected:

afx_msg void OnPassButtonClicked();

You're right that a function in the handler messes with my data,
though I haven't tracked down just exactly why yet. But that function
works perfectly fine if I comment out the CButton::Create line. And it
has for many iterations of this program. I'll look more closely at it,
though.

Roger
Post by Scott McPhillips [MVP]
Post by Roger Lakner
In my CView-derived class in my SDI app, I wish to embed a button. So,
CButton PassButton;
PassButton.Create(_T("Test"), WS_CHILD | WS_VISIBLE |
BS_DEFPUSHBUTTON, CRect(362,441,362+100,441+30), this,
ID_PASSBUTTON);
I, of course, have a message map to a function that handles the button
press. Everything works fine, button displays, clicking on the button
starts the expected handler, etc., except that the data associated
with many of my other variables is now garbage. If I comment out the
Create statement -- no problems. Is this a faulty implementation of a
button on my part or a bug in the CButton code or something else?
Any ideas will be greatly appreciated.
I'm using Visual .NET 2003 on XP SP2.
Roger Lakner
Just some ideas...
Maybe the code in the handler touches memory it shouldn't? Does the
problem happen with an empty handler function?
Maybe the message map macro and handler function signature are
incorrect? (This would destroy the stack.)
Maybe you need a full rebuild?
--
Scott McPhillips [VC++ MVP]
Roger Lakner
2005-12-18 19:02:51 UTC
Permalink
Sorry, the function declaration is actually public, not protected.
-Roger
Post by Roger Lakner
Scott,
Thanks for the ideas to try. A complete rebuild I've done several
times already. The message map macro and handler signature are
BEGIN_MESSAGE_MAP(CTestView, CView)
ON_BN_CLICKED(ID_PASSBUTTON, OnPassButtonClicked)
END_MESSAGE_MAP()
afx_msg void OnPassButtonClicked();
You're right that a function in the handler messes with my data,
though I haven't tracked down just exactly why yet. But that
function
works perfectly fine if I comment out the CButton::Create line. And it
has for many iterations of this program. I'll look more closely at it,
though.
Roger
Post by Scott McPhillips [MVP]
Post by Roger Lakner
In my CView-derived class in my SDI app, I wish to embed a button. So,
CButton PassButton;
PassButton.Create(_T("Test"), WS_CHILD | WS_VISIBLE |
BS_DEFPUSHBUTTON, CRect(362,441,362+100,441+30), this,
ID_PASSBUTTON);
I, of course, have a message map to a function that handles the button
press. Everything works fine, button displays, clicking on the button
starts the expected handler, etc., except that the data associated
with many of my other variables is now garbage. If I comment out the
Create statement -- no problems. Is this a faulty implementation
of
a
button on my part or a bug in the CButton code or something else?
Any ideas will be greatly appreciated.
I'm using Visual .NET 2003 on XP SP2.
Roger Lakner
Just some ideas...
Maybe the code in the handler touches memory it shouldn't? Does the
problem happen with an empty handler function?
Maybe the message map macro and handler function signature are
incorrect? (This would destroy the stack.)
Maybe you need a full rebuild?
--
Scott McPhillips [VC++ MVP]
Tom Serface
2005-12-18 23:18:59 UTC
Permalink
Are the other variables referenced in the handler code? You could put
something like:

UINT x = m_OtherVar;

in your handler code so you could trace through with the debugger and see
when it is getting corrupted. Also, be sure that your stack isn't getting
hit by an out of bounds (outside of an array) error or that you are not
writing to a pointer that has not been initialized or something like that.

Tom
Post by Roger Lakner
In my CView-derived class in my SDI app, I wish to embed a button. So,
CButton PassButton;
PassButton.Create(_T("Test"), WS_CHILD | WS_VISIBLE |
BS_DEFPUSHBUTTON, CRect(362,441,362+100,441+30), this, ID_PASSBUTTON);
I, of course, have a message map to a function that handles the button
press. Everything works fine, button displays, clicking on the button
starts the expected handler, etc., except that the data associated
with many of my other variables is now garbage. If I comment out the
Create statement -- no problems. Is this a faulty implementation of a
button on my part or a bug in the CButton code or something else? Any
ideas will be greatly appreciated.
I'm using Visual .NET 2003 on XP SP2.
Roger Lakner
Roger Lakner
2005-12-19 00:00:29 UTC
Permalink
Tom,
You're idea about a bad pointer or out-of-bounds array error was my
first thought, but that doesn't seem to be the case here. The only
time I get the problem is when the button click handler gets called by
an actual click of the button. If I just call the function, bypassing
the need to click the button, everything works fine. It has something
to do with the manner in which the button handler is entered, not
something to do with what happens in the button handler. Perhaps there
is something wrong with the ON_BN_CLICKED macro or its associated
functions as Scott suggested.

Roger
Post by Tom Serface
Are the other variables referenced in the handler code? You could
UINT x = m_OtherVar;
in your handler code so you could trace through with the debugger
and see when it is getting corrupted. Also, be sure that your stack
isn't getting hit by an out of bounds (outside of an array) error or
that you are not writing to a pointer that has not been initialized
or something like that.
Tom
Post by Roger Lakner
In my CView-derived class in my SDI app, I wish to embed a button. So,
CButton PassButton;
PassButton.Create(_T("Test"), WS_CHILD | WS_VISIBLE |
BS_DEFPUSHBUTTON, CRect(362,441,362+100,441+30), this,
ID_PASSBUTTON);
I, of course, have a message map to a function that handles the button
press. Everything works fine, button displays, clicking on the button
starts the expected handler, etc., except that the data associated
with many of my other variables is now garbage. If I comment out the
Create statement -- no problems. Is this a faulty implementation of a
button on my part or a bug in the CButton code or something else?
Any ideas will be greatly appreciated.
I'm using Visual .NET 2003 on XP SP2.
Roger Lakner
Tom Serface
2005-12-19 18:00:13 UTC
Permalink
Weird...

You may want to just delete the handler and variable (keep your handler
function of course... don't want to lose your own code) then rebuild the
thing using the wizard to hook the event. I don't know if this will change
anything, but like the witch said to Dorothy, sometimes it helps to start at
the beginning.

Tom
Tom,
You're idea about a bad pointer or out-of-bounds array error was my first
thought, but that doesn't seem to be the case here. The only time I get
the problem is when the button click handler gets called by an actual
click of the button. If I just call the function, bypassing the need to
click the button, everything works fine. It has something to do with the
manner in which the button handler is entered, not something to do with
what happens in the button handler. Perhaps there is something wrong with
the ON_BN_CLICKED macro or its associated functions as Scott suggested.
Roger Lakner
2005-12-20 15:15:07 UTC
Permalink
I'm not sure how to use the wizard to hook the event since my button
is being
created in the context of a CView-derived class, not a CFormView- or
CDialog-
derived class. That's why I originally just entered the message map
entry by hand.
Perhaps there is some other way?

Roger
Post by Tom Serface
Weird...
You may want to just delete the handler and variable (keep your
handler function of course... don't want to lose your own code) then
rebuild the thing using the wizard to hook the event. I don't know
if this will change anything, but like the witch said to Dorothy,
sometimes it helps to start at the beginning.
Tom
Post by Roger Lakner
Tom,
You're idea about a bad pointer or out-of-bounds array error was my
first thought, but that doesn't seem to be the case here. The only
time I get the problem is when the button click handler gets called
by an actual click of the button. If I just call the function,
bypassing the need to click the button, everything works fine. It
has something to do with the manner in which the button handler is
entered, not something to do with what happens in the button
handler. Perhaps there is something wrong with the ON_BN_CLICKED
macro or its associated functions as Scott suggested.
Tom Serface
2005-12-20 16:42:26 UTC
Permalink
Oops, sorry, I misunderstood.

You could try creating a small dialog (just for the sake of getting the
code) and adding the same button and the handler then cut and paste the code
that is created. Sorry... not much to go on.

Tom
I'm not sure how to use the wizard to hook the event since my button is
being
created in the context of a CView-derived class, not a CFormView- or
CDialog-
derived class. That's why I originally just entered the message map entry
by hand.
Perhaps there is some other way?
Roger
Scott McPhillips [MVP]
2005-12-21 00:24:02 UTC
Permalink
Post by Roger Lakner
I'm not sure how to use the wizard to hook the event since my button
is being
created in the context of a CView-derived class, not a CFormView- or
CDialog-
derived class. That's why I originally just entered the message map
entry by hand.
Perhaps there is some other way?
I don't think that's going to help. The function prototype you posted
is correct. This problem is pretty stubborn. Personally, at this point
I would descend into assembly language and single-step into the
function, comparing the good scenario with the bad. Something seems to
be wrong with the calling convention/parameter passing, and that can
only be seen by watching the call be performed.
--
Scott McPhillips [VC++ MVP]
Roger Lakner
2005-12-26 23:34:14 UTC
Permalink
If I set a breakpoint on the data in question, making it break
whenever the data changes, it breaks while performing a function
ntdll.dll!7c910ba9()
ntdll.dll!7c91b417()
ntdll.dll!7c912d34()

And the relevant disassembly code surrounding this instruction looks
like this:
7C910B94 mov edi,edi
7C910B96 push ebp
7C910B97 mov ebp,esp
7C910B99 mov edx,dword ptr [ebp+0Ch]
7C910B9C movzx ecx,word ptr [edx]
7C910B9F cmp ecx,80h
7C910BA5 mov eax,dword ptr [ebp+8]
7C910BA8 push esi
7C910BA9 mov esi,dword ptr [eax+170h]
7C910BAF mov dword ptr [ebp+8],ecx
7C910BB2 jb 7C910BC2
7C910BB4 dec dword ptr [eax+16Ch]
7C910BBA test esi,esi
7C910BBC jne 7C912157
7C910BC2 pop esi
7C910BC3 pop ebp
7C910BC4 ret 8

This doesn't mean much to me, except to increase my despair. Perhaps
someone out there has some idea of what might be going on.

Roger
In my CView-derived class in my SDI app, I wish to embed a button. So,
CButton PassButton;
PassButton.Create(_T("Test"), WS_CHILD | WS_VISIBLE |
BS_DEFPUSHBUTTON, CRect(362,441,362+100,441+30), this,
ID_PASSBUTTON);
I, of course, have a message map to a function that handles the button
press. Everything works fine, button displays, clicking on the button
starts the expected handler, etc., except that the data associated
with many of my other variables is now garbage. If I comment out the
Create statement -- no problems. Is this a faulty implementation of a
button on my part or a bug in the CButton code or something else?
Any ideas will be greatly appreciated.
I'm using Visual .NET 2003 on XP SP2.
Roger Lakner
Loading...