-
problem with pointers
I am developing a simple personal finance program. In this program I have a class (CAccountViewer) that simply displays the transactions from a particular account and also allows to enter new transactions. I have another class (CAccountManager) that maintains a list of accounts in a std::map. When the user clicks on an account control transfers to CAccountViewer and the account viewer requests a pointer to the account that the user selected from CAccountManager and saves that pointer as a member variable. This all works fine but when the I attempt to add a new transaction I get a seg fault. Debbuging shows me that the address that the pointer points to becomes invalid somewhere but I can't figure out where. Here is the relivant code.
this is where the user selects an account to view :
Code:
void CHomeFrame::OnListBoxSelect(wxCommandEvent &event)
{
int sel = m_pAccountListBox->GetSelection();
if (sel != -1)
{
wxString AccountName = m_pAccountListBox->GetString(sel);
m_StateManager.AddNewState(new CAccountViewer(this, &m_AccountManager, AccountName) );
}
}
control then transfers to the account viewer here :
Code:
CAccountViewer::CAccountViewer(wxWindow *pParent, CAccountManager *pAM, wxString &AccountName)
: wxPanel(pParent, ID_DATAWINDOW)
{
SetOwnBackgroundColour(wxColour(100, 100, 100) );
m_Balance = 0.0;
m_AccountNames = pAM->GetAccountNames();
m_pAccount = pAM->GetAccount(AccountName);
m_pGrid = new wxGrid(this, ID_DATAWINDOW, wxDefaultPosition, wxDefaultSize, wxVSCROLL);
if (!m_pGrid->CreateGrid(0, 9) ) SHOWERROR("Could not create grid");
m_pGrid->BeginBatch();
m_pAccount->ResetTransactionCounter();
while (m_pAccount->HasMoreTransactions() )
{
AddNewRow(m_pAccount->GetNextTransaction(), false);
}
m_pGrid->EndBatch();
// set up the button panel
m_pButtonPanel = new wxPanel(this, ID_BUTTONPANEL);
m_pButtonPanel->SetBackgroundColour(wxColour(200, 200, 200) );
// set up the buttons
wxButton *pBtnCheck = new wxButton(m_pButtonPanel, IDB_CHECK, wxT("Check") );
wxButton *pBtnDebit = new wxButton(m_pButtonPanel, IDB_DEBIT, wxT("Debit") );
wxButton *pBtnDeposit = new wxButton(m_pButtonPanel, IDB_DEPOSIT, wxT("Deposit") );
wxButton *pBtnATM = new wxButton(m_pButtonPanel, IDB_ATM, wxT("ATM") );
wxButton *pBtnTransfer = new wxButton(m_pButtonPanel, IDB_TRANSFER, wxT("Transfer") );
wxBoxSizer *pButtonSizer = new wxBoxSizer(wxVERTICAL);
pButtonSizer->Add(new wxStaticText(m_pButtonPanel, wxID_ANY, wxT("Enter a new Transaction") ),
1,
wxALIGN_LEFT | wxALL, 3);
pButtonSizer->Add(pBtnCheck, 1, wxALIGN_LEFT | wxALL, 3);
pButtonSizer->Add(pBtnDebit, 1, wxALIGN_LEFT | wxALL, 3);
pButtonSizer->Add(pBtnDeposit, 1, wxALIGN_LEFT | wxALL, 3);
pButtonSizer->Add(pBtnATM, 1, wxALIGN_LEFT | wxALL, 3);
pButtonSizer->Add(pBtnTransfer, 1, wxALIGN_LEFT | wxALL, 3);
m_pButtonPanel->SetSizer(pButtonSizer);
wxBoxSizer *pVSizer = new wxBoxSizer(wxVERTICAL);
pVSizer->Add(m_pGrid, 4, wxALIGN_CENTER | wxALL | wxEXPAND, 5);
pVSizer->Add(m_pButtonPanel, 1, wxALIGN_CENTER | wxALL | wxEXPAND, 5);
SetSizer(pVSizer);
// attach handlers
// mouse right click on grid
Connect(ID_DATAWINDOW, wxEVT_GRID_CELL_RIGHT_CLICK,
wxGridEventHandler(CAccountViewer::OnTransactionRightClick) );
// popup command menu from grid
Connect(IDM_VIEW_TRANSACTION, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(CAccountViewer::OnViewTransaction) );
Connect(IDM_VIEW_IMAGES, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(CAccountViewer::OnViewImages) );
Connect(IDM_EDIT_TRANSACTION, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(CAccountViewer::OnEditTransaction) );
Connect(IDM_DELETE_TRANSACTION, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(CAccountViewer::OnDeleteTransaction) );
Connect(IDM_CHANGE_TRANSACTION_STATUS, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(CAccountViewer::OnChangeTransactionStatus) );
// buttons
pBtnCheck->Connect(IDB_CHECK, wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(CAccountViewer::OnNewTransaction) );
pBtnDebit->Connect(IDB_DEBIT, wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(CAccountViewer::OnNewTransaction) );
pBtnDeposit->Connect(IDB_DEPOSIT, wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(CAccountViewer::OnNewTransaction) );
pBtnATM->Connect(IDB_ATM, wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(CAccountViewer::OnNewTransaction) );
pBtnTransfer->Connect(IDB_TRANSFER, wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(CAccountViewer::OnNewTransaction) );
}
and when one of the new transaction buttons are clicked :
Code:
void CAccountViewer::OnNewTransaction(wxCommandEvent &event)
{
bool IsXfer = false;
CTransactionViewer *pTransactionViewer;
STransaction Transaction;
Transaction.TransactionNumber = m_pAccount->GetNewTransactionNumber();
Transaction.TransferNumber = 0;
Transaction.AccountName = m_pAccount->GetAccountName();
Transaction.Cleared = TS_OPEN;
In red is where the seg faults occur. I don't quite understand this since m_pAccount is a valid pointer in the constructor for CAccountViewer and then suddenly is not??!??