Skip to content

Commit a269244

Browse files
committed
Check for null pointers in a few more places, avoiding crashes (mostly with new OpenDSS users)
1 parent 113c80e commit a269244

File tree

6 files changed

+38
-7
lines changed

6 files changed

+38
-7
lines changed

src/CAPI/CAPI_Transformers.pas

+3-6
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ procedure Transformers_Get_WdgVoltages(var ResultPtr: PDouble; ResultCount: PAPI
494494
var
495495
elem: TObj;
496496
begin
497-
if not _activeObj(DSSPrime, elem) then
497+
if (not _activeObj(DSSPrime, elem)) or MissingSolution(DSSPrime) then
498498
begin
499499
DefaultResult(ResultPtr, ResultCount);
500500
Exit;
@@ -521,7 +521,7 @@ procedure Transformers_Get_WdgCurrents(var ResultPtr: PDouble; ResultCount: PAPI
521521
elem: TObj;
522522
NumCurrents: Integer;
523523
begin
524-
if not _activeObj(DSSPrime, elem) then
524+
if (not _activeObj(DSSPrime, elem)) or MissingSolution(DSSPrime) then
525525
begin
526526
DefaultResult(ResultPtr, ResultCount);
527527
Exit;
@@ -594,17 +594,14 @@ procedure Transformers_Get_LossesByType(var ResultPtr: PDouble; ResultCount: PAP
594594
CResult: PComplexArray; // this array is one-based, see DSSUcomplex
595595
elem: TObj;
596596
begin
597-
if not _activeObj(DSSPrime, elem) then
597+
if (not _activeObj(DSSPrime, elem)) or MissingSolution(DSSPrime) then
598598
begin
599599
DefaultResult(ResultPtr, ResultCount);
600600
Exit;
601601
end;
602602

603603
DSS_RecreateArray_PDouble(ResultPtr, ResultCount, 2 * 3);
604604

605-
if not elem.Enabled then
606-
Exit;
607-
608605
CResult := PComplexArray(ResultPtr);
609606
elem.GetLosses(CResult[1], CResult[2], CResult[3]);
610607
// Keep the results in VA (NOT kVA) for consistency with CktElement_Get_Losses

src/Common/Circuit.pas

+5
Original file line numberDiff line numberDiff line change
@@ -2266,6 +2266,11 @@ function TDSSCircuit.Get_Losses: Complex;
22662266
begin
22672267
// Return total losses in all PD Elements
22682268
Result := 0;
2269+
if (Solution.NodeV = NIL) then
2270+
begin
2271+
Exit;
2272+
end;
2273+
22692274
for pdelem in PDElements do
22702275
begin
22712276
if pdelem.enabled then

src/Common/CktElement.pas

+3
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,9 @@ procedure TDSSCktElement.ComputeVterminal;
11391139
nref: PInteger;
11401140
nv0, nv: PDouble;
11411141
begin
1142+
if NodeRef = NIL then
1143+
Exit;
1144+
11421145
vterm := PDouble(VTerminal);
11431146
nref := PInteger(NodeRef);
11441147
nv0 := PDouble(ActiveCircuit.solution.NodeV);

src/PDElements/AutoTrans.pas

+14
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,9 @@ procedure TAutoTransObj.GetAllWindingCurrents(CurrBuffer: pComplexArray);
15351535
ITerm_NL: pComplexArray;
15361536

15371537
begin
1538+
if (not Enabled) or (NodeRef = NIL) or (ActiveCircuit.Solution.NodeV = NIL) then
1539+
Exit;
1540+
15381541
try
15391542
Vterm := Allocmem(SizeOf(Complex) * 2 * NumWindings);
15401543
Iterm := Allocmem(SizeOf(Complex) * 2 * NumWindings);
@@ -1604,6 +1607,9 @@ procedure TAutoTransObj.GetWindingVoltages(iWind: Integer; VBuffer: pComplexArra
16041607
var
16051608
i, ii, k, NeutTerm: Integer;
16061609
begin
1610+
if (not Enabled) or (NodeRef = NIL) or (ActiveCircuit.Solution.NodeV = NIL) then
1611+
Exit;
1612+
16071613
try
16081614
// return Zero if winding number improperly specified
16091615
if (iWind < 1) or (iWind > NumWindings) then
@@ -1669,6 +1675,14 @@ procedure TAutoTransObj.GetLosses(var TotalLosses, LoadLosses, NoLoadLosses: Com
16691675
cTempIterminal: pComplexArray;
16701676
i: Integer;
16711677
begin
1678+
if (not FEnabled) or (NodeRef = NIL) then
1679+
begin
1680+
TotalLosses := 0;
1681+
LoadLosses := 0;
1682+
NoLoadLosses := 0;
1683+
Exit;
1684+
end;
1685+
16721686
// Calculates losses in watts, vars
16731687
TotalLosses := Losses; // Side effect: computes Iterminal
16741688

src/PDElements/Reactor.pas

+9
Original file line numberDiff line numberDiff line change
@@ -1019,8 +1019,17 @@ procedure TReactorObj.GetLosses(var TotalLosses, LoadLosses, NoLoadLosses: Compl
10191019
i: Integer;
10201020
v: Complex;
10211021
begin
1022+
if (not FEnabled) or (NodeRef = NIL) then
1023+
begin
1024+
TotalLosses := 0;
1025+
LoadLosses := 0;
1026+
NoLoadLosses := 0;
1027+
Exit;
1028+
end;
1029+
10221030
// Only report No Load Losses if Rp defined and Reactor is a shunt device;
10231031
// Else do default behavior.
1032+
10241033
if (RpSpecified and IsShunt and (Rp <> 0.0)) then
10251034
begin
10261035
TotalLosses := Losses; // Side effect: computes Iterminal and Vterminal

src/PDElements/Transformer.pas

+4-1
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,9 @@ procedure TTransfObj.GetWindingVoltages(iWind: Integer; VBuffer: pComplexArray);
15831583
var
15841584
i, ii, k, NeutTerm: Integer;
15851585
begin
1586+
if (not Enabled) or (NodeRef = NIL) or (ActiveCircuit.Solution.NodeV = NIL) then
1587+
Exit;
1588+
15861589
// return Zero if winding number improperly specified
15871590
if (iWind < 1) or (iWind > NumWindings) then
15881591
begin
@@ -1632,7 +1635,7 @@ procedure TTransfObj.GetLosses(var TotalLosses, LoadLosses, NoLoadLosses: Comple
16321635
cTempIterminal: pComplexArray;
16331636
i: Integer;
16341637
begin
1635-
if not FEnabled then
1638+
if (not FEnabled) or (NodeRef = NIL) then
16361639
begin
16371640
TotalLosses := 0;
16381641
LoadLosses := 0;

0 commit comments

Comments
 (0)