c# - MaxMind GeoIP Reader Dispose Issue - UnauthorizedAccessException on mmdb file following Reader.Dispose() -


i using maxmind.db.reader class access geolite2-city.mmdb file , having file access issues performing actions on file after calling dispose on reader.

the simplest way reproduce issue have console application, similar maxmind.db.benchmark project, has static instance of reader class, in case called _cityreader.

now, if set _cityreader variable new instance of reader call dispose on (i set null) move file (moving works fine) call delete on file's new location unauthorizedaccessexception on delete operation...

_cityreader = new reader(@"c:\temp\maxmind\active\geolite2-city.mmdb", fileaccessmode.memorymapped);  _cityreader.dispose(); _cityreader = null;  file.move(@"c:\temp\maxmind\active\geolite2-city.mmdb", @"c:\temp\maxmind\active\geolite2-citymoved.mmdb"); file.delete(@"c:\temp\maxmind\active\geolite2-citymoved.mmdb"); 

what i've found if use reflection call dispose _cityreader._stream.value property in scenario before calling dispose on _cityreader variable file delete...

_cityreader = new reader(@"c:\temp\maxmind\active\geolite2-city.mmdb", fileaccessmode.memorymapped);  fieldinfo field = typeof(reader).getfield("_stream", bindingflags.nonpublic | bindingflags.instance); threadlocal<stream> fieldvalue = (threadlocal<stream>)field.getvalue(_cityreader); fieldvalue.value.dispose();  _cityreader.dispose(); _cityreader = null;  file.move(@"c:\temp\maxmind\active\geolite2-city.mmdb", @"c:\temp\maxmind\active\geolite2-citymoved.mmdb"); file.delete(@"c:\temp\maxmind\active\geolite2-citymoved.mmdb"); 

but, on top of this, dispose of static reader object on different thread 1 created on , find doing bit reflection under scenario means still unauthorizedaccessexception. in scenario find need force garbage collection after disposing of _cityreader variable , setting null. reproduce one, have static method in console app this...

private static void disposereaderandswitchfiles() {     _cityreader.dispose();     _cityreader = null;      gc.collect();      //  try switching files     file.move(@"c:\temp\maxmind\active\geolite2-city.mmdb", @"c:\temp\maxmind\active\geolite2-citymoved.mmdb");     file.delete(@"c:\temp\maxmind\active\geolite2-citymoved.mmdb"); } 

then in main method of console app have this...

_cityreader = new reader(@"c:\temp\maxmind\active\geolite2-city.mmdb", fileaccessmode.memorymapped);  thread t = new thread(disposereaderandswitchfiles); t.start(); t.join();  console.writeline("press key continue..."); console.readkey(); 

so code sample work, if remove gc.collect() call disposereaderandswitchfiles method unauthorizedaccessexception.

has else experienced similar issues maxmind.db.reader object?

is there doing wrong or else should doing? feels bit dirty forcing garbage collection , rather avoid if possible.

thanks,

phil

i have issue not similar still access issue. can find here. exception while accessing maxmind's geoip-country.mmdb database through hive


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -