Custom WAbstractItemModel resulting in "Wt: fatal error: bad any cast"
Added by Rakesh Vidyadharan almost 1 year ago
Hello,
I have a fairly simple custom table model for displaying docker containers that are running. When I attempt to use it, the app crashes with a "Wt: fatal error: bad any cast"
message. Since the exception is handled, I have not been able to see the trace in a debugger. The source files are attached. Any idea what I am doing wrong in the model implementation?
The logs produced are as follows:
[2023-11-24 12:46:49.289552][INFO][0x16ff97000][/Users/rakesh/projects/customer/wirepulse/utils/sys-mgr/src/model/containermodel.cpp:headerData:45] Retrieving header name for column: 0
[2023-11-24 12:46:49.289655][INFO][0x16ff97000][/Users/rakesh/projects/customer/wirepulse/utils/sys-mgr/src/model/containermodel.cpp:headerData:45] Retrieving header name for column: 1
[2023-11-24 12:46:49.289761][INFO][0x16ff97000][/Users/rakesh/projects/customer/wirepulse/utils/sys-mgr/src/model/containermodel.cpp:headerData:45] Retrieving header name for column: 2
[2023-11-24 12:46:49.289869][INFO][0x16ff97000][/Users/rakesh/projects/customer/wirepulse/utils/sys-mgr/src/model/containermodel.cpp:headerData:45] Retrieving header name for column: 3
[2023-11-24 12:46:49.289970][INFO][0x16ff97000][/Users/rakesh/projects/customer/wirepulse/utils/sys-mgr/src/model/containermodel.cpp:headerData:45] Retrieving header name for column: 4
[2023-11-24 12:46:49.290075][INFO][0x16ff97000][/Users/rakesh/projects/customer/wirepulse/utils/sys-mgr/src/model/containermodel.cpp:headerData:45] Retrieving header name for column: 0
[2023-11-24 12:46:49.290178][INFO][0x16ff97000][/Users/rakesh/projects/customer/wirepulse/utils/sys-mgr/src/model/containermodel.cpp:headerData:45] Retrieving header name for column: 0
[2023-11-24 12:46:49.290282][INFO][0x16ff97000][/Users/rakesh/projects/customer/wirepulse/utils/sys-mgr/src/model/containermodel.cpp:headerData:45] Retrieving header name for column: 0
[2023-11-24 12:46:49.290383][INFO][0x16ff97000][/Users/rakesh/projects/customer/wirepulse/utils/sys-mgr/src/model/containermodel.cpp:headerData:45] Retrieving header name for column: 0
[2023-11-24 12:46:49.290486][INFO][0x16ff97000][/Users/rakesh/projects/customer/wirepulse/utils/sys-mgr/src/model/containermodel.cpp:headerData:45] Retrieving header name for column: 0
[2023-Nov-24 06:46:49.286] 84314 [/ uGD3e91zJHjsUmg6] [error] "Wt: fatal error: bad any cast"
[2023-Nov-24 06:46:49.286] 84314 - [info] "WebController: Removing session uGD3e91zJHjsUmg6"
containermodel.cpp (2.66 KB) containermodel.cpp | Model implementation | ||
containermodel.h (1.58 KB) containermodel.h | Model definition |
Replies (3)
RE: Custom WAbstractItemModel resulting in "Wt: fatal error: bad any cast" - Added by Rakesh Vidyadharan 12 months ago
I made some changes to inherit from WAbstractTableModel and also modified the columnCount
and rowCount
implementations to match how they are implemented in Dbo::QueryModel. Still get the same error.
RE: Custom WAbstractItemModel resulting in "Wt: fatal error: bad any cast" - Added by Matthias Van Ceulebroeck 12 months ago
Hi Rakesh,
This is because your data
and headerData
are not "complete". It will return a value for each Wt::ItemDataRole
, but I believe that is only desired for Display
, and Tooltip
maybe. What happens here is that is will get a value for Link
, even though it doesn't have one, resulting in a wrong cast.
I would create an early return for both, checking the condition, and returning {}
if any of the other roles are accessed.
RE: Custom WAbstractItemModel resulting in "Wt: fatal error: bad any cast" - Added by Rakesh Vidyadharan 12 months ago
Hi Matthias,
Thanks for that explanation. Yes, I was able to resolve it by restricting the logic to only Display
and Edit
roles.
if ( role != Wt::ItemDataRole::Display && role != Wt::ItemDataRole::Edit ) return {};