Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / March 2006

Tip: Looking for answers? Try searching our database.

AccessViolationException: Attempted to read or write protected memory

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Thomas Spellman - 16 Mar 2006 03:24 GMT
I'm trying to use libusb-win32 (http://libusb-win32.sourceforge.net/)
via #libusb (http://www.icsharpcode.net/opensource/sharpusblib/) with
.NET 2.0 on Windows XP SP2.  I keep getting an exception from a
function, usb_get_busses().  It works the first time I call it, but
throws an exception if I call it after usb_get_busses():

Got Exception (Form1->parseBusses): System.AccessViolationException:
Attempted to read or write protected memory. This is often an indication
that other memory is corrupt.
  at USBlib.Internal.NativeMethods.usb_find_busses()

USBlib.Internal.NativeMethods contains the interop wrappers around the c
functions (code from #libusb):

[DllImport("libusb0.dll", CallingConvention = CallingConvention.Cdecl,
ExactSpelling = true), SuppressUnmanagedCodeSecurity]
public static extern int usb_find_busses();

[DllImport("libusb0.dll", CallingConvention = CallingConvention.Cdecl,
ExactSpelling = true), SuppressUnmanagedCodeSecurity]
public static extern usb_bus usb_get_busses();

The c function, usb_find_busses updates a global pointer, *usb_busses,
within the c code, and usb_get_busses() returns it (code from libusb-win32):

//usb.c

struct usb_bus *usb_busses = NULL;

int usb_find_busses(void)
{
 // updates the usb_busses pointer to point to a linked list of busses
}

struct usb_bus *usb_get_busses(void)  //returns the global pointer

Why is this crashing?  How do I fix it?  There should be no problm
calling usb_get_busses() as many times as desired.  Is this the wrong
way to deal with a return value of a pointer to a struct?

T
Thomas Spellman - 19 Mar 2006 22:12 GMT
I'm getting so many different problems from this code.  Sometimes it
just works, and if it works, it keeps working.  BUT, sometimes I get an
AccessViolationException (reading or writing protected memory),
sometimes i get a stack overrun, sometimes it just quits, sometimes it
freezes.  Sometimes the exception popup points to the interop function,
sometimes it points to the dispose function, sometimes to the
Application.Run function.  How should I debug this interop stuff?  How
do I know what code in the DLL is the problem?  I have the code for it.
Is it possible to bedug both the CLR and the native code?  I only have
C# Express, do I need the Pro edition for this?  I have VS2003 Pro,
would that be better for debugging this interop stuff?

T

> I'm trying to use libusb-win32 (http://libusb-win32.sourceforge.net/)
> via #libusb (http://www.icsharpcode.net/opensource/sharpusblib/) with
[quoted text clipped - 37 lines]
>
> T

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.