The code we just described is fully functional, but, it can be strengthened, specifically with the function, AlbumDao::albums()
. In this function, we iterate through the database rows and create a new Album
to fill a list. We can zoom in on this specific code section:
QVector<Album*> list; while(query.next()) { Album* album = new Album(); album->setId(query.value("id").toInt()); album->setName(query.value("name").toString()); list.append(album); } return list;
Let's say that the name
column has been renamed to title
. If you forget to update query.value("name")
, you might run into trouble. The Qt framework does not rely on exceptions, but this cannot be said for every API available in the wild. An exception here would cause a memory leak: the Album* album
function has been allocated on the heap but not released. To handle this, you would have to surround the risky code with a try
catch
statement and deallocate the...