Memory-leaks.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <html lang="en">
  2. <head>
  3. <title>Memory leaks - avram - a virtual machine code interpreter</title>
  4. <meta http-equiv="Content-Type" content="text/html">
  5. <meta name="description" content="avram - a virtual machine code interpreter">
  6. <meta name="generator" content="makeinfo 4.13">
  7. <link title="Top" rel="start" href="index.html#Top">
  8. <link rel="up" href="Working-around-library-misfeatures.html#Working-around-library-misfeatures" title="Working around library misfeatures">
  9. <link rel="prev" href="Inept-excess-verbiage.html#Inept-excess-verbiage" title="Inept excess verbiage">
  10. <link rel="next" href="Suicidal-exception-handling.html#Suicidal-exception-handling" title="Suicidal exception handling">
  11. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  12. <meta http-equiv="Content-Style-Type" content="text/css">
  13. <style type="text/css"><!--
  14. pre.display { font-family:inherit }
  15. pre.format { font-family:inherit }
  16. pre.smalldisplay { font-family:inherit; font-size:smaller }
  17. pre.smallformat { font-family:inherit; font-size:smaller }
  18. pre.smallexample { font-size:smaller }
  19. pre.smalllisp { font-size:smaller }
  20. span.sc { font-variant:small-caps }
  21. span.roman { font-family:serif; font-weight:normal; }
  22. span.sansserif { font-family:sans-serif; font-weight:normal; }
  23. --></style>
  24. </head>
  25. <body>
  26. <div class="node">
  27. <a name="Memory-leaks"></a>
  28. <p>
  29. Next:&nbsp;<a rel="next" accesskey="n" href="Suicidal-exception-handling.html#Suicidal-exception-handling">Suicidal exception handling</a>,
  30. Previous:&nbsp;<a rel="previous" accesskey="p" href="Inept-excess-verbiage.html#Inept-excess-verbiage">Inept excess verbiage</a>,
  31. Up:&nbsp;<a rel="up" accesskey="u" href="Working-around-library-misfeatures.html#Working-around-library-misfeatures">Working around library misfeatures</a>
  32. <hr>
  33. </div>
  34. <h5 class="subsubsection">3.9.3.2 Memory leaks</h5>
  35. <p>Incorrect memory management may undermine confidence in a library when
  36. one wonders what else it gets wrong, but if the worst it does is leave
  37. a few bytes unreclaimed, then help is at hand.
  38. <p>The first priority is to assess the seriousness of the situation.
  39. Similarly to the way library functions are bracketed with calls to
  40. those listed in <a href="Inept-excess-verbiage.html#Inept-excess-verbiage">Inept excess verbiage</a>, the following functions
  41. are meant to be placed before and after a call to a library function
  42. either for diagnostic purposes or production use.
  43. <div class="defun">
  44. &mdash; Function: void <b>avm_manage_memory</b> ()<var><a name="index-avm_005fmanage_005fmemory-683"></a></var><br>
  45. <blockquote><p>After this function is called, all subsequent calls to the standard C
  46. functions <code>malloc</code>, <code>free</code>, and <code>realloc</code> are
  47. intercepted and logged until the next time
  48. <code>avm_dont_manage_memory</code> is called. Furthermore, a complete
  49. record is maintained of the addresses and sizes of all allocated areas
  50. of memory during this time in a persistent data structure managed
  51. internally.
  52. </p></blockquote></div>
  53. <div class="defun">
  54. &mdash; Function: void <b>avm_dont_manage_memory</b> ()<var><a name="index-avm_005fdont_005fmanage_005fmemory-684"></a></var><br>
  55. <blockquote><p>Calling this function suspends the storage monitoring activities
  56. initiated by calling <code>avm_manage_memory</code>, but the record of
  57. allocated memory areas is not erased.
  58. </p></blockquote></div>
  59. <div class="defun">
  60. &mdash; Function: void <b>avm_debug_memory</b> ()<var><a name="index-avm_005fdebug_005fmemory-685"></a></var><br>
  61. <blockquote><p>After this function is called and <code>avm_manage_memory</code> is also
  62. called, the standard output stream will display a running account of
  63. the sizes and addresses of all memory allocations or deallocations as
  64. they occur until the next call to either <code>avm_dont_debug_memory</code>
  65. or <code>avm_dont_manage_memory</code>.
  66. </p></blockquote></div>
  67. <div class="defun">
  68. &mdash; Function: void <b>avm_dont_debug_memory</b> ()<var><a name="index-avm_005fdont_005fdebug_005fmemory-686"></a></var><br>
  69. <blockquote><p>This function stops the output being sent to <code>stdout</code> caused by
  70. <code>avm_debug_memory</code>, if any, but has no effect on the logging of
  71. memory management events preformed due to <code>avm_manage_memory</code>.
  72. </p></blockquote></div>
  73. <p>While the latter two are not useful in production code, they can help
  74. to clarify an inadequately documented API during development by
  75. experimentally identifying the functions that cause memory to be
  76. allocated. They can also provide the answer to questions like whether
  77. separate copies are made from arrays passed to functions (useful for
  78. knowing when it's appropriate to free them).
  79. <p>Although the console output reveals everything there is to know about
  80. memory management during the selected window, the question of
  81. unreclaimed storage is more directly settled by the following
  82. functions.
  83. <div class="defun">
  84. &mdash; Function: void <b>avm_initialize_mwrap</b> ()<var><a name="index-avm_005finitialize_005fmwrap-687"></a></var><br>
  85. <blockquote><p>This function has to be called before any other functions from
  86. <samp><span class="file">mwrap.h</span></samp> in order to clean the slate and prepare the static data
  87. structures for use. This function might not have to be called
  88. explicitly if the client module is part of <code>avram</code>, whose main
  89. program would have already called it. There is no harm in calling it
  90. repeatedly.
  91. </p></blockquote></div>
  92. <div class="defun">
  93. &mdash; Function: void <b>avm_count_mwrap</b> ()<var><a name="index-avm_005fcount_005fmwrap-688"></a></var><br>
  94. <blockquote><p>This function should be called after the last call to any other
  95. functions in <samp><span class="file">mwrap.h</span></samp>, when it is expected that all storage that
  96. was allocated while <code>avm_manage_memory</code> was in effect should have
  97. been reclaimed.
  98. <p>If there is no unreclaimed storage allocated during an interval when
  99. memory was being managed, this function returns uneventfully. However,
  100. if any storage remains unreclaimed, a message stating the number of
  101. bytes is written to <code>stderr</code>.
  102. <p>If <code>avm_debug_memory</code> is also in effect when this function
  103. detects unreclaimed storage, an itemized list of the unreclaimed
  104. memory addresses and their sizes is written to standard output.
  105. </p></blockquote></div>
  106. <p>Of course, in order for <code>avm_count_mwrap</code> to report meaningful
  107. results, any memory that is allocated during the interval between
  108. calls to <code>avm_manage_memory</code> and <code>avm_dont_manage_memory</code>
  109. must have been given an opportunity to be reclaimed also while
  110. this logging mechanism is in effect. However, there may be arbitrarily
  111. many intervening intervals during which it is suspended.
  112. <p>On the other hand, any storage that is allocated when memory is not
  113. being managed must not be freed at a time when it is (except for
  114. freeing a <code>NULL</code> pointer, which is tolerated but not
  115. encouraged). Doing so raises an internal error, causing termination
  116. <a name="index-internal-error-689"></a>with extreme prejudice. This behavior is a precaution against library
  117. functions freeing storage that they didn't allocate, which would mean
  118. no memory is safe and it's better for <code>avram</code> not to continue.
  119. <p>If these investigations uncover no evidence of a memory leak, then
  120. perhaps the relevant library functions are reliable enough to run
  121. without supervisory memory management. Alternatively, when memory
  122. leaks are indicated, the next function provides a simple remedy.
  123. <div class="defun">
  124. &mdash; Function: void <b>avm_free_managed_memory</b> ()<var><a name="index-avm_005ffree_005fmanaged_005fmemory-690"></a></var><br>
  125. <blockquote><p>This function causes all storage to be reclaimed that was allocated
  126. at any time while logging of memory allocation was in effect (i.e.,
  127. whenever <code>avm_manage_memory</code> had been called more recently than
  128. <code>avm_dont_manage_memory</code>). When the storage is freed, no further
  129. record of it is maintained.
  130. <p>A side effect of this function is to call <code>avm_dont_manage_memory</code>
  131. and therefore leave memory management turned off.
  132. </p></blockquote></div>
  133. <p>This last function when used in conjunction with the others is
  134. therefore the workaround for library functions that don't clean up
  135. after themselves. It may be important to do it for them if repeated
  136. calls to the library function are expected, which would otherwise
  137. cause unreclaimed storage to accumulate until it curtailed other
  138. operations.
  139. <p>One small issue with this function is the assumption that unreclaimed
  140. storage is really a leak and not internal library data that is
  141. designed to persist between calls. If this assumption is not valid,
  142. breakage will occur. However, libraries deliberately making use of
  143. persistent data are likely to have initialization and destructor
  144. functions as part of their API's, so this assumption is often
  145. justified if they don't.
  146. <p>An example of using these functions is given below.
  147. <p>In this example, <code>allocated_library_object</code> is a hypothetical
  148. function exported by an external library that causes storage to be
  149. allocated, and <code>library_reclamation_routine</code> is provided by the
  150. same library ostensibly to reclaim the storage thus
  151. allocated. However, the latter is suspected of memory leaks.
  152. <p>The variable <code>my_data</code> is declared and used by an <code>avram</code>
  153. developer who is presumably competent to reclaim it correctly, rather
  154. than it being part of an external library. Memory management is
  155. therefore enabled during the calls to the library routines but not at
  156. other times.
  157. <p>The call to <code>avm_count_mwrap</code> is redundant immediately after a
  158. call to <code>avm_free_managed_memory</code>, because with all managed
  159. memory having been freed, no memory leak will ever be detected, but it
  160. is included for illustrative purposes.
  161. <pre class="example"> #include &lt;avm/mwrap.h&gt;
  162. ...
  163. {
  164. void *behemoth;
  165. char *my_data;
  166. avm_initialize_mwrap ();
  167. avm_manage_memory ();
  168. behemoth = allocated_library_object (foo, bar);
  169. avm_dont_manage_memory ();
  170. my_data = (char *) malloc (100);
  171. ...
  172. free (my_data);
  173. avm_manage_memory ();
  174. library_reclamation_routine (&amp;behemoth);
  175. avm_free_managed_memory ();
  176. avm_count_mwrap ();
  177. return;
  178. }
  179. </pre>
  180. <p>It might be a cleaner solution in some sense to omit the call to
  181. <code>library_reclamation_routine</code> entirely, because the storage
  182. allocated during the call to <code>allocated_library_object</code> will be
  183. reclaimed perfectly well by <code>avm_free_managed_memory</code> without
  184. it. Doing so may also be the only option if the library reclamation
  185. routine is either extremely unreliable or non-existent. However, the
  186. style above is to be preferred for portability if possible. The memory
  187. management functions rely on the availability of the system header
  188. file <code>malloc.h</code>, and GNU C library features whose portability is
  189. not assured. If the required features are not detected on the host
  190. system at configuration time, conditional directives in the
  191. <code>avram</code> source will make the <code>avm_</code>* memory management
  192. functions perform no operations, and the responsibility for memory
  193. management will devolve to the possibly less robust external library
  194. implementation.
  195. </body></html>