我有一个树视图,当我按下应该向上移动节点的向上箭头时,我随机收到错误尝试读取或写入受保护的内存。Theo 以下代码是我的向上按钮单击事件
void pbUp_Click(object sender, PActionEventArgs e)
{
int nIndex = 0;
int nPrevIndex = 0;
//Setting the node to the node being moved up.
FavoriteTreeNode node = tvFavorites.SelectedNode as FavoriteTreeNode;
nIndex = node.Index;
FavoriteTreeNode prevNode = null;
FavoriteTreeNode parentNode = null;
try
{
if (node.Level > 0 && node.Index > 0 )
{
//Retrieving the parent node of the favorite if there is on.
parentNode = node.Parent as FavoriteTreeNode;
nPrevIndex = nIndex - 1;
prevNode = parentNode.Nodes[nPrevIndex] as FavoriteTreeNode;
}
// the node is the first child of the parent we need to get the parents previous node to insert.
else if (node.Level > 0 && node.Index == 0)
{
parentNode = node.Parent as FavoriteTreeNode;
//Retrieving the previous node to the selected node being moved.
nPrevIndex = parentNode.Index - 1;
prevNode = tvFavorites.Nodes[nPrevIndex] as FavoriteTreeNode;
}
else
{
nPrevIndex = nIndex - 1;
prevNode = tvFavorites.Nodes[nPrevIndex] as FavoriteTreeNode;
}
//Setting what type of node we are dealing with a folder or favorite.
if (node.FavoriteType == FavoriteType.Folder.ToString())
{
//If the node index is greater than 0 then we know there are nodes above the
//currently selected node.
if (nIndex > 0)
{
//Retrieving the previous node to the selected node being moved.
nPrevIndex = nIndex - 1;
prevNode = tvFavorites.Nodes[nPrevIndex] as FavoriteTreeNode;
//Removing the current selected node
tvFavorites.Nodes.RemoveAt(nIndex);
//Inserting folder into the previous nodes index. The treeview automatically re-indexes the values.
tvFavorites.Nodes.Insert(nPrevIndex, node);
}
}
// We are dealing with a favorite.
else if (node.FavoriteType == FavoriteType.Favorite.ToString())
{
//If the previous node is a favorite then we can just move the node up.
if (prevNode.FavoriteType == FavoriteType.Favorite.ToString())
{
if (parentNode != null && node.Index > 0)
{
parentNode.Nodes.RemoveAt(nIndex);
parentNode.Nodes.Insert(nPrevIndex, node);
}
//The node is under a parent and is the first child.
else if (parentNode != null && node.Index == 0)
{
parentNode.Nodes.RemoveAt(nIndex);
node.ParentPtid = 0;
tvFavorites.Nodes.Insert(parentNode.Index, node);
}
else
{
//Removing the currently selected node.
tvFavorites.Nodes.RemoveAt(nIndex);
//Inserting folder into the previous nodes index. The treeview automatically re-indexes the values.
tvFavorites.Nodes.Insert(nPrevIndex, node);
}
}
else
{
if (parentNode != null)
{
//Removing node from parent node
parentNode.Nodes.RemoveAt(nIndex);
//Setting the nodes parent ptid to the folder the node is moving to ptid.
node.ParentPtid = prevNode.Ptid;
prevNode.Nodes.Add(node);
}
else
{
//Removing the current selected node
tvFavorites.Nodes.RemoveAt(nIndex);
parentNode = prevNode;
//Setting the nodes parent ptid to the folder ID to indicate the favorite belongs to the folder.
node.ParentPtid = parentNode.Ptid;
//parentNode doesn't have any children so just add the favorite to the node.
parentNode.Nodes.Add(node);
}
}
}
}
catch (Exception)
{
throw;
}
tvFavorites.ExpandAll();
node.TreeView.SelectedNode = node;
tvFavorites.Focus(); //WI#18749
}
输出给了我以下信息。phoenix.client.teller.start.exe 中 0x77202c16 (ntdll.dll) 的第一次机会异常:0xC0000005:访问冲突读取位置 0x000002c4。System.Windows.Forms.dll 中出现“System.AccessViolationException”类型的第一次机会异常 System.Windows.Forms.dll 中出现“System.AccessViolationException”类型的异常,但未在用户代码中处理
附加信息:试图读取或写入受保护的内存。这通常表明其他内存已损坏。
线程“Win32 线程”(0x1cb4) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0x1804) 已退出,代码为 0 (0x0)。步入:跨过非用户代码“System.Environment.GetResourceStringLocal” 步入:跨过非用户代码“System.Reflection.TargetInvocationException.TargetInvocationException” mscorlib 中发生了“System.Reflection.TargetInvocationException”类型的未处理异常。 dll
附加信息:调用的目标已引发异常。
线程“Win32 线程”(0x1ca4) 已退出,代码为 0 (0x0)。线程“主线程”(0x1c84) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0xedc) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0x468) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0x104c) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0x1d88) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0x1d0c) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0x1d04) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0x1cc8) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0x1cc0) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0x1cac) 已退出,代码为 0 (0x0)。线程“Win32 线程” (0x1ca0) 以代码 0 (0x0) 退出。线程“Win32 线程”(0x1d10) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0x1d08) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0x1ccc) 已退出,代码为 0 (0x0)。
当我在选项调试非托管下设置时,它不会拉起堆栈跟踪
调用堆栈 ntdll.dll!_KiFastSystemCallRet@0()
ntdll.dll!_ZwWaitForSingleObject@12() + 0xc 字节
KernelBase.dll!_WaitForSingleObjectEx@12() + 0x6c 字节
kernel32.dll!_WaitForSingleObjectExImplementation@12() + 0x43 字节
msvcr100_clr0400.dll !@_EH4_CallFilterFunc@8() + 0x12 字节 user32.dll!_InternalCallWinProc@20() + 0x23 字节
user32.dll!_UserCallWinProcCheckWow@32() + 0xb3 字节
user32.dll!DispatchClientMessage@20() + 0x4b 字节
user32.dll!__fnDWORD@4() + 0x24 字节 ntdll.dll!_KiUserCallbackDispatcher@12() + 0x2e 字节
user32.dll!_DispatchClientMessage@20()
user32.dll!_NtUserMessageCall@28() + 0xc 字节 user32.dll!_SendMessageWorker@20() - 0x3853 字节
user32.dll!_SendMessageW@16() + 0x49 字节 [外部代码]
Phoenix.Client.AdGbFavorite.dll!Phoenix.Client.AdGbFavorite.frmFavorites.pbUp_Click(object sender, Phoenix.Windows.Forms.PActionEventArgs e) Line 404 + 0x21 bytes C#