classes and members

Discussion in 'C++' started by rory, Nov 3, 2006.

  1. rory

    rory Guest

    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.
    rory, Nov 3, 2006
    #1
    1. Advertising

  2. 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
    Stuart Redmann, Nov 3, 2006
    #2
    1. Advertising

  3. rory

    rory Guest

    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
    rory, Nov 3, 2006
    #3
  4. rory

    VJ Guest

    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
    VJ, Nov 3, 2006
    #4
  5. 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
    Stuart Redmann, Nov 3, 2006
    #5
  6. rory

    rory Guest

    > 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.
    rory, Nov 3, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JFCM
    Replies:
    4
    Views:
    5,724
  2. CoolPint
    Replies:
    8
    Views:
    957
    Jeff Schwab
    Dec 14, 2003
  3. Dave
    Replies:
    3
    Views:
    360
    tom_usenet
    Aug 10, 2004
  4. Daz
    Replies:
    5
    Views:
    567
  5. Rit
    Replies:
    23
    Views:
    923
    Mike Schilling
    Jan 3, 2010
Loading...

Share This Page