Skip to content

Commit

Permalink
Check for null pointers in a few more places, avoiding crashes (mostl…
Browse files Browse the repository at this point in the history
…y with new OpenDSS users)
  • Loading branch information
PMeira committed Feb 8, 2024
1 parent 113c80e commit a269244
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
9 changes: 3 additions & 6 deletions src/CAPI/CAPI_Transformers.pas
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ procedure Transformers_Get_WdgVoltages(var ResultPtr: PDouble; ResultCount: PAPI
var
elem: TObj;
begin
if not _activeObj(DSSPrime, elem) then
if (not _activeObj(DSSPrime, elem)) or MissingSolution(DSSPrime) then
begin
DefaultResult(ResultPtr, ResultCount);
Exit;
Expand All @@ -521,7 +521,7 @@ procedure Transformers_Get_WdgCurrents(var ResultPtr: PDouble; ResultCount: PAPI
elem: TObj;
NumCurrents: Integer;
begin
if not _activeObj(DSSPrime, elem) then
if (not _activeObj(DSSPrime, elem)) or MissingSolution(DSSPrime) then
begin
DefaultResult(ResultPtr, ResultCount);
Exit;
Expand Down Expand Up @@ -594,17 +594,14 @@ procedure Transformers_Get_LossesByType(var ResultPtr: PDouble; ResultCount: PAP
CResult: PComplexArray; // this array is one-based, see DSSUcomplex
elem: TObj;
begin
if not _activeObj(DSSPrime, elem) then
if (not _activeObj(DSSPrime, elem)) or MissingSolution(DSSPrime) then
begin
DefaultResult(ResultPtr, ResultCount);
Exit;
end;

DSS_RecreateArray_PDouble(ResultPtr, ResultCount, 2 * 3);

if not elem.Enabled then
Exit;

CResult := PComplexArray(ResultPtr);
elem.GetLosses(CResult[1], CResult[2], CResult[3]);
// Keep the results in VA (NOT kVA) for consistency with CktElement_Get_Losses
Expand Down
5 changes: 5 additions & 0 deletions src/Common/Circuit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2266,6 +2266,11 @@ function TDSSCircuit.Get_Losses: Complex;
begin
// Return total losses in all PD Elements
Result := 0;
if (Solution.NodeV = NIL) then
begin
Exit;
end;

for pdelem in PDElements do
begin
if pdelem.enabled then
Expand Down
3 changes: 3 additions & 0 deletions src/Common/CktElement.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,9 @@ procedure TDSSCktElement.ComputeVterminal;
nref: PInteger;
nv0, nv: PDouble;
begin
if NodeRef = NIL then
Exit;

vterm := PDouble(VTerminal);
nref := PInteger(NodeRef);
nv0 := PDouble(ActiveCircuit.solution.NodeV);
Expand Down
14 changes: 14 additions & 0 deletions src/PDElements/AutoTrans.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,9 @@ procedure TAutoTransObj.GetAllWindingCurrents(CurrBuffer: pComplexArray);
ITerm_NL: pComplexArray;

begin
if (not Enabled) or (NodeRef = NIL) or (ActiveCircuit.Solution.NodeV = NIL) then
Exit;

try
Vterm := Allocmem(SizeOf(Complex) * 2 * NumWindings);
Iterm := Allocmem(SizeOf(Complex) * 2 * NumWindings);
Expand Down Expand Up @@ -1604,6 +1607,9 @@ procedure TAutoTransObj.GetWindingVoltages(iWind: Integer; VBuffer: pComplexArra
var
i, ii, k, NeutTerm: Integer;
begin
if (not Enabled) or (NodeRef = NIL) or (ActiveCircuit.Solution.NodeV = NIL) then
Exit;

try
// return Zero if winding number improperly specified
if (iWind < 1) or (iWind > NumWindings) then
Expand Down Expand Up @@ -1669,6 +1675,14 @@ procedure TAutoTransObj.GetLosses(var TotalLosses, LoadLosses, NoLoadLosses: Com
cTempIterminal: pComplexArray;
i: Integer;
begin
if (not FEnabled) or (NodeRef = NIL) then
begin
TotalLosses := 0;
LoadLosses := 0;
NoLoadLosses := 0;
Exit;
end;

// Calculates losses in watts, vars
TotalLosses := Losses; // Side effect: computes Iterminal

Expand Down
9 changes: 9 additions & 0 deletions src/PDElements/Reactor.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1019,8 +1019,17 @@ procedure TReactorObj.GetLosses(var TotalLosses, LoadLosses, NoLoadLosses: Compl
i: Integer;
v: Complex;
begin
if (not FEnabled) or (NodeRef = NIL) then
begin
TotalLosses := 0;
LoadLosses := 0;
NoLoadLosses := 0;
Exit;
end;

// Only report No Load Losses if Rp defined and Reactor is a shunt device;
// Else do default behavior.

if (RpSpecified and IsShunt and (Rp <> 0.0)) then
begin
TotalLosses := Losses; // Side effect: computes Iterminal and Vterminal
Expand Down
5 changes: 4 additions & 1 deletion src/PDElements/Transformer.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,9 @@ procedure TTransfObj.GetWindingVoltages(iWind: Integer; VBuffer: pComplexArray);
var
i, ii, k, NeutTerm: Integer;
begin
if (not Enabled) or (NodeRef = NIL) or (ActiveCircuit.Solution.NodeV = NIL) then
Exit;

// return Zero if winding number improperly specified
if (iWind < 1) or (iWind > NumWindings) then
begin
Expand Down Expand Up @@ -1632,7 +1635,7 @@ procedure TTransfObj.GetLosses(var TotalLosses, LoadLosses, NoLoadLosses: Comple
cTempIterminal: pComplexArray;
i: Integer;
begin
if not FEnabled then
if (not FEnabled) or (NodeRef = NIL) then
begin
TotalLosses := 0;
LoadLosses := 0;
Expand Down

0 comments on commit a269244

Please sign in to comment.