Close-Application issue

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
Nicola87
In need of some credit
In need of some credit
Posts: 9
Joined: Mon Sep 27, 2010 1:20 pm

Close-Application issue

Post by Nicola87 »

Hi guys, I really wish you could help me with this.
I wrote a program which calculates the "Codice Fiscale", the equivalent to a tax code in Italy.
Everything's fine until I exit the application.
It crashes but, using w7 it actually doesn't show, while it does under xp (that classic window appears when all programs crash). But it's also shown in the IDE i'm using, C::B, 'cause a negative value is returned, instead of returning 0 on success exit.
I don't know if it's a memory-related issue or whatever else, that's the only thing I can think of, but I don't know how to solve it.
Another problem, in the MyDialog class, method OnCheckBox.
Each time you want to exit, it asks for confirmation, I check the box so you don't ask me again.
I save the information of asking/not-asking for confirmation in a file, config.ini.
It's opened e read correctly, but can't make it to write to the file. Why?
Hope you can help me and thanks in advance (sorry for my bad English).
Nicola87
In need of some credit
In need of some credit
Posts: 9
Joined: Mon Sep 27, 2010 1:20 pm

Post by Nicola87 »

Sorry, I exceeded with the size of the attachment.
Check it here:
http://www.mediafire.com/?en1nhmiuv1npo58.

Thankx again.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Hi,

few people will download your whole project and debug it for you. Please try to post relevant parts of code (especially possible for config issues).

Regarding shutdown crash, try commenting out your destructors or any place where you deallocate stuff. Does it stop crashing? Then uncomment them one by one until it crashes again, then you will know which one is the problem
"Keyboard not detected. Press F1 to continue"
-- Windows
Nicola87
In need of some credit
In need of some credit
Posts: 9
Joined: Mon Sep 27, 2010 1:20 pm

Post by Nicola87 »

Sorry for that, as u can see i'm a newbie here.
Well, I already tried showing messages on each destructor.
Everything shows as it is supposed to, but it still crashes at the end. I tried writing a very tiny simple program voluntarily
causing a memory leak, but it did not crash.
I actually do not deallocate everything i allocate, 'cause, as far as i know, when you destroy a frame, the library will destroy its children for you, i.e. panel inside the frame and buttons, static text, text controll, ecc inside the panel.
Am I right?
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Well, showing a message box was not the point of my suggestion, sure the message boxes will show. I was rather considering that you may be deleting something you shouldn't - in which case the delete will work just fine but it will crash later when wx tries to access the object you eagerly deleted.
"Keyboard not detected. Press F1 to continue"
-- Windows
Nicola87
In need of some credit
In need of some credit
Posts: 9
Joined: Mon Sep 27, 2010 1:20 pm

Post by Nicola87 »

Oh yeah sorry, I misunderstood. Somehow i thought you were referring to the order destructors were called.
Anyway I found it. It was one single delete that made my program crash.
My second issue: I can't manage to write to the file.
Here's a snippet of the code:
void MyDialog::OnCheckBox(wxCommandEvent& event)
{
wxTextFile File;
wxStringTokenizer stk;
File.Open(wxT("config.ini"));

if(!File.IsOpened()){
wxLogError(wxT("Impossibilie aprire il file di configurazione (config.ini)"));
return;
}

for(size_t i = 0; i < File.GetLineCount(); i++)
{
if(File.GetLine(i).Len() == 0) continue;
stk.SetString(File.GetLine(i), wxT("="), wxTOKEN_RET_EMPTY_ALL);
if(stk.CountTokens() != 2) continue;

if(stk.GetNextToken() == wxT("CONFIRM_ON_EXIT"))
{
wxString temp(stk.GetNextToken());
if(temp == wxT("1")){
File.RemoveLine(i);
File.InsertLine(wxT("CONFIRM_ON_EXIT=0"), i, wxTextFileType_Dos);
}else{
File.RemoveLine(i);
File.InsertLine(wxT("CONFIRM_ON_EXIT=1"), i, wxTextFileType_Dos);
}
break;
}
}
File.Close();
}
It opens and I can read from it, but can't write to.
Where did I go wrong?
And always thank you for you patience.
Nicola87
In need of some credit
In need of some credit
Posts: 9
Joined: Mon Sep 27, 2010 1:20 pm

Post by Nicola87 »

Oh yeah sorry, I misunderstood. Somehow i thought you were referring to the order destructors were called.
Anyway I found it. It was one single delete that made my program crash.
My second issue: I can't manage to write to the file.
Here's a snippet of the code:

Code: Select all

