Bug 1039 - appears to call method twice when only 1 delegate this cause sharing violation
Summary: appears to call method twice when only 1 delegate this cause sharing violation
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 1.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2011-09-25 12:02 UTC by john murray
Modified: 2011-09-25 20:32 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

VS2010 project folder for passwind demonstrating issue (2.12 MB, application/octet-stream)
2011-09-25 12:02 UTC, john murray

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description john murray 2011-09-25 12:02:18 UTC
Created attachment 488 [details]
VS2010 project folder for passwind demonstrating issue

from a form with 
4 buttons and one spinner 
a method is called with different date parameters (dependent on button or spinner selection
GetWind(ds)  ds is the date time selection 

this checks to see if a copy of the wx chart for this date time has already been downloaded and saved
if not it downloads a copy and saves it, otherwise it uses the saved version (caching by another name) 
Works fine if one comments out the line saveBMP line 433 wxClass4.cs
(though I havent stress tested it) nevertheless using the debugger it seems to go through the code in GetWindmap() twice
this is where the error arises - when it reaches the savebmp it seems to think th e file is already open and throws a sharing violation (on the second pass) 
It also seems to be downlodding the bitmap a second time which is no good
Comment 1 Jonathan Pobst 2011-09-25 20:32:46 UTC
It looks like you aren't disposing or closing the FileStream you create in WXClass5.cs at line ~444.  The easiest way to ensure things always get disposed is to use the using pattern:

using (var fs = new System.IO.FileStream (filename, FileMode.Create))
  WXclass5.windmap.Compress (Bitmap.CompressFormat.Png, 85, fs);

This ensures the FileStream will be disposed as soon as it goes out of scope, even if an exception occurs.

As for GetWind being called twice, if you check the call stack of where it is called from, you will see that the first time it is called from the button.Click, and the second time it is called from sp1.ItemSelected, because your button calls sp1.SetSelection(nn).