Skip to content

Commit 1be0636

Browse files
authored
Update Volatile_Usage.md with detailed explanations
Clarify the importance of the Volatile qualifier in AutoIt callbacks and provide guidelines for object cleanup in Volatile functions.
1 parent 3cecb09 commit 1be0636

1 file changed

Lines changed: 38 additions & 21 deletions

File tree

Doc/🪽 Volatile_Usage.md

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,28 @@
33
In **NetWebView2Lib**, callback functions such as `JS_Events_OnMessageReceived` and `OnBasicAuthenticationRequested` **must** be declared using the `Volatile` qualifier. This is a technical requirement for reliable interaction between AutoIt and the WebView2 runtime.
44

55
## What `Volatile` Does
6-
The `Volatile` keyword enables **re-entrant execution**.
7-
* **With Volatile:** While a function is running, AutoIt continues to process its internal message loop, including GUI and COM events.
8-
* **Without Volatile:** AutoIt pauses message processing until the function returns.
6+
7+
The `Volatile` keyword enables **re-entrant execution**.
8+
9+
- **With Volatile:** While a function is running, AutoIt continues to process its internal message loop, including GUI and COM events.
10+
11+
- **Without Volatile:** AutoIt pauses message processing until the function returns.
12+
913

1014
## Why It Is Required
1115

1216
### 1. Continuous Message Processing
17+
1318
WebView2 communicates with AutoIt through COM events. If AutoIt stops processing messages while handling a callback, incoming events may be delayed or blocked. Declaring the function as `Volatile` ensures that message handling continues during execution, preventing the interface from becoming unresponsive.
1419

1520
### 2. Synchronous COM Callbacks
21+
1622
WebView2 event handlers are invoked through **synchronous COM calls**. This means the WebView2 process waits for AutoIt to finish executing the callback before continuing. If AutoIt is busy (e.g., during GUI interaction like moving the window), the WebView2 process may stall. Using `Volatile` allows the callback to execute without interrupting message handling.
1723

1824
### 3. Deadlock Prevention
25+
1926
Because AutoIt and WebView2 run in separate processes, improper message handling can lead to deadlocks.
27+
2028
> **Deadlock Scenario:**
2129
> 1. WebView2 triggers a callback and waits for AutoIt to return.
2230
> 2. AutoIt is blocked (e.g. by a system menu or moving the GUI) and is not processing messages.
@@ -25,46 +33,55 @@ Because AutoIt and WebView2 run in separate processes, improper message handling
2533
Declaring callbacks as `Volatile` prevents this by allowing AutoIt to remain responsive.
2634

2735
## Rule of Thumb
36+
2837
**All functions used as WebView2 event handlers MUST be declared Volatile.**
2938

30-
## Example
31-
```autoit
39+
**Example**
40+
41+
```AutoIt
3242
; WebView2 callback must be declared as Volatile
3343
Volatile Func JS_Events_OnMessageReceived($oWebV2M, $hGUI, $sMessage)
3444
; Handle message from WebView2
3545
ConsoleWrite("Message received: " & $sMessage & @CRLF)
3646
EndFunc
3747
```
3848

39-
## Object Cleanup
49+
---
50+
51+
## Object Cleanup & Memory Management in Callbacks
4052

41-
In AutoIt, when working with COM objects (like the WebView2 Manager or Bridge), it is highly recommended to explicitly release the object references when they are no longer needed.
53+
When working with **Volatile** functions in `NetWebView2Lib`, AutoIt often receives COM objects as arguments (like `$oArgs` or `$oFrame`) directly from the WebView2 engine. To ensure high performance and stability, it is highly recommended to explicitly release these references before the function returns.
4254

43-
### Why zero out objects?
55+
### Why zero out objects in Volatile functions?
4456

45-
1. **Reference Counting:** COM objects use reference counting. The object stays in memory as long as there is at least one active reference to it. Setting `$oObject = 0` decrements this count.
57+
1. **Immediate Reference Release:** COM objects use **Reference Counting**. As long as AutoIt holds a reference to an object in a variable, that object stays alive in memory. By setting `$oObject = 0`, you decrement this count immediately.
4658

47-
2. **Preventing Memory Leaks:** If a script runs for a long time and repeatedly creates objects without releasing them, it will consume increasing amounts of RAM.
59+
2. **Preventing Memory Leaks:** WebView2 events can fire hundreds of times (e.g., during navigation or frame changes). If these references are not cleared, your script's RAM usage will grow unnecessarily over time.
4860

49-
3. **Proper Shutdown:** WebView2 is an external process. Explicitly cleaning up ensures that the underlying `WebView2Loader` and Chromium processes terminate correctly when your GUI closes.
61+
3. **Process Sync:** Since WebView2 is an external Chromium process, clearing the reference in AutoIt tells the engine that you are done with that specific resource, allowing it to clean up its own internal memory.
5062

5163

5264
### Implementation Example
5365

54-
Always pair your initialization with a cleanup block, typically before the script exits:
66+
In every **Volatile** event handler, make it a habit to nullify any object received as a parameter at the end of the code block.
5567

5668

5769
```AutoIt
58-
; ... inside your Exit logic ...
70+
; Example: Handling a new frame creation
71+
Volatile Func NetWebView2_Events_OnFrameCreated($oWebV2M, $hGUI, $oFrame)
72+
Local Const $s_Prefix = "[EVENT: OnFrameCreated]: WebV2M: " & VarGetType($oWebV2M) & " GUI: " & $hGUI & " Frame: " & VarGetType($oFrame)
73+
74+
; Perform your logic here
75+
__NetWebView2_Log(@ScriptLineNumber, $s_Prefix, 1)
76+
77+
; --- Performance Tip ---
78+
; Manually release the object reference to help AutoIt's memory management
79+
$oFrame = 0
80+
EndFunc ;==>NetWebView2_Events_OnFrameCreated
81+
```
5982

60-
; 1. Call the UDF's cleanup function to release internal resources
61-
_NetWebView2_CleanUp($oWebV2M, $oBridge)
6283

63-
; 2. Explicitly nullify the object variables
64-
$oBridge = 0
65-
$oWebV2M = 0
84+
> **💡 Pro Tip:** This practice is especially important for the `$oArgs` object in events like `OnWebMessageReceived` or `OnNavigationCompleted`. Even though AutoIt will eventually clear local variables when the function exits, setting them to `0` manually is an "active" safeguard against memory fragmentation in high-frequency events.
6685
67-
Exit
68-
```
6986

70-
> **Tip:** In `Volatile` functions, if you receive an object as an argument (like `$oArgs`), it is a good habit to set `$oArgs = 0` at the end of the function to ensure the COM reference is released immediately after the event is handled.
87+
---

0 commit comments

Comments
 (0)