
Originally Posted by
homerun4711
The AddressBook-class has no other member using the same name. I am sure

You should still check, dereferencing a NULL pointer is bad. What works today may not work tomorrow or next month after you've made changes to the code, and checking the returned pointer will find the problem immediately. If you know that the pointer is 100% always suppose to return a value other than NULL then use can use Q_ASSERT, otherwise use if statements.

Originally Posted by
homerun4711
Well, using
delete tableView;
tableView = dmgr->customerTableView();
delete tableView;
tableView = dmgr->customerTableView();
To copy to clipboard, switch view to plain text mode
Ok, I rewrote the function returning the QTableWidget, it's working now.
I forgot to pass the correct parent. *stupid*stupid*
Soultion was
{
m_customerTableView->setModel(model_customer);
m_customerTableView
->setObjectName
(QString::fromUtf8("tableView"));
m_customerTableView
->setGeometry
(QRect(10,
10,
701,
191));
return m_customerTableView;
}
QTableView *DataManager::customerTableView(QWidget* parent)
{
m_customerTableView = new QTableView(parent);
m_customerTableView->setModel(model_customer);
m_customerTableView->setObjectName(QString::fromUtf8("tableView"));
m_customerTableView->setGeometry(QRect(10, 10, 701, 191));
return m_customerTableView;
}
To copy to clipboard, switch view to plain text mode
and to call it using
tableView = dmgr->customerTableView(this);
tableView = dmgr->customerTableView(this);
To copy to clipboard, switch view to plain text mode
This is bad juju. What happens if you call "delete tableView" without the next call to "customerTableView"? What state is m_customerTableView variable left in? How about if there are other member functions of DataManager that work with m_customerTableView and what it points to is deleted outside of the DataManger class?
A slightly better implementation than the current.
QTableView * DataManager
::DeleteCustomerTableView(void) {
delete m_customerTableView;
m_customerTableView = NULL;
return m_customerTableview;
}
{
DeleteCustomerTableView();
m_customerTableView->setModel(model_customer);
m_customerTableView
->setObjectName
(QString::fromUtf8("tableView"));
m_customerTableView
->setGeometry
(QRect(10,
10,
701,
191));
return m_customerTableView;
}
...
// delete tableView; DataManager handles the delete, this call not needed.
tableView = dmgr->customerTableView(this);
QTableView * DataManager::DeleteCustomerTableView(void)
{
delete m_customerTableView;
m_customerTableView = NULL;
return m_customerTableview;
}
QTableView *DataManager::customerTableView(QWidget* parent)
{
DeleteCustomerTableView();
m_customerTableView = new QTableView(parent);
m_customerTableView->setModel(model_customer);
m_customerTableView->setObjectName(QString::fromUtf8("tableView"));
m_customerTableView->setGeometry(QRect(10, 10, 701, 191));
return m_customerTableView;
}
...
// delete tableView; DataManager handles the delete, this call not needed.
tableView = dmgr->customerTableView(this);
To copy to clipboard, switch view to plain text mode
There is still a problem with the code above as it is possible to dereference a returned NULL pointer from new'd QTableView (out of memory).
Rules for staying out of trouble when programming in C/C++.
1) If a function returns an error code, check the returned value, never assume the function call was successful.
2) Always check returned pointers to ensure they are not NULL, handle accordingly.
3) If a class owns a pointer, don't delete the pointer outside of the class.
4) If a class hands ownership of pointer to another class, apply rule 3.
Qt's object ownership tree might blur #3 a bit. Still DataManager should handle the deletion of the object, so added DeleteCustomerTableView for this purpose.
tableView = dmgr->DeleteCustomerTableView();
tableView = dmgr->DeleteCustomerTableView();
To copy to clipboard, switch view to plain text mode
Writing defensible code is TEDIOUS WORK (actually it's second nature if you do it all time) and adds lots of extra lines of code, however the benefits are huge. When the app starts crashing during development the error will be easier to find. When the application is deployed to outside users (with varying system architecture) defensible coding will enable you to determine the problem. Defensible coding allows errors to be handled gracefully, did a new call return NULL (out of memory) then maybe some resources can be freed and new called again. Do you check the return value from QObject::connect(...), if not how do you now the connection was successful?

Originally Posted by
homerun4711
The AddressBook-class has no other member using the same name. I am sure

You should still check, dereferencing a NULL pointer is bad. What works today may not work tomorrow or next month after you've made changes to the code, and checking the returned pointer will find the problem immediately. If you know that the pointer is 100% always suppose to return a value other than NULL then use can use Q_ASSERT, otherwise use if statements.

Originally Posted by
homerun4711
Well, using
delete tableView;
tableView = dmgr->customerTableView();
delete tableView;
tableView = dmgr->customerTableView();
To copy to clipboard, switch view to plain text mode
Ok, I rewrote the function returning the QTableWidget, it's working now.
I forgot to pass the correct parent. *stupid*stupid*
Soultion was
{
m_customerTableView->setModel(model_customer);
m_customerTableView
->setObjectName
(QString::fromUtf8("tableView"));
m_customerTableView
->setGeometry
(QRect(10,
10,
701,
191));
return m_customerTableView;
}
QTableView *DataManager::customerTableView(QWidget* parent)
{
m_customerTableView = new QTableView(parent);
m_customerTableView->setModel(model_customer);
m_customerTableView->setObjectName(QString::fromUtf8("tableView"));
m_customerTableView->setGeometry(QRect(10, 10, 701, 191));
return m_customerTableView;
}
To copy to clipboard, switch view to plain text mode
and to call it using
tableView = dmgr->customerTableView(this);
tableView = dmgr->customerTableView(this);
To copy to clipboard, switch view to plain text mode
This is bad juju. What happens if you call "delete tableView" without the next call to "customerTableView"? What state is m_customerTableView variable left in? How about if there are other member functions of DataManager that work with m_customerTableView and what it points to is deleted outside of the DataManger class?
A slightly better implementation than the current.
QTableView * DataManager
::DeleteCustomerTableView(void) {
delete m_customerTableView;
m_customerTableView = NULL;
return m_customerTableview;
}
{
DeleteCustomerTableView();
m_customerTableView->setModel(model_customer);
m_customerTableView
->setObjectName
(QString::fromUtf8("tableView"));
m_customerTableView
->setGeometry
(QRect(10,
10,
701,
191));
return m_customerTableView;
}
...
// delete tableView; DataManager handles the delete, this call not needed.
tableView = dmgr->customerTableView(this);
QTableView * DataManager::DeleteCustomerTableView(void)
{
delete m_customerTableView;
m_customerTableView = NULL;
return m_customerTableview;
}
QTableView *DataManager::customerTableView(QWidget* parent)
{
DeleteCustomerTableView();
m_customerTableView = new QTableView(parent);
m_customerTableView->setModel(model_customer);
m_customerTableView->setObjectName(QString::fromUtf8("tableView"));
m_customerTableView->setGeometry(QRect(10, 10, 701, 191));
return m_customerTableView;
}
...
// delete tableView; DataManager handles the delete, this call not needed.
tableView = dmgr->customerTableView(this);
To copy to clipboard, switch view to plain text mode
There is still a problem with the code above as it is possible to dereference a returned NULL pointer from new'd QTableView (out of memory).
Rules for staying out of trouble when programming in C/C++.
1) If a function returns an error code, check the returned value, never assume the function call was successful.
2) Always check returned pointers to ensure they are not NULL, handle accordingly.
3) If a class owns a pointer, don't delete the pointer outside of the class.
4) If a class hands ownership of pointer to another class, apply rule 3.
Qt's object ownership tree might blur #3 a bit. Still DataManager should handle the deletion of the object, so added DeleteCustomerTableView for this purpose.
tableView = dmgr->DeleteCustomerTableView();
tableView = dmgr->DeleteCustomerTableView();
To copy to clipboard, switch view to plain text mode
Writing defensible code is TEDIOUS WORK (actually it's second nature if you do it all time) and adds lots of extra lines of code, however the benefits are huge. When the app starts crashing during development the error will be easier to find. When the application is deployed to outside users (with varying system architecture) defensible coding will enable you to determine the problem. Defensible coding allows errors to be handled gracefully, did a new call return NULL (out of memory) then maybe some resources can be freed and new called again. Do you check the return value from QObject::connect(...), if not how do you now the connection was successful?

Originally Posted by
homerun4711
The AddressBook-class has no other member using the same name. I am sure

You should still check, dereferencing a NULL pointer is bad. What works today may not work tomorrow or next month after you've made changes to the code, and checking the returned pointer will find the problem immediately. If you know that the pointer is 100% always suppose to return a value other than NULL then use can use Q_ASSERT, otherwise use if statements.

Originally Posted by
homerun4711
Well, using
delete tableView;
tableView = dmgr->customerTableView();
delete tableView;
tableView = dmgr->customerTableView();
To copy to clipboard, switch view to plain text mode
Ok, I rewrote the function returning the QTableWidget, it's working now.
I forgot to pass the correct parent. *stupid*stupid*
Soultion was
{
m_customerTableView->setModel(model_customer);
m_customerTableView
->setObjectName
(QString::fromUtf8("tableView"));
m_customerTableView
->setGeometry
(QRect(10,
10,
701,
191));
return m_customerTableView;
}
QTableView *DataManager::customerTableView(QWidget* parent)
{
m_customerTableView = new QTableView(parent);
m_customerTableView->setModel(model_customer);
m_customerTableView->setObjectName(QString::fromUtf8("tableView"));
m_customerTableView->setGeometry(QRect(10, 10, 701, 191));
return m_customerTableView;
}
To copy to clipboard, switch view to plain text mode
and to call it using
tableView = dmgr->customerTableView(this);
tableView = dmgr->customerTableView(this);
To copy to clipboard, switch view to plain text mode
This is bad juju. What happens if you call "delete tableView" without the next call to "customerTableView"? What state is m_customerTableView variable left in? How about if there are other member functions of DataManager that work with m_customerTableView and what it points to is deleted outside of the DataManger class?
A slightly better implementation than the current.
QTableView * DataManager
::DeleteCustomerTableView(void) {
delete m_customerTableView;
m_customerTableView = NULL;
return m_customerTableview;
}
{
DeleteCustomerTableView();
m_customerTableView->setModel(model_customer);
m_customerTableView
->setObjectName
(QString::fromUtf8("tableView"));
m_customerTableView
->setGeometry
(QRect(10,
10,
701,
191));
return m_customerTableView;
}
...
// delete tableView; DataManager handles the delete, this call not needed.
tableView = dmgr->customerTableView(this);
QTableView * DataManager::DeleteCustomerTableView(void)
{
delete m_customerTableView;
m_customerTableView = NULL;
return m_customerTableview;
}
QTableView *DataManager::customerTableView(QWidget* parent)
{
DeleteCustomerTableView();
m_customerTableView = new QTableView(parent);
m_customerTableView->setModel(model_customer);
m_customerTableView->setObjectName(QString::fromUtf8("tableView"));
m_customerTableView->setGeometry(QRect(10, 10, 701, 191));
return m_customerTableView;
}
...
// delete tableView; DataManager handles the delete, this call not needed.
tableView = dmgr->customerTableView(this);
To copy to clipboard, switch view to plain text mode
There is still a problem with the code above as it is possible to dereference a returned NULL pointer from new'd QTableView (out of memory).
Rules for staying out of trouble when programming in C/C++.
1) If a function returns an error code, check the returned value, never assume the function call was successful.
2) Always check returned pointers to ensure they are not NULL, handle accordingly.
3) If a class owns a pointer, don't delete the pointer outside of the class.
4) If a class hands ownership of pointer to another class, apply rule 3.
Qt's object ownership tree might blur #3 a bit. Still DataManager should handle the deletion of the object, so added DeleteCustomerTableView for this purpose.
tableView = dmgr->DeleteCustomerTableView();
tableView = dmgr->DeleteCustomerTableView();
To copy to clipboard, switch view to plain text mode
Writing defensible code is TEDIOUS WORK (actually it's second nature if you do it all time) and adds lots of extra lines of code, however the benefits are huge. When the app starts crashing during development the error will be easier to find. When the application is deployed to outside users (with varying system architecture) defensible coding will enable you to determine the problem. Defensible coding allows errors to be handled gracefully, did a new call return NULL (out of memory) then maybe some resources can be freed and new called again. Do you check the return value from QObject::connect(...), if not how do you now the connection was successful?
Added after 9 minutes:

Originally Posted by
nroberts
With class members this has some interesting and sometimes confusing implications.
and loops.
{
int i = 0;
.
.
.
for(int i = 0; i < someLayout->count(); ++i) {
if(someLayout->itemAt(i) == someWidget)
break;
}
someLayout
->insertWidget
(i,
new QWidget());
// oops, inserted widget at position 0}
{
int i = 0;
.
.
.
for(int i = 0; i < someLayout->count(); ++i) {
if(someLayout->itemAt(i) == someWidget)
break;
}
someLayout->insertWidget(i, new QWidget()); // oops, inserted widget at position 0
}
To copy to clipboard, switch view to plain text mode
Bookmarks