I’m new-ish to the SDK, I’ve been trying to run ClearDataDefinition on all my registered simvars for when a user attempts to either disconnect from SimConnect or close my program. However, I am consistently getting CTDs when I try to do this.
This might be really straightforward, but for the love of god, it’s driving me insane. My program is written in managed C# and I’ve seen that you ideally need to prevent the simvar from sending events and then run ClearDataDefinition. I’m measuring throttle positions and therefore a lot of my registered simvars have a period of every sim frame as opposed to a second.
I’m simply running this code below against an array of typeIds and requestIds I stored when I first registered them.
this.simConnect!.RequestDataOnSimObject(
eRequestId, etypeId, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.NEVER,
SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0,0,0),
ex => new InternalException($"Failed to invoke 'RequestDataOnSimObject(...)'.", ex));
this.simConnect!.ClearDataDefinition(eTypeId);
However, I am consistently getting CTDs when I try to clear the data definition. I appreciate I’ve probably overlooked something major or whatnot but any pointers in the right direction would be appreciated. I’m pulling my hair out
I also appreciate the fact this has been posted on the main forums, I completely overlooked the devsupport forums dedicated to developing with the SDK.
Both MSFS and my program just crash, MSFS first then my program with no trail even in the debugger. I have the .dmp file for MSFS and this is what that says, I understand it’s not much help.
Ah so MSFS itself is crashing. That’s a bug for Asobo since the remote calls shouldn’t be able to trigger a title crash. You might want to just collect that MSFS .dmp file and re-file as a bug following the guidelines here: How to report a bug or crash
You RequestData and then ClearData right after, in a tight loop? W/out even getting a chance to read the data? I’m not surprised it crashes. Keep in mind these are async requests.
Inside my SimConnect context, SimVarId holds all the currently registered events and aircraft is a derived class that performs SimConnect requests and listens to SimConnect events:
public void Disconnect()
{
foreach (IAircraft aircraft in aircrafts)
{
aircraft.Dispose();
}
aircrafts.Clear();
if (simCon != null)
{
foreach (SimVarId sid in SimVarIds)
{
simCon.StopReceivingPrimitiveData(sid.RequestId, sid.TypeId);
}
simCon.Close();
simCon = null!;
}
}
The code below relates to the simCon variable in the code above:
public void Close()
{
if (this.simConnect == null) return;
var primitiveTypes = primitiveManager.GetRegisteredTypesWithSimVarNames();
primitiveTypes.ForEach(primitive => UnregisterPrimitive(primitive.id, primitive.simVarName));
this.primitiveManager.RemoveAll();
var types = typeManager.GetRegisteredTypes();
types.ForEach(q => UnregisterType(q));
var eventIds = eventManager.Select(q => q.EventId).Select(q => (int)q).ToList();
eventIds.ForEach(q => UnregisterSystemEvent(q));
this.simConnect.Dispose();
this.simConnect = null;
this.winHandleManager.Dispose();
Logger.LogMethodEnd();
}
public void UnregisterPrimitive(int typeId, string simVarName)
{
EnsureConnected();
EEnum eTypeId = (EEnum)typeId;
System.Diagnostics.Debug.WriteLine("Unregistering primitive type {0}", eTypeId);
Try(() =>
{
this.simConnect!.ClearDataDefinition(eTypeId);
}, ex => new InternalException($"Failed to unregister primitive type {eTypeId}.", ex));
}
public void StopReceivingPrimitiveData(int requestId, int typeId)
{
EnsureConnected();
EEnum eRequestId = (EEnum)requestId;
EEnum etypeId = (EEnum)typeId;
Try(() =>
this.simConnect!.RequestDataOnSimObject(
eRequestId, etypeId, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.NEVER,
SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0,0,0),
ex => new InternalException($"Failed to invoke 'RequestDataOnSimObject(...)'.", ex));
Logger.LogMethodEnd();
}
I understand the issue here is a timing issue, but what is the best way to ensure that the definition has stopped sending events, is there some sort of indicator at all I can use to know that I can then call ClearDataDefinition?