When developing with OPC client, you need to pay attention to memory leakage from the following aspects:

Source: Internet
Author: User

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.