You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Doc/🪽 Volatile_Usage.md
+38-21Lines changed: 38 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,20 +3,28 @@
3
3
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.
4
4
5
5
## 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
+
9
13
10
14
## Why It Is Required
11
15
12
16
### 1. Continuous Message Processing
17
+
13
18
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.
14
19
15
20
### 2. Synchronous COM Callbacks
21
+
16
22
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.
17
23
18
24
### 3. Deadlock Prevention
25
+
19
26
Because AutoIt and WebView2 run in separate processes, improper message handling can lead to deadlocks.
27
+
20
28
> **Deadlock Scenario:**
21
29
> 1. WebView2 triggers a callback and waits for AutoIt to return.
22
30
> 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
25
33
Declaring callbacks as `Volatile` prevents this by allowing AutoIt to remain responsive.
26
34
27
35
## Rule of Thumb
36
+
28
37
**All functions used as WebView2 event handlers MUST be declared Volatile.**
## Object Cleanup & Memory Management in Callbacks
40
52
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.
42
54
43
-
### Why zero out objects?
55
+
### Why zero out objects in Volatile functions?
44
56
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.
46
58
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.
48
60
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.
50
62
51
63
52
64
### Implementation Example
53
65
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.
; Manually release the object reference to help AutoIt's memory management
79
+
$oFrame = 0
80
+
EndFunc ;==>NetWebView2_Events_OnFrameCreated
81
+
```
59
82
60
-
; 1. Call the UDF's cleanup function to release internal resources
61
-
_NetWebView2_CleanUp($oWebV2M, $oBridge)
62
83
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.
66
85
67
-
Exit
68
-
```
69
86
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.
0 commit comments