[resolved]Error at end of wxbuttonclik function

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
GianT
Earned some good credits
Earned some good credits
Posts: 124
Joined: Wed Mar 16, 2005 5:44 pm
Location: Guadeloupe, French West Indies
Contact:

[resolved]Error at end of wxbuttonclik function

Post by GianT » Thu Apr 28, 2005 9:29 pm

Hi, I encounter a strange error when running my program. I have no error on compiling. In fact, I have a function associated with the event coming up when clicking on an OK button that I added and then I close the window (by the method Destroy();), and I just close the window when the user clicks on the Cancel button I created. The window is closed when clicking on the cancel button, but when clicking on ok, the code is executed, but the program crashes when reaching the end of the function...
Here is my code:

Code: Select all

/*
 * WxButton1Click
 */
void ajout_temps::WxButton1Click(wxCommandEvent& event)
{
    MYSQL mysql;

    const wxString login = WxComboBox1->GetValue();
    const wxString quota = WxComboBox2->GetValue();

    const wxString insert_query = wxString::Format ("update users set quota='%s' where login='%s';",quota.c_str(),login.c_str());

    mysql_init(&mysql);
    if (mysql_real_connect(&mysql,"127.0.0.1","root","","cyberz",0,NULL,0)==NULL)
   {
      printf("Error while connecting to the database.\nErreur: %s\n",mysql_error(&mysql));
      exit(-1);
   }
   else
   {
                    printf("Insertion: (%s)\n",insert_query.c_str());
                      

                    if (!mysql_query(&mysql,insert_query.mb_str(wxConvUTF8))) 
                    {
                       wxMessageBox(wxString::Format("User %s granted with %s hours",login.c_str(),quota.c_str()),"Cr
Last edited by GianT on Fri Apr 29, 2005 3:54 pm, edited 1 time in total.

Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg » Fri Apr 29, 2005 7:00 am

Do not use Destroy() use Close() instead. What you are doing now is deleting an object you are still executing code in. Close will post an EVT_CLOSE to the main loop, and it will be handled safely after you exit this handler.

- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb

GianT
Earned some good credits
Earned some good credits
Posts: 124
Joined: Wed Mar 16, 2005 5:44 pm
Location: Guadeloupe, French West Indies
Contact:

Post by GianT » Fri Apr 29, 2005 12:48 pm

I tried with Close but I still got the problem. Notice that the problem is still present where I don't write anything at the end (I mean when I just drop Destroy and don't try to close the wxdialog)...

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE » Fri Apr 29, 2005 12:53 pm

Hi!
GianT wrote:I tried with Close but I still got the problem. Notice that the problem is still present where I don't write anything at the end (I mean when I just drop Destroy and don't try to close the wxdialog)...
So, could it be that the problem is not related to wxWidgets? Maybe something is wrong with your MySQL stuff..
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

GianT
Earned some good credits
Earned some good credits
Posts: 124
Joined: Wed Mar 16, 2005 5:44 pm
Location: Guadeloupe, French West Indies
Contact:

Post by GianT » Fri Apr 29, 2005 12:57 pm

Yes it seems so. Indeed, I just tried to comment the mysql part and it works well... But when I let even the mysql_init and the mysql_close (without all the requests) I have the problem... I am fed up with that I don't understand why this s**t bugs :@@@@@ :evil:

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE » Fri Apr 29, 2005 1:11 pm

Hi!
I just looked this up in the MySQL API. You use mysql_init() the wrong way...
Try with

Code: Select all

MYSQL* mysql;
mysql = mysql_init(NULL);
if(!mysql)
//something terrible happend :)
See here http://dev.mysql.com/doc/mysql/en/mysql-init.html and read the comment by Robert Basler.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg » Fri Apr 29, 2005 1:56 pm

Yeah you might look at the MSSQL object is it allowed to create this instance on the stack ?

Anyway the Close / Destroy was definately an issue of concern as this cleans up the object you are in yourself (ok after all events are handled, but you never know)..

It is similar to:

Code: Select all

MyClass::SoSomething()
{
     delete this;

     CallSomeOtherFunction();
}
To come back to the mysql problem, it sounds logical that the stack object creation is wrong. Because you declare a temp object in your function, assign it to:

Code: Select all

 mysql_init(&mysql); 
And where do you tell the mysql framework the object is invalid? mysql cannot smell that the function you are in already cleaned this object. And that's ofcourse what happens upon exit of the function

- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb

GianT
Earned some good credits
Earned some good credits
Posts: 124
Joined: Wed Mar 16, 2005 5:44 pm
Location: Guadeloupe, French West Indies
Contact:

Post by GianT » Fri Apr 29, 2005 3:09 pm

Yes thanks it works with mysql_init(NULL) :D . That's strange because I always used it this way.

Post Reply