Commit 681895c
committed
fix: resolve null pointer crash when TextCalculator exits
1. Changed m_dataModel and m_calculator member variables from raw
pointers to QPointer<QAbstractItemModel> and QPointer<TextCalculator> to
automatically track object destruction
2. Added onDataModelDestroyed() slot to handle model destruction:
clear m_dataModel pointer, emit dataModelChanged() signal, and reset
calculated widths to prevent stale pointer access
3. Extracted width reset logic into resetCalculatedWidths() method to
avoid code duplication and ensure consistent state cleanup
4. In scheduleCalculation(), call resetCalculatedWidths() when model is
null or disabled to properly reset cached values
5. In setEnabled(false), replaced inline width reset with
resetCalculatedWidths() call for consistency
6. In destructor, always call disconnectDataModelSignals() without null
check, as QPointer ensures safe disconnection even if model is destroyed
7. In TextCalculatorAttached::setCalculator(), added guard for duplicate
calculator assignment and proper disconnect of previous calculator
signals to prevent double connections
8. Always call updateElidedText() even when calculator is null to ensure
text is properly cleared
9. Added QPointer include in header for QPointer usage
Log: Fixed null pointer crash during TextCalculator exit in dock task
panel
Influence:
1. Test dock task panel close and exit operations to verify no crashes
occur
2. Verify task item text display updates correctly when tasks change
3. Test with rapid task creation/deletion scenarios to check memory
safety
4. Verify elided text updates properly when calculator is reassigned
5. Test disabled state transitions to confirm width values are reset
correctly
fix: 修复 TextCalculator 退出时的野指针崩溃问题
1. 将 m_dataModel 和 m_calculator 成员变量从原始指针改为
QPointer<QAbstractItemModel> 和 QPointer<TextCalculator>,自动追踪对象
销毁
2. 新增 onDataModelDestroyed() 槽函数处理模型销毁:清除 m_dataModel 指
针、发射 dataModelChanged() 信号并重置计算宽度,防止野指针访问
3. 将宽度重置逻辑提取为 resetCalculatedWidths() 方法,避免代码重复并确保
状态一致清理
4. 在 scheduleCalculation() 中,当模型为空或禁用时调用
resetCalculatedWidths() 正确重置缓存值
5. 在 setEnabled(false) 中,将内联宽度重置替换为 resetCalculatedWidths()
调用,保持一致性
6. 在析构函数中,始终调用 disconnectDataModelSignals() 而不进行空检查,
因为 QPointer 确保即使模型已销毁也能安全断开连接
7. 在 TextCalculatorAttached::setCalculator() 中,添加重复计算器赋值防
护,并正确断开前一个计算器的信号连接,防止重复连接
8. 即使计算器为空也始终调用 updateElidedText(),确保正确清理文本
9. 在头文件中添加 QPointer 包含以支持 QPointer 使用
Log: 修复 Dock 任务面板 TextCalculator 退出时的野指针崩溃问题
Influence:
1. 测试 dock 任务面板的关闭和退出操作,验证无崩溃发生
2. 验证任务项文本显示在任务变更时正确更新
3. 测试快速创建/删除任务场景,检查内存安全性
4. 验证当计算器重新赋值时省略文本正确更新
5. 测试禁用状态切换,确认宽度值正确重置1 parent 96c0307 commit 681895c
2 files changed
Lines changed: 54 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
34 | | - | |
35 | | - | |
| 33 | + | |
36 | 34 | | |
37 | 35 | | |
38 | 36 | | |
| |||
113 | 111 | | |
114 | 112 | | |
115 | 113 | | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
| 114 | + | |
120 | 115 | | |
121 | 116 | | |
122 | 117 | | |
| |||
137 | 132 | | |
138 | 133 | | |
139 | 134 | | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
144 | 139 | | |
| 140 | + | |
145 | 141 | | |
146 | 142 | | |
147 | 143 | | |
| |||
157 | 153 | | |
158 | 154 | | |
159 | 155 | | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
160 | 163 | | |
161 | 164 | | |
162 | 165 | | |
163 | 166 | | |
164 | 167 | | |
165 | 168 | | |
| 169 | + | |
166 | 170 | | |
167 | 171 | | |
168 | 172 | | |
169 | 173 | | |
170 | 174 | | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
171 | 191 | | |
172 | 192 | | |
173 | 193 | | |
| |||
372 | 392 | | |
373 | 393 | | |
374 | 394 | | |
375 | | - | |
376 | | - | |
377 | | - | |
378 | | - | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
379 | 408 | | |
| 409 | + | |
| 410 | + | |
380 | 411 | | |
381 | 412 | | |
382 | 413 | | |
| |||
422 | 453 | | |
423 | 454 | | |
424 | 455 | | |
425 | | - | |
| 456 | + | |
426 | 457 | | |
427 | 458 | | |
428 | 459 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
58 | | - | |
| 59 | + | |
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
| |||
159 | 160 | | |
160 | 161 | | |
161 | 162 | | |
| 163 | + | |
162 | 164 | | |
163 | 165 | | |
164 | 166 | | |
165 | 167 | | |
166 | 168 | | |
167 | 169 | | |
| 170 | + | |
168 | 171 | | |
169 | 172 | | |
170 | 173 | | |
| |||
178 | 181 | | |
179 | 182 | | |
180 | 183 | | |
181 | | - | |
| 184 | + | |
182 | 185 | | |
183 | 186 | | |
184 | 187 | | |
| |||
0 commit comments