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

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] Can't create an instance of a class

Post 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.
Last edited by GianT on Tue Jun 28, 2005 1:23 am, edited 2 times in total.
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 »

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...
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 »

Hmm, it seems that segmentation fauts cannot be solved that easily. I've not solved the problem yet, could someone help me? :cry:
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post 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
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
upCASE
Moderator
Moderator
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post 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
}
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 »

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!!!
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post 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
[Mostly retired moderator, still check in to clean up some stuff]
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 »

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...
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 »

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.
Post Reply