Page 1 of 1

[Resolved] Can't create an instance of a class

Posted: Wed Jun 22, 2005 3:07 am
by GianT
Hi...I'm sorry to do so many posts, but sometimes, programming makes me...Never mind.
When I intend to create an instance of the class ThreadServer, I got an error of execution. After debugging, it comes that the constructor code is executed with no error, but the line after the object creation is not executed and I get an error of execution: Windows says that the program encountered an error and has to close...
Here are some parts of my code if this can help understand:

The constructor code:

Code: Select all

ThreadServer::ThreadServer(wxSocketBase* socket): wxThread()
{
    wxMessageBox("debug in the constructor!!");
    this->sock = socket;
    wxMessageBox("debug after socket");
    // No problem there
}

The function which creates the thread:

Code: Select all

void newProgramFrame::OnClientConnect(wxSocketBase* sock)
{
        wxMessageBox("Connexion arrived");

       th = new ThreadServer(sock);
       // Does not reach this line!!!!!!!!!!
       wxMessageBox("Thread Instance Created");
       th->Create();
       wxMessageBox("Thread Created");
       th->Run();
       wxMessageBox("Thread launched");


     return;
     
}

I am so upset with that (it worked very well yesterday!!), that I'm gonna take a small break and come back in some hours...I have to sleep a little.
If someone has an idea, don't hesitate to tell about it.

Posted: Wed Jun 22, 2005 8:42 pm
by GianT
I just discovered the debugguer mode of dev-cpp, and it tells me that I have a segmentation fault at this line, just like I thought:

Code: Select all

th = new ThreadServer(sock);
I don't understand why I have this error, here is the declaration of the threadserver object in the header file:

Code: Select all

ThreadServer *th;
I also tried to execute the code with no parameter in the constructor, but I get the same error at the execution...

Posted: Fri Jun 24, 2005 6:57 am
by GianT
Hmm, it seems that segmentation fauts cannot be solved that easily. I've not solved the problem yet, could someone help me? :cry:

Posted: Fri Jun 24, 2005 7:13 am
by Jorg
- Is sock as argument, a valid socket?

- What happens when you do not create the thread?

Simply comment out some codew until you have a bare skeleton of what should work. If it doesn't try to reproduce it in the thread sample.

If it crashes it probably never initialises properly, so whatever is in your thread constructor is wrong. To start with doing GUI stuff in a thread. Are you sure you can do that ?

- Jorgen

Posted: Fri Jun 24, 2005 7:15 am
by upCASE
Hi!
GianT wrote:Hmm, it seems that segmentation fauts cannot be solved that easily.
True, as I can't simply reproduce the problem. Maybe if you post some more code?

BTW: I do have something similar. It's a small POP3 Server thread, where the thread also is an event handler and it works just fine.
Maybe try with a thread ctor like this

Code: Select all

ThreadServer::ThreadServer(wxSocketBase* socket)
{
    Create();
    wxMessageBox("debug in the constructor!!");
    this->sock = socket;
    wxMessageBox("debug after socket");
    // No problem there
}

Posted: Mon Jun 27, 2005 1:58 am
by GianT
Jorg wrote:To start with doing GUI stuff in a thread. Are you sure you can do that ?
I added this wxMessageBoxes only for debugging.
upCASE wrote: I'm not sure, but IMaybe try with a thread ctor like this

Code: Select all

ThreadServer::ThreadServer(wxSocketBase* socket)
{
    Create();
    wxMessageBox("debug in the constructor!!");
    this->sock = socket;
    wxMessageBox("debug after socket");
    // No problem there
} 
I tried this way, and it didn't work. BTW, an additionnal information: I tried with a blank constructor, I mean with no code in it, and I still got this segmentation fault!!! Damn!!!

Posted: Mon Jun 27, 2005 2:37 am
by Ryan Norton
GianT wrote:
Jorg wrote:To start with doing GUI stuff in a thread. Are you sure you can do that ?
I added this wxMessageBoxes only for debugging.
upCASE wrote: I'm not sure, but IMaybe try with a thread ctor like this

Code: Select all

ThreadServer::ThreadServer(wxSocketBase* socket)
{
    Create();
    wxMessageBox("debug in the constructor!!");
    this->sock = socket;
    wxMessageBox("debug after socket");
    // No problem there
} 
I tried this way, and it didn't work. BTW, an additionnal information: I tried with a blank constructor, I mean with no code in it, and I still got this segmentation fault!!! Damn!!!
Please post a backtrace

Posted: Mon Jun 27, 2005 3:00 am
by GianT
Ryan Norton wrote:Please post a backtrace
Err...What is a backtrace? :oops:
Anyway, I managed to partially solve the problem, but still doesn't understand what caused it. Actually, the thread "th" was defined in NewProgrameFrame.h, so that all the NewProgramFrame.cpp functions could access it. What I did is to declare it in the function creating and calling the thread...
After that, I modified the constructor, and I had the same problem with a variable defined in the header and used in the cpp file. I put it in global in the cpp file and it works... Strange thing...

Posted: Tue Jun 28, 2005 1:12 am
by GianT
No problem anymore, I just declared all the variables I give in arguments as global ones in the cpp file, instead of declaring them in the header file.