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 / September 2006

Tip: Looking for answers? Try searching our database.

Memory problems - something wrong with my code?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris  Ashley - 28 Sep 2006 13:49 GMT
I have been getting 'access violation reading location' errors
occasionally in my app. I have debugged this and found it is one of my
interop functions. Am I doing anything wrong here? The unmanaged
function takes a pointer to some bytes in memory and a long, and
returns a pointer which points somewhere into those bytes based on the
long value.

My interop code is like so:

   [DllImport("ImageFile.dll", EntryPoint =
"?PB_SkipBytes@@YAPAEPAEJ@Z", CallingConvention =
CallingConvention.Cdecl)]

   public static extern IntPtr PB_SkipBytes(IntPtr pIn, int
bytesToSkip);

public static Byte[] SkipBytes(Byte[] bytToDecompress, int
intBytesToSkip)
   {
       IntPtr pbData;
       IntPtr pbByteArray;

       System.Diagnostics.Debug.WriteLine("Entering Skipbytes
function");
       // Pin the array in memory so the garbage collector can't move
it around
       System.Diagnostics.Debug.WriteLine("About to pin array");
       GCHandle pinnedArray = GCHandle.Alloc(bytToDecompress,
GCHandleType.Pinned);
       System.Diagnostics.Debug.WriteLine("Pinned array -
byttodecompress length is " + bytToDecompress.Length);

       // Get a pointer to the beginning of the pinned byte array
       pbByteArray = pinnedArray.AddrOfPinnedObject();
       System.Diagnostics.Debug.WriteLine("Got pointer");

       // Call the skip bytes function and get a pointer to the
remaining uncompressed data
       System.Diagnostics.Debug.WriteLine("Skipping " + intBytesToSkip
+ " bytes");
       pbData = PB_SkipBytes(pbByteArray, intBytesToSkip);
       System.Diagnostics.Debug.WriteLine("Called");

       // Calculate the size of the new managed array and allocate
       int intNewCompressedArraySize = (pbByteArray.ToInt32() +
bytToDecompress.Length) - pbData.ToInt32();
       System.Diagnostics.Debug.WriteLine("Calculating new array
size");
       byte[] bytBuffer = new byte[intNewCompressedArraySize];

       // Copy the remaining uncompressed data into the buffer
       System.Diagnostics.Debug.WriteLine("Copying to new array");
       Marshal.Copy(pbData, bytBuffer, 0, intNewCompressedArraySize);
       System.Diagnostics.Debug.WriteLine("Copied to new array");

       // return buffer and free memory
       pinnedArray.Free();
       return bytBuffer;
   }
Mattias Sjögren - 28 Sep 2006 20:33 GMT
>I have been getting 'access violation reading location' errors
>occasionally in my app. I have debugged this and found it is one of my
>interop functions. Am I doing anything wrong here?

The code looks good to me. Are you sure the error isn't in the native
code?

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Rate this thread:







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.