Page 1 of 1

[resolved]Error at end of wxbuttonclik function

Posted: Thu Apr 28, 2005 9:29 pm
by GianT
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

Posted: Fri Apr 29, 2005 7:00 am
by Jorg
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

Posted: Fri Apr 29, 2005 12:48 pm
by GianT
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)...

Posted: Fri Apr 29, 2005 12:53 pm
by upCASE
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..

Posted: Fri Apr 29, 2005 12:57 pm
by GianT
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:

Posted: Fri Apr 29, 2005 1:11 pm
by upCASE
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.

Posted: Fri Apr 29, 2005 1:56 pm
by Jorg
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

Posted: Fri Apr 29, 2005 3:09 pm
by GianT
Yes thanks it works with mysql_init(NULL) :D . That's strange because I always used it this way.