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: Select all
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) );
}
}
Code: Select all
void CStateManager::AddNewState(wxWindow *newWindow)
{
// do not push NULL values
if (newWindow)
{
wxWindow *oldWindow = *m_StateList.begin();
m_StateList.push_front(newWindow);
m_pParentSizer->Replace(oldWindow, newWindow);
m_pParentSizer->Layout();
oldWindow->Show(false);
newWindow->Show(true);
newWindow->Refresh();
}
else
{
SHOWERROR("Cannot push NULL value");
}
}
Code: Select all
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
//....
}
Code: Select all
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;
//...
}