C# Treeview error Attempted to read or write protected memory

2

I have a treeview and when I press my up arrow which should move the node up I get randomly the error Attempted to read or write protected memory. Theo following code is my up button click event

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
}

The output gives me the following. First-chance exception at 0x77202c16 (ntdll.dll) in phoenix.client.teller.start.exe: 0xC0000005: Access violation reading location 0x000002c4. A first chance exception of type 'System.AccessViolationException' occurred in System.Windows.Forms.dll An exception of type 'System.AccessViolationException' occurred in System.Windows.Forms.dll but was not handled in user code

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

The thread 'Win32 Thread' (0x1cb4) has exited with code 0 (0x0). The thread 'Win32 Thread' (0x1804) has exited with code 0 (0x0). Step into: Stepping over non-user code 'System.Environment.GetResourceStringLocal' Step into: Stepping over non-user code 'System.Reflection.TargetInvocationException.TargetInvocationException' An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

Additional information: Exception has been thrown by the target of an invocation.

The thread 'Win32 Thread' (0x1ca4) has exited with code 0 (0x0). The thread 'Main Thread' (0x1c84) has exited with code 0 (0x0). The thread 'Win32 Thread' (0xedc) has exited with code 0 (0x0). The thread 'Win32 Thread' (0x468) has exited with code 0 (0x0). The thread 'Win32 Thread' (0x104c) has exited with code 0 (0x0). The thread 'Win32 Thread' (0x1d88) has exited with code 0 (0x0). The thread 'Win32 Thread' (0x1d0c) has exited with code 0 (0x0). The thread 'Win32 Thread' (0x1d04) has exited with code 0 (0x0). The thread 'Win32 Thread' (0x1cc8) has exited with code 0 (0x0). The thread 'Win32 Thread' (0x1cc0) has exited with code 0 (0x0). The thread 'Win32 Thread' (0x1cac) has exited with code 0 (0x0). The thread 'Win32 Thread' (0x1ca0) has exited with code 0 (0x0). The thread 'Win32 Thread' (0x1d10) has exited with code 0 (0x0). The thread 'Win32 Thread' (0x1d08) has exited with code 0 (0x0). The thread 'Win32 Thread' (0x1ccc) has exited with code 0 (0x0).

It wont pull up the stacktrace when I am settingt he below options debug unmanaged

Call Stack ntdll.dll!_KiFastSystemCallRet@0()
ntdll.dll!_ZwWaitForSingleObject@12() + 0xc bytes
KernelBase.dll!_WaitForSingleObjectEx@12() + 0x6c bytes
kernel32.dll!_WaitForSingleObjectExImplementation@12() + 0x43 bytes
msvcr100_clr0400.dll!@_EH4_CallFilterFunc@8() + 0x12 bytes user32.dll!_InternalCallWinProc@20() + 0x23 bytes
user32.dll!_UserCallWinProcCheckWow@32() + 0xb3 bytes
user32.dll!DispatchClientMessage@20() + 0x4b bytes
user32.dll!
__fnDWORD@4() + 0x24 bytes ntdll.dll!_KiUserCallbackDispatcher@12() + 0x2e bytes
user32.dll!_DispatchClientMessage@20()
user32.dll!_NtUserMessageCall@28() + 0xc bytes user32.dll!_SendMessageWorker@20() - 0x3853 bytes
user32.dll!_SendMessageW@16() + 0x49 bytes [External Code]

Phoenix.Client.AdGbFavorite.dll!Phoenix.Client.AdGbFavorite.frmFavorites.pbUp_Click(object sender, Phoenix.Windows.Forms.PActionEventArgs e) Line 404 + 0x21 bytes C#

c#
treeview
runtime-error
asked on Stack Overflow Sep 26, 2012 by Spafa9 • edited Sep 26, 2012 by Spafa9

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0