When developing with OPC client, you need to pay attention to memory leakage from the following aspects:
(1) All returned pointers must be released with cotaskmemfree, for example:
[Delphi] view plaincopy
Function group_tryadditems (agroupintf: iopcitemmgt; aitemcount: integer; aitems: popcitemdefarray): popcitemresultarray;
VaR
Verrors: presultlist;
Begin
Result: = nil;
Verrors: = nil;
If agroupintf. additems (aitemcount, psafearray (aitems), result, verrors) <> s_ OK then
Begin
Cotaskmemfree (result );
Result: = nil;
End;
Cotaskmemfree (verrors );
End;
(2) After Reading data using the isyncread interface, release the data with variantclear, for example:
[Delphi] view plaincopy
Function group_readitem2 (agroupintf: iopcitemmgt; aitemserverhandle: opchandle; out aquality: Word): olevariant;
VaR
Verrors: presultlist;
Vopcsyncio: iopcsyncio;
Itemvalues: popcitemstatearray;
Begin
Olecheck (agroupintf. QueryInterface (iid_iopcsyncio, vopcsyncio ));
Try
Opcerrorcheck (vopcsyncio. Read (opc_ds_device, 1, @ aitemserverhandle, itemvalues, verrors ));
Try
Opcresulterrorscheck (1, nil, null, verrors );
Result: = itemvalues ^ [0]. vdatavalue;
Aquality: = itemvalues ^ [0]. wquality;
Finally
Variantclear (itemvalues ^ [0]. vdatavalue); // Memory Leak
Cotaskmemfree (itemvalues );
End;
Finally
Vopcsyncio: = nil;
End;
End;