avoiding_20resource_20leaks
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
avoiding_20resource_20leaks [2018/03/31 13:19] – external edit 127.0.0.1 | avoiding_20resource_20leaks [2024/01/05 00:22] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
=====Avoiding resource leaks===== | =====Avoiding resource leaks===== | ||
- | //by Richard Russell, November 2007//\\ \\ A **resource leak** is the term used when a program uses some finite system resource but doesn' | + | //by Richard Russell, November 2007//\\ \\ A **resource leak** is the term used when a program uses some finite system resource but doesn' |
+ | |||
==== Causes of resource leaks ==== | ==== Causes of resource leaks ==== | ||
- | \\ | + | |
+ | There are two principal kinds of resource leak which can affect //BBC BASIC for Windows// programs. The first concerns the allocation of memory from BASIC' | ||
+ | |||
+ | <code bb4w> | ||
DIM ltime% 15, stime% 9 | DIM ltime% 15, stime% 9 | ||
REPEAT | REPEAT | ||
Line 11: | Line 15: | ||
WAIT 10 | WAIT 10 | ||
UNTIL FALSE | UNTIL FALSE | ||
- | This program displays the current time, ten times a second, until interrupted by the user. It is a perfectly good program and should run indefinitely without problems.\\ \\ Compare it with this slightly different example:\\ | + | </ |
+ | |||
+ | This program displays the current time, ten times a second, until interrupted by the user. It is a perfectly good program and should run indefinitely without problems.\\ \\ Compare it with this slightly different example: | ||
+ | |||
+ | <code bb4w> | ||
REPEAT | REPEAT | ||
DIM ltime% 15, stime% 9 | DIM ltime% 15, stime% 9 | ||
Line 19: | Line 27: | ||
WAIT 10 | WAIT 10 | ||
UNTIL FALSE | UNTIL FALSE | ||
- | The only difference is that the **DIM** statement is now inside the loop. This means that every time the loop is executed (approximately ten times per second) another 26 bytes of heap are allocated, and that memory is //never freed//. This program will appear to run correctly, and indeed it will run for quite some time without problems - approximately one hour with the default setting of HIMEM. But once that time has elapsed the program will fail catastrophically with a **No room** error when all the heap space is exhausted.\\ \\ The second kind of resource leak which can affect //BBC BASIC for Windows// programs is concerned with calling **Windows API** functions. Many Windows API functions must be used in pairs: typically the first of the pair allocates some resource or creates an object, whilst the second of the pair frees the resource or destroys the object. If you call the first API function but fail to call the second a resource leak will occur. Here are some examples of API functions which //must// be used in pairs:\\ \\ | + | </ |
+ | |||
+ | The only difference is that the **DIM** statement is now inside the loop. This means that every time the loop is executed (approximately ten times per second) another 26 bytes of heap are allocated, and that memory is //never freed//. This program will appear to run correctly, and indeed it will run for quite some time without problems - approximately one hour with the default setting of HIMEM. But once that time has elapsed the program will fail catastrophically with a **No room** error when all the heap space is exhausted.\\ \\ The second kind of resource leak which can affect //BBC BASIC for Windows// programs is concerned with calling **Windows API** functions. Many Windows API functions must be used in pairs: typically the first of the pair allocates some resource or creates an object, whilst the second of the pair frees the resource or destroys the object. If you call the first API function but fail to call the second a resource leak will occur. Here are some examples of API functions which //must// be used in pairs: | ||
| GlobalAlloc\\ | GlobalFree\\ | | | GlobalAlloc\\ | GlobalFree\\ | | ||
Line 27: | Line 37: | ||
| GdipLoadImageFromFile\\ | GdipDisposeImage\\ | | | GdipLoadImageFromFile\\ | GdipDisposeImage\\ | | ||
| InitializeCriticalSection\\ | DeleteCriticalSection\\ | | | InitializeCriticalSection\\ | DeleteCriticalSection\\ | | ||
- | There are many more!\\ \\ Sometimes the sequence in which you make the calls is important. For example suppose you create a bitmap and select it into a Device Context thus:\\ \\ | + | |
+ | There are many more!\\ \\ Sometimes the sequence in which you make the calls is important. For example suppose you create a bitmap and select it into a Device Context thus: | ||
+ | |||
+ | <code bb4w> | ||
SYS " | SYS " | ||
SYS " | SYS " | ||
SYS " | SYS " | ||
- | When you've finished with the objects you must free them in the correct order:\\ \\ | + | </ |
+ | |||
+ | When you've finished with the objects you must free them in the correct order: | ||
+ | |||
+ | <code bb4w> | ||
SYS " | SYS " | ||
SYS " | SYS " | ||
SYS " | SYS " | ||
- | If instead you were to do the following, you would cause a resource leak:\\ \\ | + | </ |
+ | |||
+ | If instead you were to do the following, you would cause a resource leak: | ||
+ | |||
+ | <code bb4w> | ||
SYS " | SYS " | ||
SYS " | SYS " | ||
SYS " | SYS " | ||
- | \\ | + | </ |
==== Detecting resource leaks ==== | ==== Detecting resource leaks ==== | ||
- | \\ | + | |
+ | The first kind of resource leak described above (caused by misuse of the **DIM** statement) can be detected using the **Memory usage monitor** described in [[/ |
avoiding_20resource_20leaks.1522502346.txt.gz · Last modified: 2024/01/05 00:18 (external edit)