albar965 / littlenavmap

Little Navmap is a free flight planner, navigation tool, moving map, airport search and airport information system for Flight Simulator X, Microsoft Flight Simulator 2020, Prepar3D and X-Plane.
https://albar965.github.io/littlenavmap.html
GNU General Public License v3.0
1.27k stars 163 forks source link

Handle leak when having LNM open but no simulator running #891

Open tomatosalat0 opened 2 years ago

tomatosalat0 commented 2 years ago

Hi,

first of all, thank you for this lovely tool - I really like it a lot. But: I've noticed that LNM has a handle leak.

Environment:

Steps to reproduce:

Observations:

I hope you can reproduce the issue with the steps mentioned above.

albar965 commented 2 years ago

Thank you for the detailed report! Very good.

I can reproduce this with increasing number of handles in the Task Manager. Unfortunately I do not see anything in the Resource Monitor which could also tell me what handles are left.

I have to dig into the SimConnect code and check if something is not closed there.

tomatosalat0 commented 2 years ago

Great to hear that it is not only on my side.

I just tried to find out which kind of handle is leaking and it seems like its an Event handle leak (probably created by CreateEventA, CreateEventW or CreateEventEx). Unfortunately the handle itself doesn't have a name (or at least the tool I used didn't show any).

I only know about htrace to track the creation call stack of every handle, but I don't have the Driver SDK installed on my machine to check for that by myself right now.

I would probably need a lot of time to get into LNM source code, so I can't give you any more detailed hint right now.

albar965 commented 2 years ago

This is a SimConnect issue. Handles get lost as soon as I call SC_Open. Calling SC_Close in case of failure makes no sense since the resulting handle is NULL anyway.

No other parts of LNM cause this. No handles leaked as soon as I comment in SC_Open.

No idea how to fix this. Using latest MSFS SimConnect requires a 64 bit build. 32 bit build is kept to have FSX support.

albar965 commented 2 years ago

No idea how to work around this. Maybe unload and reload the whole DLL.

tomatosalat0 commented 2 years ago

Unloading the DLL won't fix the issue. Handles are bound to the process in Windows afaik - so a process restart is the only option to unload the handles.

I just thought about it as well and got four ideas for a work-around (and do be honest, all of them are quite ugly in my opinion):

Those are the options which just came to my mind. Don't know if you really want to implement any of those though.

albar965 commented 2 years ago

Thank you for the hints. Very helpful.

Creating a separate executable ...

Overhead. Another program to maintain and build.

Create hooks into the methods ...

Sounds tempting but also dangerous. You never know what you're closing there. BTW: It's not the main thread but there is only one thread dealing with the connection.

Default your AutoConnect option to false ...

Not really but I can stop autoconnect (not disable the option, just stop) after half an hour or so and show a warning dialog. Or resume once LNM is activated, etc. Or increase reconnect time for each unsuccessful attempt.

Add an FSUIPC connection-check method ...

Overhead and support (need to help people installing). I know FSUIPC from FSX but I'd rather not use this path.

The hooks and the warning seem feasible.

Again thanks for the help. I'll see what can be done.

Alex

albar965 commented 2 years ago

Postponing since workaround 2629de452118862dc9f2881fc84abf6652676563 mitigates the issue.