Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   classes and members (http://www.velocityreviews.com/forums/t458129-classes-and-members.html)

rory 11-03-2006 10:54 AM

classes and members
 
Hi everyone. I am developing a program using wxWidgets, but my problems
are more to do with general C++ practices so I've decided to ask here
rather than on the wxWidgets list. I have a main class, my frame class,
which contains lots of GUI controls I need in my application. I've also
derived a new class, wxLoopStation, from the MDIChildFrame base class.
I create an objects of this class in one of my main frame class member
functions and it all goes fine. The problem is when I try to access a
member variable of wxLoopStation I keep getting a crash, the program
simply hangs there? Any ideas? Here is some pseudo code from my app:

//wxLooperFrame is my main class....
void wxLooperFrame::CreateChildFrame()
{
//create a new child frame...
loopStation=new wxLoopStation((wxMDIParentFrame*)this, -1, "Loop1",
wxPoint(20, 20), wxSize(215, 255));
//create a text box in new child frame
loopStation->freqText = new wxTextCtrl(panel, -1, "1",wxPoint(140,
24), wxSize(40, 20), wxTE_READONLY);
}

void wxLooperFrame::ButtonClick()
{
//read text from loopStation's text control
wxMessageBox(loopStation->freqText->GetLabel());
}

So whenever ButtonClick() is called I get a crash. Any ideas? I hope
it's something simple!

Rory.


Stuart Redmann 11-03-2006 01:10 PM

Re: classes and members
 
rory wrote:
> void wxLooperFrame::ButtonClick()
> {
> //read text from loopStation's text control
> wxMessageBox(loopStation->freqText->GetLabel());
> }
>
> So whenever ButtonClick() is called I get a crash. Any ideas? I hope
> it's something simple!


When using pointers, you should always make sure that the pointers
actually point somewhere. Easiest thing for your code would be to set a
breakpoint at ButtonClick and examine the pointers. In general it would
be a good practise to use some kind of ASSERT meachnism, like Visual C
provides:

void wxLooperFrame::ButtonClick()
{
//read text from loopStation's text control
ASSERT (loopStation);
ASSERT (loopStation->freqText);
wxMessageBox(loopStation->freqText->GetLabel());
}

When not avaluating to true, the assert will pop up a little debug
window at run-time, so that you can trace into your code conveniently.

Regards,
Stuart

rory 11-03-2006 02:00 PM

Re: classes and members
 
Thanks Stuart, unfortunately I am using mingw and don't have it st up
for debugging, nor would I know how to use that debugger even if I did
have it set up. From looking at the code can you see any obvious
reasons why I can't call the GetValue() method? Given that loopStation
is a member variable of wxLooperFrame I should be able to call all its
public methods from any of wxLooperFrames member functions, right?
Thanks for the help.

Rory.


Stuart Redmann wrote:
> rory wrote:
> > void wxLooperFrame::ButtonClick()
> > {
> > //read text from loopStation's text control
> > wxMessageBox(loopStation->freqText->GetLabel());
> > }
> >
> > So whenever ButtonClick() is called I get a crash. Any ideas? I hope
> > it's something simple!

>
> When using pointers, you should always make sure that the pointers
> actually point somewhere. Easiest thing for your code would be to set a
> breakpoint at ButtonClick and examine the pointers. In general it would
> be a good practise to use some kind of ASSERT meachnism, like Visual C
> provides:
>
> void wxLooperFrame::ButtonClick()
> {
> //read text from loopStation's text control
> ASSERT (loopStation);
> ASSERT (loopStation->freqText);
> wxMessageBox(loopStation->freqText->GetLabel());
> }
>
> When not avaluating to true, the assert will pop up a little debug
> window at run-time, so that you can trace into your code conveniently.
>
> Regards,
> Stuart



VJ 11-03-2006 02:17 PM

Re: classes and members
 
rory wrote:
> Thanks Stuart, unfortunately I am using mingw and don't have it st up
> for debugging, nor would I know how to use that debugger even if I did
> have it set up. From looking at the code can you see any obvious
> reasons why I can't call the GetValue() method? Given that loopStation
> is a member variable of wxLooperFrame I should be able to call all its
> public methods from any of wxLooperFrames member functions, right?
> Thanks for the help.
>
> Rory.
>
>
> Stuart Redmann wrote:
>
>>rory wrote:
>>
>>>void wxLooperFrame::ButtonClick()
>>>{
>>>//read text from loopStation's text control
>>>wxMessageBox(loopStation->freqText->GetLabel());
>>>}
>>>
>>>So whenever ButtonClick() is called I get a crash. Any ideas? I hope
>>>it's something simple!

>>
>>When using pointers, you should always make sure that the pointers
>>actually point somewhere. Easiest thing for your code would be to set a
>>breakpoint at ButtonClick and examine the pointers. In general it would
>>be a good practise to use some kind of ASSERT meachnism, like Visual C
>>provides:
>>
>>void wxLooperFrame::ButtonClick()
>>{
>> //read text from loopStation's text control
>> ASSERT (loopStation);
>> ASSERT (loopStation->freqText);
>> wxMessageBox(loopStation->freqText->GetLabel());
>>}
>>
>>When not avaluating to true, the assert will pop up a little debug
>>window at run-time, so that you can trace into your code conveniently.
>>
>>Regards,
>>Stuart

>
>


Might be a stupid question, but: are you sure CreateChildFrame() is called?

You might want to initialize loopStation to NULL in the constructor, and
check if it is still NULL in ButtonClick() to make sure

Stuart Redmann 11-03-2006 02:29 PM

Re: classes and members
 
rory wrote:
> Thanks Stuart, unfortunately I am using mingw and don't have it st up
> for debugging, nor would I know how to use that debugger even if I did
> have it set up. From looking at the code can you see any obvious
> reasons why I can't call the GetValue() method? Given that loopStation
> is a member variable of wxLooperFrame I should be able to call all its
> public methods from any of wxLooperFrames member functions, right?


If you tried to access a member variable or a member function that is
not public, the compiler would have complained.

Stuart

rory 11-03-2006 02:29 PM

Re: classes and members
 
> Might be a stupid question, but: are you sure CreateChildFrame() is called?
>
> You might want to initialize loopStation to NULL in the constructor, and
> check if it is still NULL in ButtonClick() to make sure


I'm sure it's been called, if it wasn't I wouldn't be seeing the
MDIChildFrame appear on screen as that is the only place I call the
constructor for the loopStation class. I might try the wxWidgets list
and see what they think. Thanks for all te help

Rory.



All times are GMT. The time now is 05:05 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.