Back

Topic

[KB150]Using a Buffer with SCADA BASIC

Tags: Scada Basic, Scripting

14 years ago
By RM
Options
Print
Applies to:

PcVue all versions.
While the basic content of this article remains valid, details may have changed since it was written.


Summary:

This article gives advice on using buffers in SCADA BASIC programs. It also provides a way of checking whether an application handles the buffers correctly.


Details:

There are several SCADA BASIC functions that use buffers (e.g. SVTREND and SELECTOR).

Before using any of these functions, it is necessary to allocate a buffer (i.e. a block of memory) with ALLOC_BUFFER.

ALLOC_BUFFER returns a handle that is used subsequently for referencing the reserved memory area.

Problem

An important issue in programming is that of freeing all the memory that has been reserved. Otherwise, the application occupies excess memory which may cause erroneous operation of the operating system.

You must check that the logic of your program includes provision to free its allocated buffers in all execution conditions.

The verb FREE_BUFFER is used to free an allocated buffer.

Note

Sometimes buffers are created directly by a SCADA BASIC function (e.g. FILETOBUF). It is still necessary then to free those buffers.

Warn

You must not allocate a handle before executing the function FILETOBUF otherwise two buffers are created by PcVue.

Example

This example allocates a buffer, then frees it after use if it is still allocated:

Buf = FILETOBUF( file );

if (Buf != 0) then

FREE_BUFFER( Buf );
Buf = 0;

End if

In the example, if the file does not exist then buf is null (= 0). When FREE_BUFFER is used on a null handle, it generates an error.

Warn

You must check that the handle is not null before trying to free the buffer and setting it to 0 as shown above.

To check whether your application is dealing correctly with buffers, you can examine the PcVue log files called Audit.Counters.xxxx.txt located in the PcVue\Bin\Log Files\ folder.

In those files, towards the end of file, a line shows:

[ ALLOC_BUFFER count ] Current = xxx Cumulative = yyy Current allocated memory = zzz

 Part        Description
 xxx  The current number of buffers allocated by the application
 yyy  Total of buffers used since the application was started
 zzz  Current allocated memory in bytes
Warn

The number of current buffers xxx should be kept below 20, depending on the application. If the number is increasing it means that buffers are being allocated but not being released.


 

Created on: 05 Nov 2010 Last update: 26 Aug 2024