Program freezes... why? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Program freezes... why?

Post by FlyingIsFun1217 »

Code: Select all

class libDialog : public wxDialog
{
    public:
        libDialog(const wxString &title);

        void CancelClicked(wxCloseEvent& event);
        void StartClicked(wxCommandEvent& event);
        bool CreateLibrary();

    private:
        int         newSongs;
        bool        isScanning;
        wxString    rootDirectory;

    protected:

        wxDirPickerCtrl *currentDirectoryPicker;

};

libDialog::libDialog(const wxString &title) : wxDialog(NULL, -1, title, wxDefaultPosition, wxSize(475, 260))
{
    isScanning = false;

    [...]

    Connect(ID_CancelButton, wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction) &libDialog::CancelClicked);
    Connect(ID_StartButton, wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction) &libDialog::StartClicked);
}

void libDialog::CancelClicked(wxCloseEvent& event)
{
    if(isScanning == false)
    {
        this -> Destroy();
    }

    else
    {
        isScanning = false;

    }
}

void libDialog::StartClicked(wxCommandEvent& event)
{
    if(isScanning == false)
    {
        isScanning = true;
    }

    else
    {
        rootDirectory = currentDirectoryPicker -> GetPath();

        if(wxDir::Exists(rootDirectory) == true)
        {
            if(CreateLibrary() == false)
            {
                //
            }

            else
            {
                //
            }
        }
    }
}

bool libDialog::CreateLibrary()
{
    return true;
}
Just added the StartClicked() stuff, and now every time I try and run the program, it works just fine until I initialize the class, in which it freezes...

Can anybody spot it? Is there a secret I'm missing? Sorry if it seems like I'm just letting other people solve my problems, but I already tried looking it over (many times).

FlyingIsFun1217
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Re: Program freezes... why?

Post by mc2r »

I see a couple things here I am a little unsure of but I don't know that they have anything to do with your problem.

FlyingIsFun1217 wrote:

Code: Select all

void libDialog::CancelClicked(wxCloseEvent& event)
{
    if(isScanning == false)
    {
        this -> Destroy();
    }

    else
    {
        isScanning = false;

    }
}
Here if isScanning is true you set it to false but it doesn't look like you ever actually stop it from scanning? Also if it was scanning and you click cancel don't you still want to close the dialog? Maybe not?

Maybe this is closer,

Code: Select all

void libDialog::CancelClicked(wxCloseEvent& event)
{
    if(isScanning == true)
    {
        isScanning = false;
        // Do something to stop scanning
    }

    this -> Destroy();
}
FlyingIsFun1217 wrote:

Code: Select all

void libDialog::StartClicked(wxCommandEvent& event)
{
    if(isScanning == false)
    {
        isScanning = true;
    }

    else
    {
        rootDirectory = currentDirectoryPicker -> GetPath();

        if(wxDir::Exists(rootDirectory) == true)
        {
            if(CreateLibrary() == false)
            {
                //
            }

            else
            {
                //
            }
        }
    }
}
Hmm not sure of this either, maybe it is like you intend but it looks to me like to start scanning you will need to click ID_StartButton twice to actually start the scan. First time it will set isScanning to true, and the second time (when isScanning == true) it will actually start doing something.
FlyingIsFun1217 wrote:Just added the StartClicked() stuff, and now every time I try and run the program, it works just fine until I initialize the class, in which it freezes...
Hmm, I'd say if it freezes when you initialize the class that that doesn't sound like the StartClicked() code. That should only get called with ID_StartButton is clicked. You have a section of your constructor that has been [...snipped...], maybe look there if the freeze happens before ID_StartButton is actually clicked.

Also you might want to think about deriving a class from wxThread and putting your scanning stuff in there. Have StartClicked() run the thread, CancelClicked() stop the thread. Don't forget don't do any gui stuff in the thread but post an event and have the main thread handle all the gui stuff.

Putting some high processing task in an event handler leads to the program "freezing" while the processing is going on. While in an event handler other events can't be handled.

Hope some of that was of help.

-Max
Last edited by mc2r on Tue Aug 05, 2008 4:05 am, edited 1 time in total.
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Post by FlyingIsFun1217 »

