On fire event the application chrashes 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
kea_
Experienced Solver
Experienced Solver
Posts: 59
Joined: Wed Oct 17, 2007 7:32 am

On fire event the application chrashes

Post by kea_ » Thu Jul 24, 2008 2:15 pm

Hello together,

I have written a class who is sending an event.
If I fire the event one time it works everything fine.
But if I fire the event 2 times my application crashes.
Can any body give me a helping hand?

Part of my source:

void csDatabase::CallEvent(){
csDatabase db;
db.SetId(0);
MyEvtHandler *met;
met->ProcessEvent(db);
//met->ProcessThreadEvent(db);
//met->AddPendingEvent(db);
//met->ProcessPendingEvents();
CallEvent2();
}

void csDatabase::CallEvent2(){
csDatabase db;
db.SetId(1);
MyEvtHandler *met;
met->ProcessEvent(db);
}

Have a big thank
kea_

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

Re: On fire event the application chrashes

Post by mc2r » Thu Jul 24, 2008 2:27 pm

Is this the actual code you are using, ie... did you cut and paste it? Or did you rewrite a portion of it for the post? I ask because there is a big error for sure and if this is cut and pasted code it will never work as expected.
kea_ wrote:

Code: Select all

    // ...snip...
    MyEvtHandler *met;
    met->ProcessEvent(db);

    // ...snip...
    MyEvtHandler *met;
    met->ProcessEvent(db);
}
Neither of these are correct c++. met is an uninitialized pointer and you can not ever call a method of it, as it is not actually pointing to an instance of MyEvtHandler. you have to call new first, or better if it is in a functions scope you could just make it an object.

Code: Select all

MyEvtHandler met(...Whatever arguments the constructor needs...);
met.ProcessEvent(db);
or with new

Code: Select all

MyEvtHandler *met = NULL;
met = new MyEvtHandler(...Whatever arguments the constructor needs...);
if(met){
    met->ProcessEvent(db);
    // Other stuff with met
    delete met;
}
-Max

kea_
Experienced Solver
Experienced Solver
Posts: 59
Joined: Wed Oct 17, 2007 7:32 am

Post by kea_ » Fri Jul 25, 2008 5:28 am

Hello Max,
thank you for your answer.
I show you the complete code.
In the way I do it it works but just for one time.
I tryed it in your way but then it wouldn't work.
Did I do something wrong in the base?

Database.h
#ifndef __csDatabase_h__
#define __csDatabase_h__

#include <wx/event.h>


// For compilers that support precompilation, includes "wx.h".
#include <wx/wxprec.h>

#ifdef __BORLANDC__
#pragma hdrstop
#endif


class csDatabase : public wxEvent
{
public:
// F U N C T I O N S
csDatabase();
csDatabase(const csDatabase &event);
wxEvent *Clone() const;
void CallEvent();
void CallEvent2();

private:
// P A R A M E T E R S
// F U N C T I O N S
// E V E N T S
// E N U M
};


class MyEvtHandler : public wxEvtHandler
{
public:
MyEvtHandler();
~MyEvtHandler();
};


BEGIN_DECLARE_EVENT_TYPES()
DECLARE_EVENT_TYPE(myEVT_SAVING, -1)
END_DECLARE_EVENT_TYPES()


typedef void (wxEvtHandler::*csDatabaseFunction)(csDatabase&);

#define EVT_SAVING(fn) \
DECLARE_EVENT_TABLE_ENTRY(myEVT_SAVING, -1, -1, \
(wxObjectEventFunction) (wxEventFunction) \
wxStaticCastEvent(wxCommandEventFunction, & fn), (wxObject *) NULL),



/*
#define EVT_SAVING(fn) \
DECLARE_EVENT_TABLE_ENTRY(myEVT_SAVING, -1, -1, \
(wxObjectEventFunction) (wxEventFunction) \
wxStaticCastEvent(wxCommandEventFunction, & fn), (wxObject *) NULL ),
*/
#endif

csDatabase.cpp
#include "csDatabase.h"

DEFINE_EVENT_TYPE(myEVT_SAVING)



csDatabase::csDatabase()
:wxEvent(){
SetEventType(myEVT_SAVING);
}


csDatabase::csDatabase(const csDatabase &event)
:wxEvent(event){
}


wxEvent *csDatabase::Clone() const{
return new csDatabase(*this);
}


void csDatabase::CallEvent(){
csDatabase db;
db.SetId(0);
MyEvtHandler *met;
met->ProcessEvent(db);
//met->ProcessEvent(db);

//met->ProcessThreadEvent(db);
//met->AddPendingEvent(db);
//met->ProcessPendingEvents();
//CallEvent2();
}


void csDatabase::CallEvent2(){
csDatabase db;
db.SetId(1);
MyEvtHandler *met;
met->ProcessEvent(db);

// met->AddPendingEvent(db);
}


MyEvtHandler::MyEvtHandler()
:wxEvtHandler(){
}


MyEvtHandler::~MyEvtHandler(){
}

Greetings and thanks
kea_

kea_
Experienced Solver
Experienced Solver
Posts: 59
Joined: Wed Oct 17, 2007 7:32 am

Post by kea_ » Fri Jul 25, 2008 5:45 am

Hello to all,

I solved the problem.
Just pointed to the MyEvtHandler once an called the function ProcessEvent more times.

See the code below.

void csDatabase::CallEvent(){
csDatabase db;
db.SetId(0);
MyEvtHandler *met;
met->ProcessEvent(db);
CallEvent2(db, met);
}


void csDatabase::CallEvent2(csDatabase &i_db, MyEvtHandler *i_met){
i_db.SetId(1);
i_met->ProcessEvent(i_db);
}

Post Reply