void MyDialog::OnCheckBox(wxCommandEvent& event)
{
    wxTextFile File;
    wxStringTokenizer stk;
    File.Open(wxT("config.ini"));

    if(!File.IsOpened()){
        wxLogError(wxT("Impossibilie aprire il file di configurazione (config.ini)"));
        return;
    }

    for(size_t i = 0; i < File.GetLineCount(); i++)
    {
        if(File.GetLine(i).Len() == 0) continue;
        stk.SetString(File.GetLine(i), wxT("="), wxTOKEN_RET_EMPTY_ALL);
        if(stk.CountTokens() != 2) continue;

        if(stk.GetNextToken() == wxT("CONFIRM_ON_EXIT"))
        {
            wxString temp(stk.GetNextToken());
            if(temp == wxT("1")){
                File.RemoveLine(i);
                File.InsertLine(wxT("CONFIRM_ON_EXIT=0"), i, wxTextFileType_Dos);
            }else{
                File.RemoveLine(i);
                File.InsertLine(wxT("CONFIRM_ON_EXIT=1"), i, wxTextFileType_Dos);
            }
            break;
        }
    }
    File.Close();
}
It opens and I can read from it, but can't write to.
Where did I go wrong?
And always thank you for you patience.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Let me quote the docs for wxTextFile :
Save your changes: notice that the changes you make to the file will not be saved automatically; calling Close or doing nothing discards them! To save the changes you must explicitly call Write - here, you may also change the line termination type if you wish.
i.e. call Write before Close
"Keyboard not detected. Press F1 to continue"
-- Windows
Nicola87
In need of some credit
In need of some credit
Posts: 9
Joined: Mon Sep 27, 2010 1:20 pm

Post by Nicola87 »

I have the "Cross-Platform GUI Programming with wxWidgets" documentation and didn't find it.
Anyway, it's still not working.

Code: Select all

void MyDialog::OnCheckBox(wxCommandEvent& event)
{
    wxTextFile File;
    wxStringTokenizer stk;
    File.Open(wxT("config.ini"), wxConvUTF8);

    if(!File.IsOpened()){
        wxLogError(wxT("Impossibilie aprire il file di configurazione (config.ini)"));
        return;
    }

    for(size_t i = 0; i < File.GetLineCount(); i++)
    {
        if(File.GetLine(i).Len() == 0) continue;
        stk.SetString(File.GetLine(i), wxT("="), wxTOKEN_RET_EMPTY_ALL);
        if(stk.CountTokens() != 2) continue;

        wxString temp1(stk.GetNextToken());
        wxMessageBox(temp1);

        if(temp1 == wxT("CONFIRM_ON_EXIT"))
        {
            wxString temp2 = stk.GetNextToken();
            wxMessageBox(temp2);
            if(temp2 == wxT("1")){
                File.RemoveLine(i);
                File.InsertLine(wxT("CONFIRM_ON_EXIT=0"), i, wxTextFileType_Dos);
            }else{
                File.RemoveLine(i);
                File.InsertLine(wxT("CONFIRM_ON_EXIT=1"), i, wxTextFileType_Dos);
            }
            break;
        }
    }
    File.Write(wxTextFileType_Dos, wxConvUTF8);
    File.Close();
}
Here temp2 is not shown, the if-comparison fails. Why is that? Thanx.
jfouche
Super wx Problem Solver
Super wx Problem Solver
Posts: 442
Joined: Tue May 06, 2008 4:52 pm
Location: France

Post by jfouche »

Does :

Code: Select all

wxMessageBox(File.GetLine(i));
wxString temp1(stk.GetNextToken()); 
wxMessageBox(wxString(wxtT("temp1 = ")) + temp1); 
wxString temp2 = stk.GetNextToken(); 
wxMessageBox(wxString(wxtT("temp2 = ")) + temp2);
Shows you CONFIRM_ON_EXIT for temp1 ?
What is your text file content ?
Jérémie
Nicola87
In need of some credit
In need of some credit
Posts: 9
Joined: Mon Sep 27, 2010 1:20 pm

Post by Nicola87 »

It does.
This might be a stupid question: is there an operator== for wxString? 'cause I don't seem to find it...
I find an operator== for wxArrayString, wxDateSpan, wxFileName, wxGBPosition, wxGBSpan, wxPlatformInfo and wxRichTextRange, but not for wxString.
But looks like it works...
Anyway, this is not working either:

Code: Select all

