- .NET API: fixed memory management issue in MmsValue.SetElement (see #213)

pull/228/head
Michael Zillgith 6 years ago
parent 4aaced2639
commit 3fd4fed886

@ -534,8 +534,15 @@ namespace IEC61850
} }
/// <summary> /// <summary>
/// Sets the element of an array of structure /// Sets the element of an array or structure
/// </summary> /// </summary>
/// <remarks>
/// After calling this function the native memory of the element will be managed by the array or structure.
/// Therefore an element can only be used in a single array or structure.
/// When the value is required in multiple arrays or structures
/// a clone has to be created before using this function!
/// To be save, always use a clone for setting the element.
/// </remarks>
/// <param name="index">index of the element starting with 0</param> /// <param name="index">index of the element starting with 0</param>
/// <param name="elementValue">MmsValue instance that will be used as element value</param> /// <param name="elementValue">MmsValue instance that will be used as element value</param>
/// <exception cref="MmsValueException">This exception is thrown if the value has the wrong type.</exception> /// <exception cref="MmsValueException">This exception is thrown if the value has the wrong type.</exception>
@ -544,25 +551,35 @@ namespace IEC61850
{ {
MmsType elementType = GetType(); MmsType elementType = GetType();
if ((elementType == MmsType.MMS_ARRAY) || (elementType == MmsType.MMS_STRUCTURE)) { if ((elementType == MmsType.MMS_ARRAY) || (elementType == MmsType.MMS_STRUCTURE))
{
if ((index >= 0) && (index < Size()))
{
if ((index >= 0) && (index < Size ())) {
if (elementValue != null) if (elementValue != null)
{
MmsValue_setElement(valueReference, index, elementValue.valueReference); MmsValue_setElement(valueReference, index, elementValue.valueReference);
/* will be deleted by structure */
elementValue.responsableForDeletion = false;
}
else else
MmsValue_setElement(valueReference, index, IntPtr.Zero); MmsValue_setElement(valueReference, index, IntPtr.Zero);
} else }
else
throw new MmsValueException("Index out of bounds"); throw new MmsValueException("Index out of bounds");
} else }
else
throw new MmsValueException("Value is of wrong type"); throw new MmsValueException("Value is of wrong type");
} }
public MmsDataAccessError GetDataAccessError() public MmsDataAccessError GetDataAccessError()
{ {
if (GetType () == MmsType.MMS_DATA_ACCESS_ERROR) { if (GetType() == MmsType.MMS_DATA_ACCESS_ERROR)
{
int errorCode = MmsValue_getDataAccessError(valueReference); int errorCode = MmsValue_getDataAccessError(valueReference);
return (MmsDataAccessError)errorCode; return (MmsDataAccessError)errorCode;

@ -294,9 +294,6 @@ namespace tests
{ {
IedModel iedModel = ConfigFileParser.CreateModelFromConfigFile("../../model.cfg"); IedModel iedModel = ConfigFileParser.CreateModelFromConfigFile("../../model.cfg");
ModelNode ind1 = iedModel.GetModelNodeByShortObjectReference ("GenericIO/GGIO1.Ind1.stVal"); ModelNode ind1 = iedModel.GetModelNodeByShortObjectReference ("GenericIO/GGIO1.Ind1.stVal");
Assert.IsTrue (ind1.GetType ().Equals (typeof(IEC61850.Server.DataAttribute))); Assert.IsTrue (ind1.GetType ().Equals (typeof(IEC61850.Server.DataAttribute)));
@ -556,6 +553,45 @@ namespace tests
Assert.AreEqual (Validity.QUESTIONABLE, q.Validity); Assert.AreEqual (Validity.QUESTIONABLE, q.Validity);
} }
[Test()]
public void MmsValaueCreateStructureAndAddElement()
{
MmsValue structure1 = MmsValue.NewEmptyStructure(1);
MmsValue structure2 = MmsValue.NewEmptyStructure(1);
MmsValue element = MmsValue.NewEmptyStructure(1);
structure1.SetElement(0, element);
/* Clone is required when adding the value to another structure or element */
MmsValue elementClone = element.Clone();
structure2.SetElement(0, elementClone);
element.Dispose();
structure1.Dispose();
structure2.Dispose();
Assert.AreEqual(true, true);
}
[Test()]
public void MmsValueClone()
{
MmsValue boolValue = new MmsValue(true);
MmsValue boolClone = boolValue.Clone();
boolValue.Dispose();
boolClone.Dispose();
MmsValue structure = MmsValue.NewEmptyStructure(1);
MmsValue structureClone = structure.Clone();
structure.Dispose();
structureClone.Dispose();
}
} }
} }

Loading…
Cancel
Save