View Issue Details

IDProjectCategoryView StatusLast Update
0002338NoesisGUIC# SDKpublic2022-05-30 18:19
ReporterAndreN Assigned Tosfernandez  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Product Version3.1.4 
Target Version3.1.5Fixed in Version3.1.5 
Summary0002338: LocalFontProvider is not closing Stream and thus running into errors
DescriptionHello

When working with the LocalFontProvider from the NoesisApp namespace, I noticed an issue.

* When I specify a font in my XAML file like for example this this FontFamily="Fonts/#Aero Matics", it starts scanning the whole Fonts folder and registers all fonts files found there.
   Those fonts got read by the method OpenFont from the LocalFontProvider, which generates a stream with new FileStream(...):

public override Stream OpenFont(Uri folder, string id) => (Stream) new FileStream(System.IO.Path.Combine(this._basePath, folder.GetPath(), id), FileMode.Open);

 * I think this is done, in order to understand, which fonts are available in the directory. The fonts itself get not really loaded at this time.
    But afterwards it loads again this one file, where the intended font is inside. Here, I think the font is now really getting loaded. However now an exception occurs:

Unhandled exception. System.IO.IOException: The process cannot access the file '<path-to-gui-files>\Fonts\Aero Matics Regular.ttf' because it is being used by another process.
   at Main.Program.<>c.<Main>b__0_0(Exception exception) in <project-dir>\Main\Main\Program.cs:line 49
   at Noesis.Error.UnhandledException(Exception exception)
   at Noesis.Extend.ProviderOpenFont(IntPtr cPtr, IntPtr folder, IntPtr filename)
   at Noesis.View.Noesis_View_SetSize(HandleRef view, Int32 width, Int32 height)
   at Noesis.View.SetSize(Int32 width, Int32 height)
...

* I think the reason for this is, that the stream of the font file has not been closed yet.
   If I use instead inside the OpenFont(...) method the method OpenRead(...) for opening the stream it works and no exception occurs (and the font is being displayed):

public override Stream OpenFont(Uri folder, string id) => (Stream) File.OpenRead(System.IO.Path.Combine(this.rootPath, folder.GetPath(), id));

* The difference between OpenFont(...) and new FileStream(...) is, that OpenFont(...) creates the stream like this:

new FileStream(<path>, FileMode.Open, FileAccess.Read, FileShare.Read);

* So, the essential part here is FileStream.Read and actually, if you also add FileAccess.Read, FileShare.Read the the implementation in LocalFontProvider it also works without exception.

Nevertheless, there is still the question: Is it intended, that the file-stream is not being closed after the first read (scan of the directory)? If so, why is the stream not reused internally instead of reading the same file again?

So I'm not completely sure, if "just settings the file share to read mode" is really a fix or a workaround? What do you think about this?

P.S.: I don't know how to do (inline) code formating in this ticket description.
TagsNo tags attached.
PlatformWindows

Activities

sfernandez

sfernandez

2022-05-30 18:19

manager   ~0007947

Thanks for the information, we opted to use OpenRead in the providers. And also fixed the implementation of RegisterFont to close the stream after it is read.

Issue History

Date Modified Username Field Change
2022-04-28 15:18 AndreN New Issue
2022-04-28 15:21 AndreN Description Updated
2022-04-28 15:59 sfernandez Assigned To => sfernandez
2022-04-28 15:59 sfernandez Status new => assigned
2022-04-28 15:59 sfernandez Target Version => 3.1.5
2022-05-30 18:19 sfernandez Status assigned => resolved
2022-05-30 18:19 sfernandez Resolution open => fixed
2022-05-30 18:19 sfernandez Fixed in Version => 3.1.5
2022-05-30 18:19 sfernandez Note Added: 0007947