wxString temp1(stk.GetNextToken());
        wxMessageBox(wxT("temp1 = ") + temp1);

        if(temp1.Cmp(wxString(wxT("CONFIRM_ON_EXIT"))))
        {
            wxString temp2 = stk.GetNextToken();
            wxMessageBox(wxT("temp2: = ") + temp2);
            if(temp2.Cmp(wxString(wxT("1")))){
                wxMessageBox(wxT("Da 1 a 0"));
                File.RemoveLine(i);
                File.InsertLine(wxT("CONFIRM_ON_EXIT=0"), i, wxTextFileType_Dos);
            }else{
                wxMessageBox(wxT("Da 0 a 1"));
                File.RemoveLine(i);
                File.InsertLine(wxT("CONFIRM_ON_EXIT=1"), i, wxTextFileType_Dos);
            }
            break;
        }
Nicola87
In need of some credit
In need of some credit
Posts: 9
Joined: Mon Sep 27, 2010 1:20 pm

Post by Nicola87 »

I'm sorry, I forgot it.
config.ini is just like this:
CONFIRM_ON_EXIT=1
or
CONFIRM_ON_EXIT=0
of course...
jfouche
Super wx Problem Solver
Super wx Problem Solver
Posts: 442
Joined: Tue May 06, 2008 4:52 pm
Location: France

Post by jfouche »

Nicola87 wrote:This might be a stupid question: is there an operator== for wxString? 'cause I don't seem to find it...
Don't be afraid, it exists ofc. So

Code: Select all

if(temp1 == wxT("CONFIRM_ON_EXIT")) {
have the expected behaviour.

What exactly does not work ?
Is your temp2 msgBox shown ? Does 1 or 2 is shown on the msgBox ?
Are you sure you don't have spaces arround CONFIRM_ON_EXIT (you can use temp.Trim().Trim(false) to remove extra white spaces).
Jérémie
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

You can find up to date docs online at : http://docs.wxwidgets.org/stable/wx_wxt ... wxtextfile

(the book might not contain the very latest ones)

The method Write I mentioned returned a boolean that indicates whether the operation was successful. Did you check this bool?
"Keyboard not detected. Press F1 to continue"
-- Windows
Nicola87
In need of some credit
In need of some credit
Posts: 9
Joined: Mon Sep 27, 2010 1:20 pm

Post by Nicola87 »

It all works just fine. Thankx.
One last thing, but this is a bit OT, even though it's always about my project.
I have a toolbar with some tools of course, and tool descriptions are shown.
Some descriptions are longer, others are shorter.
The fact's that the width of the border of the button associated with every tool equals the width of the border of the button wich has the longest description.
(I could have said that better, I'm Italian, sorry. Hope you got the point.)
This implies that with shorter descriptions I get empty space in the button, which I don't like to see.
Can U help me?
This is my code:

Code: Select all

wxBoxSizer * topsizer = new wxBoxSizer(wxVERTICAL);
    wxPanel * panel = new wxPanel(this, wxID_ANY);
    wxBoxSizer * vbox = new wxBoxSizer(wxVERTICAL);
    int ToolbarFlags = wxTB_HORZ_TEXT | wxTB_NODIVIDER | wxTB_FLAT | wxTB_HORIZONTAL;
    wxToolBar * toolbar = new wxToolBar(panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, ToolbarFlags);

    toolbar->AddTool(BtnExtra, wxT("Spaziatura"), wxBitmap(wxT("images/Extra.bmp"), wxBITMAP_TYPE_ANY), wxT("Attiva spaziatura CF"), wxITEM_CHECK);
    toolbar->AddSeparator();
    toolbar->AddTool(BtnInfo, wxT("Info"), wxBitmap(wxT("images/InfoBox-icon.bmp"), wxBITMAP_TYPE_ANY), wxT("Info sull'autore"));
    toolbar->AddTool(BtnExit, wxT("Esci"), wxBitmap(wxT("images/exit-icon.bmp"), wxBITMAP_TYPE_ANY), wxT("Esci dall'applicazione"));
    toolbar->Realize();
    vbox->Add(toolbar, 0, wxEXPAND);

    wxNotebook * notebook = new wxNotebook(panel, wxID_ANY);
    notebook->AddPage(CreateFirstPage(notebook), wxT("Creazione Codice Fiscale"));
    notebook->AddPage(CreateSecondPage(notebook), wxT("Verifica Codice Fiscale"));
    vbox->Add(notebook, 1, wxLEFT | wxTOP | wxEXPAND, 1);

    panel->SetSizer(vbox);
    topsizer->Add(panel, 1, wxEXPAND);

    this->SetSizer(topsizer);
    SetMinSize(minsize);
    Centre();
Post Reply