Release 10.1A: OpenEdge Application Server:
Developing WebSpeed Applications

> ## Block context and resource scope

> The context of some blocks also helps determine the scope of certain resources. Conversely, the scope of other resources might have little to do with the context in which you initially define them. Scope is really the duration that a resource is available to an application. Scope can vary depending on the resource and the conditions of application execution.

> In general, the scope of resources created at compile time (when WebSpeed compiles your application) is determined at compile time; the scope of resources created at run time (when WebSpeed executes your application) is determined at run time. (See the "Compile-time versus run-time code" section.) The scope of a resource begins when the resource is instantiated (created in your application) and ends when the resource is destroyed (removed from your application).

> For example, a `FOR` statement defines the scope of any database buffer that it implicitly defines for record reading. The scope of such a record buffer is identical to the context of the `FOR` block, because the buffer is deleted when the `FOR` block finishes with it. For example, in `sample4`, the scope of the Customer buffer ends when the `FOR` block completes. Although the `{&DISPLAY}` statement following the `FOR` block can access balance-sum, it can no longer access the Balance field for any record read into the Customer buffer by the `FOR` block.

> ##### Unscoped resources

> Note that `sample4` does not compile if executed from an HTML-mapping `process-web-request` procedure, because frames defined in an external procedure (Web object) do not scope to any of its internal procedures. As a result, the frame is invisible to the statements in the `FOR` block and the field object references, which are assumed to be in the Web object frame `(FRAME {&FRAME-NAME})`, have no recognized existence:

> sample4
>     FOR EACH sports.Customer
>       FIELDS (sports.Customer.Name sports.Customer.Balance):
>         DEFINE VARIABLE balance-sum AS DECIMAL INITIAL 0.
>         balance-sum = balance-sum + Balance.
>         {&DISPLAY} sports.Customer.Name sports.Customer.Balance.
>     END.

>     FIND FIRST sports.Customer.
>     {&DISPLAY} sports.Customer.Name sports.Customer.Balance
>       WITH FRAME {&FRAME-NAME}.

> The solution is to copy the `WITH` `FRAME` {`&FRAME-NAME}` reference to just before the colon terminator of the `FOR` `EACH` statement.

> ##### Dynamic resources

> In general, dynamic resources are resources that you implicitly or explicitly create and delete at run time. Record buffers that a `FOR` statement implicitly defines are dynamic buffers. WebSpeed creates them when the `FOR` block executes and deletes them when its execution completes. SpeedScript frames scoped to a `FOR` block are dynamic in the same way. The whole context of a procedure is dynamic. WebSpeed creates its local data resources when you call the procedure and deletes them when the procedure returns or otherwise goes out of scope.

> However, WebSpeed allows you to create and delete certain dynamic resources explicitly. For WebSpeed, this especially applies to the external procedure contexts (persistent procedures) of Web objects. In general, the scope of a dynamic resource lasts from the time you create it to the time you delete it or when the WebSpeed agent session ends, whichever occurs first. When a procedure block completes execution and its context goes out of scope, this does not affect the scope of any dynamic resources that you have created in that context. The completion of the procedure can only affect whether the resources are still accessible from the context that remains.

> WebSpeed allows you to define handles to most dynamic resources. These handles are variables that point to the resources you have created. You must ensure that handles to these resources are always available to your application until you delete the resources. If you create a persistent procedure context and do not explicitly delete it, the context remains in scope for the duration of the agent session, no matter what client accesses it. If you also lose the original handles to these procedure contexts, you might not be able to access them again. If you cannot access them, you cannot delete them, and in effect, they become memory lost to WebSpeed and the system (that is, a memory leak).

Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095