System.AccessViolationException: Attempted to read or write protected memory. |
|
Author |
Message |
erikkl2000
|
Posted: Common Language Runtime, System.AccessViolationException: Attempted to read or write protected memory. |
Top |
I have experienced much grief trying to figure out what to do about this error.
I have a project that make exstencive use of the Shell. And while working with this shell project in a stand alone project it is perfect.. Works like a champ.. Now when i get ready to intergrate the shell project into my main project i start getting weird jit errors and memory not accessable errors.. My other controls in the main form even start throwing errors when this shell project is added...
The only thing that i can think of is that there is a memory location is being overwritten while the address is in use by some other pointer.
Here is one of the main spots in my code that this is a for sure error thrower when the _ItemActivate even event is raised when an listitem is doubleClicked in the listView..
Here is the code
Orgin of error ---------------------
if (contextMenu.Handle != null)
Marshal.FreeCoTaskMem(contextMenu.Handle);
------------------------------------------------------
-----I know this is a lot of code ; however , i am hoping that someone can look and at least tell me what to look for because i have been on this puppy all weekend and have posted this into two other forms with no luck..
void FileView_ItemActivate(object sender, EventArgs e)
{
TreeNode parent = br.FolderView.SelectedNode;
if (parent != null && br.FileView.ArrayList_SelectedOrder.Count > 0)
{
ListViewItem listViewItem = (ListViewItem)br.FileView.ArrayList_SelectedOrder[0];
ShellItem shellItem = (ShellItem)listViewItem.Tag;
int startIndex = 0;
if ((Control.ModifierKeys & Keys.Alt) == 0)
{
if (shellItem.IsFolder)
{
if (!parent.IsExpanded)
parent.Expand();
br.FolderView.SelectedNode = parent.Nodes[listViewItem.Text];
startIndex = 1;
}
if (br.FileView.ArrayList_SelectedOrder.Count - startIndex > 0)
{
#region Fields
IntPtr[] pidls = new IntPtr[br.FileView.ArrayList_SelectedOrder.Count - startIndex];
for (int i = startIndex; i < pidls.Length; i++)
{
pidls[i - startIndex] = (( ShellItem)((ListViewItem)br.FileView.ArrayList_SelectedOrder).Tag).PIDLRel.Ptr;
}
IntPtr icontextMenuPtr = IntPtr.Zero, context2Ptr = IntPtr.Zero, context3Ptr = IntPtr.Zero;
ContextMenu contextMenu = new ContextMenu();
ShellItem parentShellItem = (ShellItem)parent.Tag;
IShellFolder parentShellFolder = parentShellItem.ShellFolder;
#endregion
#region Show / Invoke
try
{
if (ContextMenuHelper.GetIContextMenu(parentShellFolder, pidls, out icontextMenuPtr, out iContextMenu))
{
iContextMenu.QueryContextMenu(
contextMenu.Handle,
0,
ShellAPI.CMD_FIRST,
ShellAPI.CMD_LAST,
ShellAPI.CMF.DEFAULTONLY);
int defaultCommand = ShellAPI.GetMe****faultItem(contextMenu.Handle, false, 0);
if (defaultCommand >= ShellAPI.CMD_FIRST)
{
ContextMenuHelper.InvokeCommand(
iContextMenu,
( uint)defaultCommand - ShellAPI.CMD_FIRST,
ShellItem.GetRealPath(parentShellItem),
Control.MousePosition);
}
}
}
#endregion
catch (Exception) { }
#region Finally
finally
{
if (iContextMenu != null)
{
Marshal.ReleaseComObject(iContextMenu);
iContextMenu = null;
}
if (contextMenu.Handle != null)
Marshal.FreeCoTaskMem(contextMenu.Handle);
Marshal.Release(icontextMenuPtr);
}
#endregion
}
}
else
{
IntPtr[] pidls = new IntPtr[br.FileView.ArrayList_SelectedOrder.Count - startIndex];
for (int i = startIndex; i < pidls.Length; i++)
{
pidls[i - startIndex] = (( ShellItem)((ListViewItem)br.FileView.ArrayList_SelectedOrder).Tag).PIDLRel.Ptr;
}
ContextMenuHelper.InvokeCommand(shellItem.ParentItem, pidls, "properties", Control.MousePosition);
}
}
}
************** Exception Text ************** System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.NativeWindow.DefWndProc(Message& m) at System.Windows.Forms.Control.DefWndProc(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ListView.WndProc(Message& m) at BrowserComponets.BrowserListView.WndProc(Message& m) in G:\Documents and Settings\Erik\My Documents\Visual Studio 2005\Projects\SuperDesktop\SuperIE\BrowserComponets\Controls\BrowserListView.cs:line 132 at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
thanks a lot for any help..
.NET Development11
|
|
|
|
|
erikkl2000
|
Posted: Common Language Runtime, System.AccessViolationException: Attempted to read or write protected memory. |
Top |
I have isolated this as much as posiable and this does not seem to happen when i completely remove the system.windows.forms.webbrowser from any situation that i place it up against with my other projects...
Is there any way to isolate this webbrowser so that it does not interfear with my other projects
erik
|
|
|
|
|
MVP User
|
Posted: Common Language Runtime, System.AccessViolationException: Attempted to read or write protected memory. |
Top |
Why do you call call FreeCoTaskMem on a menu
handle This is calling for disaster, the handle is not pointing to allocated
memory. You don't own the handle either, so what are you trying to
achieve
Willy.
...
I have experienced much grief trying to figure out what to do about this
error.
I have a project that make exstencive use of the Shell. And while working
with this shell project in a stand alone project it is perfect.. Works like a
champ.. Now when i get ready to intergrate the shell project into my main
project i start getting weird jit errors and memory not accessable errors.. My
other controls in the main form even start throwing errors when this shell
project is added...
The only thing that i can think of is that there is a memory location is
being overwritten while the address is in use by some other pointer.
Here is one of the main spots in my code that this is a for sure error
thrower when the _ItemActivate even event is raised when an listitem is
doubleClicked in the listView..
Here is the code
Orgin of error ---------------------
if
(contextMenu.Handle != null)
Marshal.FreeCoTaskMem(contextMenu.Handle);
------------------------------------------------------
-----I know this is a lot of code ; however , i am hoping that someone can
look and at least tell me what to look for because i have been on this puppy
all weekend and have posted this into two other forms with no luck..
void
FileView_ItemActivate(object sender, EventArgs e)
{
TreeNode parent =
br.FolderView.SelectedNode;
if (parent !=
null &&
br.FileView.ArrayList_SelectedOrder.Count > 0)
{
ListViewItem
listViewItem = (ListViewItem)br.FileView.ArrayList_SelectedOrder[0];
ShellItem shellItem =
(ShellItem)listViewItem.Tag;
int startIndex =
0;
if ((Control.ModifierKeys &
Keys.Alt) == 0)
{
if
(shellItem.IsFolder)
{
if
(!parent.IsExpanded)
parent.Expand();
br.FolderView.SelectedNode = parent.Nodes[listViewItem.Text];
startIndex = 1;
}
if
(br.FileView.ArrayList_SelectedOrder.Count - startIndex > 0)
{
#region Fields
IntPtr[] pidls =
new IntPtr[br.FileView.ArrayList_SelectedOrder.Count - startIndex];
for (int i = startIndex; i <
pidls.Length; i++)
{
pidls[i - startIndex] = (( ShellItem)((ListViewItem)br.FileView.ArrayList_SelectedOrder[i]).Tag).PIDLRel.Ptr;
}
IntPtr
icontextMenuPtr = IntPtr.Zero, context2Ptr = IntPtr.Zero, context3Ptr = IntPtr.Zero;
ContextMenu
contextMenu = new
ContextMenu();
ShellItem
parentShellItem = (ShellItem)parent.Tag;
IShellFolder
parentShellFolder = parentShellItem.ShellFolder;
#endregion
#region Show / Invoke
try
{
if (ContextMenuHelper.GetIContextMenu(parentShellFolder, pidls, out icontextMenuPtr, out iContextMenu))
{
iContextMenu.QueryContextMenu(
contextMenu.Handle,
0,
ShellAPI.CMD_FIRST,
ShellAPI.CMD_LAST,
ShellAPI.CMF.DEFAULTONLY);
int defaultCommand =
ShellAPI.GetMe****faultItem(contextMenu.Handle, false, 0);
if (defaultCommand
>= ShellAPI.CMD_FIRST)
{
ContextMenuHelper.InvokeCommand(
iContextMenu,
( uint)defaultCommand
- ShellAPI.CMD_FIRST,
ShellItem.GetRealPath(parentShellItem),
Control.MousePosition);
}
}
}
#endregion
catch (Exception) { }
#region Finally
finally
{
if (iContextMenu !=
null)
{
Marshal.ReleaseComObject(iContextMenu);
iContextMenu = null;
}
if
(contextMenu.Handle != null)
Marshal.FreeCoTaskMem(contextMenu.Handle);
Marshal.Release(icontextMenuPtr);
}
#endregion
}
}
else
{
IntPtr[] pidls =
new IntPtr[br.FileView.ArrayList_SelectedOrder.Count - startIndex];
for (int i = startIndex; i <
pidls.Length; i++)
{
pidls[i - startIndex] = (( ShellItem)((ListViewItem)br.FileView.ArrayList_SelectedOrder[i]).Tag).PIDLRel.Ptr;
}
ContextMenuHelper.InvokeCommand(shellItem.ParentItem, pidls, "properties", Control.MousePosition);
}
}
}
************** Exception Text **************
System.AccessViolationException: Attempted to read or write protected
memory. This is often an indication that other memory is corrupt. at
System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr
hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at
System.Windows.Forms.NativeWindow.DefWndProc(Message& m) at
System.Windows.Forms.Control.DefWndProc(Message& m) at
System.Windows.Forms.Control.WndProc(Message& m) at
System.Windows.Forms.ListView.WndProc(Message& m) at
BrowserComponets.BrowserListView.WndProc(Message& m) in G:\Documents and
Settings\Erik\My Documents\Visual Studio
2005\Projects\SuperDesktop\SuperIE\BrowserComponets\Controls\BrowserListView.cs:line
132 at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
thanks a lot for any help..
|
|
|
|
|
erikkl2000
|
Posted: Common Language Runtime, System.AccessViolationException: Attempted to read or write protected memory. |
Top |
I am still learning with the marshall class and shell... I was searching for a nice way for my users to nav filesystem and i found one at codeproject.. And for the most part it has been a good learning experience and also seems to be exactly what i am looking for...
I have now got some time in this puppy and would like to know what it is that is causing this weird error...
This fileBrowser that i am using works great untill i add a webbrowser control and then as soon as i run the project the i get slapped with an error... The error that says ," Would you like to send this to MS" there is no way that i can debug this error..
Here is the site that i have gotten the browser..
http://www.codeproject.com/useritems/FileBrowser.asp df=100&forumid=330355&select=1682793&msg=1682793
Out of the projects listed their that is one that is in its simplest form called the TreeView..
Just download that and run and you;ll see it runs ok... Then add a webbrowser control and this is where the error is at..
Can someone please see if they can reproduce this error and tell me where to look/./.... Or how to look for this type of error
thanks a lot
erik
|
|
|
|
|
erikkl2000
|
Posted: Common Language Runtime, System.AccessViolationException: Attempted to read or write protected memory. |
Top |
I have been able to get this exection comming from the entry of my program... I am not sure why i get the error here..
System.Runtime.InteropServices.SEHException was unhandled Message="External component has thrown an exception." Source="System.Windows.Forms" ErrorCode=-2147467259 StackTrace: at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessag
eLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at TestApp.Program.Main() in G:\Documents and Settings\Erik\Desktop\FolderView\TestApp\Program.cs:line 17 at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()
|
|
|
|
|
erikkl2000
|
Posted: Common Language Runtime, System.AccessViolationException: Attempted to read or write protected memory. |
Top |
Below i have uploaded a shell project that works perfectly.. When i add a WebBrowser to the project then my application will crash with memory currupt errors... I have emailed the author about this and this does not happen to him... ]
In my projects state below it will throw an memory error any time after runtime begins and you can count on it once events start like.. click a link in the webbrowser or click on a tree node in the folder view.
I am hopeing that someone can reproduce this problem and give me some trouble shooting tips..
http://www.constructionsupercenter.com/SoftWare/Help/Visual%20Studio%202005/Memory/CodeProject/TreeView-Only.zip
erik
|
|
|
|
|
duray
|
Posted: Common Language Runtime, System.AccessViolationException: Attempted to read or write protected memory. |
Top |
|
|
|
|
|