[C++] SegFault bij aanmaken object in klasse

Pagina: 1
Acties:

  • 2playgames
  • Registratie: Februari 2005
  • Laatst online: 01-06 15:19
ik heb de volgende klasse (schematisch voor de leesbaarheid)
code:
1
2
3
4
class MyClass {
 void functie();
 Dinges* bla;
};


als ik hier dan de volgende implementatie aan geef, krijg ik een uncaught exception fout (volgens de debugger een Segmentation Fault)
code:
1
2
3
void MyClass::functie() {
 bla = new Dinges();
}


die krijg ik niet bij:
code:
1
2
3
void MyClass::functie() {
 Dinges* bla2 = new Dinges();
}


maar dan weer wel bij
code:
1
2
3
4
void MyClass::functie() {
 Dinges* bla2 = new Dinges();
 bla = bla2;
}


wat kan hier aan de hand zijn?
ik heb andere klasses die een soortgelijke code hebben, en die werken wel

ik programmeer onder windows xp sp2, met mingw en wxWidgets

mocht bovenstaande niet genoeg zijn, dan is hier alle code

main.hpp
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "wx/wxprec.h"

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include "wx/socket.h"


// ---------------------------------- //
// --- class forward declarations --- //
// ---------------------------------- //

class EchoClientFrame;
class EchoClient;




// --------------------------------- //
// --- actual class declarations --- //
// --------------------------------- //

// --- in deze klasse - zie main.cpp --- //
// VVVVVVVVVVVVVVVVVVVVV //
// main class
class EchoClient : public wxApp {
    public:
    virtual bool OnInit();
    void StartClient();
    
    private:
    wxSocketClient* client;
    EchoClientFrame* frame;
};

// main window
class EchoClientFrame : public wxFrame {
    public:
    
    // Constructor
    EchoClientFrame(EchoClient* application, const wxString& title);
    
    // Event handlers
    void OnQuit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);
    
    wxListBox* outputListBox;
    
    private:
    
    wxMenuBar* menuBar;
    wxTextCtrl* inputField;
    wxButton* sendButton;
    EchoClient* app;
    
    // This class handles events
    DECLARE_EVENT_TABLE()
    
};


main.cpp
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "main.hpp"

// Implements MyApp& GetApp()
DECLARE_APP(EchoClient)

// Give wxWidgets the means to create a MyApp object
IMPLEMENT_APP(EchoClient)

// Initialize the application
bool EchoClient::OnInit() {
    // Create the main application window
    frame = new EchoClientFrame(this, wxT("Echo Client"));
    // Show it
    frame->Show(true);
    // Start the event loop
    return true;
}

// --- in deze methode zit de crash --- //
// VVVVVVVVVVVVVVVVVVVVVV //
void EchoClient::StartClient() {
    wxMessageBox("Creating socket");
    wxSocketClient* client2 = new wxSocketClient(); // --- dit gaat goed
    wxMessageBox("Assigning second pointer");
    client = client2; // --- hier crash
    
    wxMessageBox("Creating socket address");
    wxIPV4address addr;
    addr.Hostname(wxT("127.0.0.1"));
    addr.Service(5000);
    
    client->Notify(true);
    
    wxMessageBox("Connecting socket");
    client->Connect(addr, false);
}


frame.cpp
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "main.hpp"

// Event table for MyFrame
BEGIN_EVENT_TABLE(EchoClientFrame, wxFrame)
    EVT_MENU(wxID_ABOUT, EchoClientFrame::OnAbout)
    EVT_MENU(wxID_EXIT,  EchoClientFrame::OnQuit)
END_EVENT_TABLE()

void EchoClientFrame::OnAbout(wxCommandEvent& event) {
    wxMessageBox(wxT("This is a graphical client to the echo service on port 7"), wxT("About"), wxOK | wxICON_INFORMATION, this);
}

void EchoClientFrame::OnQuit(wxCommandEvent& event) {
    Close();
}

EchoClientFrame::EchoClientFrame(EchoClient* application, const wxString& title) : wxFrame(NULL, wxID_ANY, title) {
    
    #include "mondrian.xpm"
    
    this->SetIcon(mondrian_xpm);
    
    this->SetSizeHints( wxDefaultSize, wxDefaultSize );
    
    menuBar = new wxMenuBar( 0 );
    wxMenu* menu;
    menu = new wxMenu();
    wxMenuItem* itemAbout = new wxMenuItem( menu, wxID_ABOUT, wxString( wxT("About") ) + wxT('\t') + wxT("F1"), wxEmptyString, wxITEM_NORMAL );
    menu->Append( itemAbout );
    wxMenuItem* itemExit = new wxMenuItem( menu, wxID_EXIT, wxString( wxT("Exit") ) + wxT('\t') + wxT("Alt-X"), wxEmptyString, wxITEM_NORMAL );
    menu->Append( itemExit );
    menuBar->Append( menu, wxT("Menu") );
    this->SetMenuBar( menuBar );
    
    wxBoxSizer* vBox1;
    vBox1 = new wxBoxSizer( wxVERTICAL );
    
    outputListBox = new wxListBox( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, NULL, 0 ); 
    vBox1->Add( outputListBox, 1, wxALL|wxEXPAND, 5 );
    
    wxBoxSizer* hBox1;
    hBox1 = new wxBoxSizer( wxHORIZONTAL );
    
    inputField = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    hBox1->Add( inputField, 1, wxALL, 5 );
    
    sendButton = new wxButton( this, wxID_ANY, wxT("Send"), wxDefaultPosition, wxDefaultSize, 0 );
    hBox1->Add( sendButton, 0, wxALL, 5 );
    
    vBox1->Add( hBox1, 0, wxEXPAND, 5 );
    
    this->SetSizer( vBox1 );
    this->Layout();
    
    outputListBox->Append("Connecting to 127.0.0.1 at port 5000");
    app->StartClient();
}

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 07:20

.oisyn

Moderator Devschuur®

Demotivational Speaker

EchoClientFrame::app blijft ongeinitialiseerd, je bent waarschijnlijk vergeten de 'application' parameter in de EchoClientFrame constructor eraan te assignen. Daardoor roep je StartClient() aan op een ongeinitaliseerde pointer, wat fout gaat zodra je in die memberfunctie wat met je eigen data probeert te doen. Had je natuurlijk zelf ook wel achter kunnen komen met wat debugwerk.

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


  • 2playgames
  • Registratie: Februari 2005
  • Laatst online: 01-06 15:19
Je hebt gelijk, ik ben vergeten om de parameter application op te slaan in app (terwijl ik dit soort constructies in Java toch al tig keer gemaakt heb :|)

En geloof me, ik ben niet de beroerdste om zelf wat dingen uit te zoeken (ik weet hoe dit wereldje werkt :+ ), maar ik heb vandaag voor het eerst de debugger gebruikt (in C++, that is), en ben al blij dat ik de segmentation fault heb gevonden 8)7

[ Voor 15% gewijzigd door 2playgames op 26-04-2007 00:37 ]