GribApiDotNet / GribApi.NET

A powerful .NET library for reading and writing GRIB 1 and 2 files
Apache License 2.0
54 stars 28 forks source link

An unhandled exception of type 'System.AccessViolationException' occurred in Grib.Api.dll #18

Closed VishmayShah closed 8 years ago

VishmayShah commented 8 years ago

I Find a special case when this exception is thrown in my code i have

function1() { try { using (GribFile gribFile = new GribFile(readPath)) { //some work gribFile.Dispose(); GC.Collect(); GC.WaitForPendingFinalizers(); } } catch (Exception ex) { GC.Collect(); GC.WaitForPendingFinalizers(); } } function2() { XDocument doc=XDocument.Parse(bigxml); } void main() { function1(); function2();

}

the above main is executed and works perfectly when we have complete file,both functions are executed properly. but when readPath Files is empty(0KB), the execution goes to catch statement with error This file is empty or invalid. and function1 completes.when function2 is called and it works with big data, it is throwing

An unhandled exception of type 'System.AccessViolationException' occurred in Grib.Api.dll

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

function2 has not any call to gribFile

if you can replicate this case please resolve this issue, i am using V6.0.3, sounds like jump to catch does not dispose gribFile completly Thank you

0x1mason commented 8 years ago

Ok, I'll take a look at it. Do you need a temporary workaround? Should be simple.

You shouldn't need to make any of those calls to GC. Dispose gets called when control leaves the using block. That in turn calls GC::SuppressFinalize. Furthermore, Collect only collects managed memory.

0x1mason commented 8 years ago

For now, you can use FileInfo::Length to check the file length before creating the GribFile. https://msdn.microsoft.com/en-us/library/system.io.fileinfo.length(v=vs.110).aspx

VishmayShah commented 8 years ago

Thanks for quick resolution, i will implement it and reply . but this is not complete because reason of going to catch may be other like Half downloaded file will throw error of end of file reached Still my temp solution is done thank you

VishmayShah commented 8 years ago

and also dispose and collect was used just for purpose of safety nothing else

VishmayShah commented 8 years ago

do we have any progress in resolution of this bug?

0x1mason commented 8 years ago

Not yet. Is the workaround working for you? On Dec 30, 2015 5:54 AM, "VishmayShah" notifications@github.com wrote:

do we have any progress in resolution of this bug?

— Reply to this email directly or view it on GitHub https://github.com/0x1mason/GribApi.NET/issues/18#issuecomment-167976456 .

VishmayShah commented 8 years ago

Half downloaded file will throw error of end of file reached. so not 100% working

0x1mason commented 8 years ago

Ok, sorry about that. I'll try to get to it by Monday. Until then, try the following:

  1. Download a hex editor, eg HxD
  2. Open a complete file and check the very last characters. They're probably "7777". If not, check the spec for your GRIB version and find out the terminating characters for your GRIB message.
  3. In addition to the FileInfo::Length check, read the file and check for the terminating characters.
VishmayShah commented 8 years ago

(37 37 37 37)7777 i am getting at last point grib2 is my version

0x1mason commented 8 years ago

If you check for that in the downloads, that will tell you if you have a good file or not.

0x1mason commented 8 years ago

Hi Vishmay,

Thanks for your patience. Can you try installing 0.6.4-beta and let me know if it resolves your issues?

You should get a FileLoadException for the empty file and a GribApiException for an incomplete file. You'll have to handle those exceptions as you would other valid exceptions. However, you shouldn't get any AccessViolationExceptions.

0x1mason commented 8 years ago

@VishmayShah I'm hoping this is resolved for you. If not, let me know.

0x1mason commented 8 years ago

For the actual 0.6.4 release, I've changed it so both situations throw FileLoadException. I also hardened the EOF check for 7777.

VishmayShah commented 8 years ago

i am not sure on will it work or not as i am not upgrading it now as it is used. the origin of problem was that in try catch block if flow goes to catch block then gribapi was not disposing

so we think that not let flow go in catch, so that check for empty file if not then try to read else don’t initialize grib api same was needed for half downloaded files (that is pending in my proj) so FileLoadException will properly dispose grib.api for 7777 is required. is it solving it? and how to use it?

From: Eric Millin Sent: Sunday, January 24, 2016 5:10 AM To: 0x1mason/GribApi.NET Cc: VishmayShah Subject: Re: [GribApi.NET] An unhandled exception of type 'System.AccessViolationException' occurred in Grib.Api.dll (#18)

For the actual 0.6.4 release, I've changed it so both situations throw FileLoadException. I also hardened the EOF check for 7777.

— Reply to this email directly or view it on GitHub.

VishmayShah commented 8 years ago

Thank you for your great responce

0x1mason commented 8 years ago

If your current solution works for you, then I would not make any changes. Here's how you would use the new changes:

SomeType data = null;
try
{
  using (GribFile file = new GribFile("mygrib.grb"))
  {
    // set `data`
  }
} catch (FileLoadException e)
{
  Console.WriteLine("The file is empty or incomplete: " + e.Message);
}

if (data == null)
{
  // handle failure
}