如何使用wxTreeItemData而不会在删除时崩溃

时间:2015-03-16 17:39:46

标签: c++ c++11 wxwidgets

我正在使用wxWidgets,并且我有一个wxTreeCtrl设置了一个root和一个孩子。我正在设置孩子的客户端数据,它可以正常运行,直到窗口关闭,此时我收到一个总线错误,我假设它来自wxWidgets试图删除我的客户端数据。

这是我的客户数据结构:

struct MapPtrCtr {
  Map* map;

  MapPtrCtr(Map* map) : map(map) {}
};

以下是我如何设置客户数据(请注意,此处mapstd::shared_ptr<Map>):

wxTreeItemId childNode = mapTree->AppendItem(node, map->getTitle());
mapTree->SetItemData(childNode, (wxTreeItemData*) new MapPtrCtr(map.get()));

这是我得到的错误:

Process:               AromatherapyMapEditor [43832]
Path:                  /Users/USER/*/AromatherapyMapEditor
Identifier:            AromatherapyMapEditor
Version:               0
Code Type:             X86-64 (Native)
Parent Process:        bash [805]
Responsible:           Terminal [798]
User ID:               501

Date/Time:             2015-03-16 13:38:02.964 -0400
OS Version:            Mac OS X 10.10.1 (14B25)
Report Version:        11
Anonymous UUID:        3887BB6D-7783-EBA5-C86C-FACE1C21094E

Sleep/Wake UUID:       9C9670EA-BBC7-4046-B544-F1CE3F184398

Time Awake Since Boot: 84000 seconds
Time Since Wake:       12000 seconds

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGBUS)
Exception Codes:       KERN_PROTECTION_FAILURE at 0x00007fb920f1ae80

VM Regions Near 0x7fb920f1ae80:
    MALLOC_TINY            00007fb920d00000-00007fb920f00000 [ 2048K] rw-/rwx SM=PRV  
--> MALLOC_TINY            00007fb920f00000-00007fb921000000 [ 1024K] rw-/rwx SM=COW  
    MALLOC_SMALL           00007fb921000000-00007fb921800000 [ 8192K] rw-/rwx SM=COW  

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   ???                             0x00007fb920f1ae80 0 + 140433098387072
1   libwx_osx_cocoau_core-3.0.dylib 0x00000001075f6a26 wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl*) + 94
2   libwx_osx_cocoau_core-3.0.dylib 0x00000001075fabe2 wxGenericTreeCtrl::Delete(wxTreeItemId const&) + 264
3   libwx_osx_cocoau_core-3.0.dylib 0x00000001075fac64 wxGenericTreeCtrl::DeleteAllItems() + 40
4   libwx_osx_cocoau_core-3.0.dylib 0x00000001075f925c wxGenericTreeCtrl::~wxGenericTreeCtrl() + 102
5   libwx_osx_cocoau_core-3.0.dylib 0x000000010759f96b wxTreeCtrl::~wxTreeCtrl() + 15
6   libwx_osx_cocoau_core-3.0.dylib 0x00000001075a5da0 wxWindowBase::Destroy() + 40
7   libwx_osx_cocoau_core-3.0.dylib 0x00000001075a5e96 wxWindowBase::DestroyChildren() + 20
8   libwx_osx_cocoau_core-3.0.dylib 0x0000000107438d79 wxWindow::~wxWindow() + 215
9   libwx_osx_cocoau_core-3.0.dylib 0x00000001075effeb wxSplitterWindow::~wxSplitterWindow() + 15
10  libwx_osx_cocoau_core-3.0.dylib 0x00000001075a5da0 wxWindowBase::Destroy() + 40
11  libwx_osx_cocoau_core-3.0.dylib 0x00000001075a5e96 wxWindowBase::DestroyChildren() + 20
12  libwx_osx_cocoau_core-3.0.dylib 0x0000000107435121 wxNonOwnedWindow::~wxNonOwnedWindow() + 45
13  AromatherapyMapEditor           0x000000010739f913 MapeditFrame::~MapeditFrame() + 259 (frame.h:24)
14  AromatherapyMapEditor           0x000000010739e1f5 MapeditFrame::~MapeditFrame() + 21 (frame.h:24)
15  AromatherapyMapEditor           0x000000010739e219 MapeditFrame::~MapeditFrame() + 25 (frame.h:24)
16  libwx_baseu-3.0.dylib           0x000000010795ec72 wxAppConsoleBase::DeletePendingObjects() + 74
17  libwx_baseu-3.0.dylib           0x000000010795ebd3 wxAppConsoleBase::ProcessIdle() + 85
18  libwx_osx_cocoau_core-3.0.dylib 0x00000001074dcef6 wxAppBase::ProcessIdle() + 22
19  libwx_osx_cocoau_core-3.0.dylib 0x000000010746be5a wxApp::ProcessIdle() + 26
20  libwx_baseu-3.0.dylib           0x0000000107984faa wxEventLoopBase::ProcessIdle() + 28
21  libwx_baseu-3.0.dylib           0x0000000107a0d661 wxCFEventLoop::CommonModeObserverCallBack(__CFRunLoopObserver*, int) + 69
22  com.apple.CoreFoundation        0x00007fff8db02d87 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
23  com.apple.CoreFoundation        0x00007fff8db02ce0 __CFRunLoopDoObservers + 368
24  com.apple.CoreFoundation        0x00007fff8daf4f1a __CFRunLoopRun + 1178
25  com.apple.CoreFoundation        0x00007fff8daf4838 CFRunLoopRunSpecific + 296
26  com.apple.HIToolbox             0x00007fff926dd43f RunCurrentEventLoopInMode + 235
27  com.apple.HIToolbox             0x00007fff926dd1ba ReceiveNextEventCommon + 431
28  com.apple.HIToolbox             0x00007fff926dcffb _BlockUntilNextEventMatchingListInModeWithFilter + 71
29  com.apple.AppKit                0x00007fff8b6906d1 _DPSNextEvent + 964
30  com.apple.AppKit                0x00007fff8b68fe80 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 194
31  com.apple.AppKit                0x00007fff8b683e23 -[NSApplication run] + 594
32  libwx_osx_cocoau_core-3.0.dylib 0x00000001074bdd8c wxGUIEventLoop::OSXDoRun() + 106
33  libwx_baseu-3.0.dylib           0x0000000107a0db35 wxCFEventLoop::DoRun() + 39
34  libwx_baseu-3.0.dylib           0x0000000107984ed8 wxEventLoopBase::Run() + 88
35  libwx_baseu-3.0.dylib           0x000000010795ea04 wxAppConsoleBase::MainLoop() + 128
36  libwx_osx_cocoau_core-3.0.dylib 0x000000010746bea8 wxApp::OnRun() + 26
37  libwx_baseu-3.0.dylib           0x00000001079b33ae wxEntry(int&, wchar_t**) + 71
38  AromatherapyMapEditor           0x000000010738d706 main + 38 (main.cpp:15)
39  libdyld.dylib                   0x00007fff916bf5c9 start + 1

有什么建议吗?我不确定如何正确使用wxTreeItemData,因为它的文档非常少。文档说要确保用new初始化结构,我这样做,所以我不确定为什么会出现问题。

1 个答案:

答案 0 :(得分:0)

那很快。我想出了如何正确使用wxTreeItemData:你必须子类它,而不仅仅是强制转换它。以下客户端数据类有效:

class MapPtrCtr : public wxTreeItemData {
  public:
    Map* map;

    MapPtrCtr(Map* map) : map(map) {}
};