Nope, I want the button actions as they are (cancel stops, doesn't close unless not run when clicked, etc.).

The snipped stuff is mostly gui code and what not that would only clutter it. I kept everything in there that could have made it crash (hence me saying, it was only after adding what I posted that it got freaked and crashed).

FlyingIsFun1217
Attachments
library_dialog.cpp
(4.59 KiB) Downloaded 70 times
library_dialog.h
(533 Bytes) Downloaded 70 times
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r »

FlyingIsFun1217 wrote:Nope, I want the button actions as they are (cancel stops, doesn't close unless not run when clicked, etc.).
Wasn't sure it didn't look quite right but I don't know the intent of the code so thought I'd mention it to make sure.
FlyingIsFun1217 wrote:The snipped stuff is mostly gui code and what not that would only clutter it. I kept everything in there that could have made it crash (hence me saying, it was only after adding what I posted that it got freaked and crashed).
Hmm, I'm not sure why StartClicked() would have an effect on the class initilization. You could try commenting out everything inside of StartCLicked() and put in a single wxMessageBox(wxT("Start clicked!")); or some such. See what happens. I'll take a look at the attachments, clutter and all ;), once I get a chance.

-Max
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Post by FlyingIsFun1217 »

Alright; Thanks again for your help! I really do appreciate it!

FlyingIsFun1217
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Post by FlyingIsFun1217 »

And for a REALLY fun twist, when I booted up into Linux and tried it under WINE, there was absolutely no problem. I'm really curious if it's just some little thing to do with the way Windows works now.

FlyingIsFun1217
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Post by FlyingIsFun1217 »

Anybody? Bump...

FlyingIsFun1217 :)
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r »

FlyingIsFun1217 wrote:Anybody? Bump...

FlyingIsFun1217 :)
Didn't see anything in the code beside what I pointed out.

Are you sure it is StartClicked()? Have you tried commenting out the code in there and adding a message box like i suggested? A button shouldn't receive a clicked event when the dialog it is in is initialized.

Also there is the thread thing. If you are doing alot of processing like scanning a directory in an event handler it will look like the program has frozen because events are handled one at a time and no other events will get processed while processing is still going on in StartClick().

-Max
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Post by FlyingIsFun1217 »

mc2r wrote: Are you sure it is StartClicked()? Have you tried commenting out the code in there and adding a message box like i suggested? A button shouldn't receive a clicked event when the dialog it is in is initialized.
Seems you're right. Commenting it out to do nothing gives the same output; a frozen program.
mc2r wrote: Also there is the thread thing. If you are doing alot of processing like scanning a directory in an event handler it will look like the program has frozen because events are handled one at a time and no other events will get processed while processing is still going on in StartClick().
I know it can't be that, because right now, there is no scanning mechanism to actually do anything. I will make sure and use threads later when it is going in, but for now, there's just nothing to thread.

Thanks again!
FlyingIsFun1217
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Post by FlyingIsFun1217 »

Alright, problem figured out, almost solved...

Seems in my main frame I was connecting the events that should have brought up the dialog to classes and functions that don't exist:

Code: Select all

frame -> Connect(ID_Library_Menu, wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction) &libDialog::libDialog);

Code: Select all

libDialog::libDialog() : wxDialog([...])
{
    //
}
Now, my only problem is that I'm attempting to Connect an event to a class constructor... Is this possible? When I try it, I recieve:
error: expected primary-expression before ')' token
Thanks!
FlyingIsFun1217
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r »

FlyingIsFun1217 wrote:Now, my only problem is that I'm attempting to Connect an event to a class constructor... Is this possible? When I try it, I recieve:
No, you can't do this. Of course having said that I am sure some smarty pants will come along and prove me wrong. But seriously no, the function needs to have the same signature as that defined for whatever the event is, a constructor even one with the same arguments doesn't qualify.

Maybe a better way to explain it, is connect will call the passed method on an already constructed object. You can't call the constructor of something that has been constructed already.

This makes no sense,

Code: Select all

SomeClass* obj = new SomeClass();
obj->SomeClass();
Also, you connect statement is missing the event sink. Without the event sink it will call the function on the event handler you called connect on.

Code: Select all

frame -> Connect(ID_Library_Menu, wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction) &libDialog::libDialog);
In this case it will call the function pointed to by the address of libDialog's constructor on frame. if you want to use connect and have it call a function this way you will have to create an instance of libDialog yourself and pass that as the event sink argument. Also will have to change the member function to something other than the constructor.

-Max
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Post by FlyingIsFun1217 »

Well, I guess it would have been easy enough to just call the function from another one right in the class; seems this does the trick just fine...

So simple... why can't I ever think of the simple things right away?

FlyingIsFun1217
Post Reply