| 1 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 4 | <head> |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 6 | <title>Dbstl memory management</title> |
| 7 | <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> |
| 8 | <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> |
| 9 | <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" /> |
| 10 | <link rel="up" href="stl.html" title="Chapter 7. Standard Template Library API" /> |
| 11 | <link rel="prev" href="stl_efficienct_use.html" title="Using dbstl efficiently" /> |
| 12 | <link rel="next" href="stl_misc.html" title="Dbstl miscellaneous notes" /> |
| 13 | </head> |
| 14 | <body> |
| 15 | <div xmlns="" class="navheader"> |
| 16 | <div class="libver"> |
| 17 | <p>Library Version 11.2.5.3</p> |
| 18 | </div> |
| 19 | <table width="100%" summary="Navigation header"> |
| 20 | <tr> |
| 21 | <th colspan="3" align="center">Dbstl memory management</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="stl_efficienct_use.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 7. Standard Template Library API</th> |
| 26 | <td width="20%" align="right"> <a accesskey="n" href="stl_misc.html">Next</a></td> |
| 27 | </tr> |
| 28 | </table> |
| 29 | <hr /> |
| 30 | </div> |
| 31 | <div class="sect1" lang="en" xml:lang="en"> |
| 32 | <div class="titlepage"> |
| 33 | <div> |
| 34 | <div> |
| 35 | <h2 class="title" style="clear: both"><a id="stl_memory_mgmt"></a>Dbstl memory management</h2> |
| 36 | </div> |
| 37 | </div> |
| 38 | </div> |
| 39 | <div class="toc"> |
| 40 | <dl> |
| 41 | <dt> |
| 42 | <span class="sect2"> |
| 43 | <a href="stl_memory_mgmt.html#idp51564672">Freeing memory</a> |
| 44 | </span> |
| 45 | </dt> |
| 46 | <dt> |
| 47 | <span class="sect2"> |
| 48 | <a href="stl_memory_mgmt.html#idp51569240">Type specific notes</a> |
| 49 | </span> |
| 50 | </dt> |
| 51 | </dl> |
| 52 | </div> |
| 53 | <div class="sect2" lang="en" xml:lang="en"> |
| 54 | <div class="titlepage"> |
| 55 | <div> |
| 56 | <div> |
| 57 | <h3 class="title"><a id="idp51564672"></a>Freeing memory</h3> |
| 58 | </div> |
| 59 | </div> |
| 60 | </div> |
| 61 | <p> |
| 62 | When using dbstl, make sure memory allocated in the heap is |
| 63 | released after use. The rules for this are: |
| 64 | </p> |
| 65 | <div class="itemizedlist"> |
| 66 | <ul type="disc"> |
| 67 | <li> |
| 68 | <p> |
| 69 | dbstl will free/delete any memory allocated by dbstl itself. |
| 70 | </p> |
| 71 | </li> |
| 72 | <li> |
| 73 | <p> |
| 74 | You are responsible for freeing/deleting any memory allocated by your code outside of dbstl. |
| 75 | </p> |
| 76 | </li> |
| 77 | </ul> |
| 78 | </div> |
| 79 | </div> |
| 80 | <div class="sect2" lang="en" xml:lang="en"> |
| 81 | <div class="titlepage"> |
| 82 | <div> |
| 83 | <div> |
| 84 | <h3 class="title"><a id="idp51569240"></a>Type specific notes</h3> |
| 85 | </div> |
| 86 | </div> |
| 87 | </div> |
| 88 | <div class="sect3" lang="en" xml:lang="en"> |
| 89 | <div class="titlepage"> |
| 90 | <div> |
| 91 | <div> |
| 92 | <h4 class="title"><a id="idp51604648"></a>DbEnv/Db</h4> |
| 93 | </div> |
| 94 | </div> |
| 95 | </div> |
| 96 | <p> |
| 97 | When you open a <code class="classname">DbEnv</code> or <code class="classname">Db</code> object using |
| 98 | <code class="methodname">dbstl::open_env()</code> or <code class="methodname">dbstl::open_db()</code>, you |
| 99 | do not need to delete that object. However, if you new'd that object and then opened it |
| 100 | without using the <code class="methodname">dbstl::open_env()</code> or |
| 101 | <code class="methodname">dbstl::open_db()</code> methods, you are responsible for deleting the |
| 102 | object. |
| 103 | </p> |
| 104 | <p> |
| 105 | Note that you must <code class="literal">new</code> the <code class="classname">Db</code> or |
| 106 | <code class="classname">DbEnv</code> object, which allocates it on the heap. You can not allocate it |
| 107 | on the stack. If you do, the order of destruction is uncontrollable, which makes dbstl |
| 108 | unable to work properly. |
| 109 | </p> |
| 110 | <p> |
| 111 | You can call <code class="function">dbstl_exit()</code> before the process exits, to release any |
| 112 | memory allocated by dbstl that has to live during the entire process lifetime. Releasing the |
| 113 | memory explicitly will not make much difference, because the process is about to exit and so |
| 114 | all memory allocated on the heap is going to be returned to the operating system anyway. The |
| 115 | only real difference is that your memory leak checker will not report false memory leaks. |
| 116 | </p> |
| 117 | <p> |
| 118 | <code class="function">dbstl_exit()</code> releases any memory allocated by dbstl on the heap. It |
| 119 | also performs other required shutdown operations, such as closing any databases and |
| 120 | environments registered to dbstl and shared across the process. |
| 121 | </p> |
| 122 | <p> |
| 123 | If you are calling the <code class="function">dbstl_exit()</code> function, and your |
| 124 | <code class="classname">DbEnv</code> or <code class="classname">Db</code> objects are new'd by your code, |
| 125 | the <code class="function">dbstl_exit()</code> function should be called before deleting the |
| 126 | <code class="classname">DbEnv</code> or <code class="classname">Db</code> objects, because they need to be |
| 127 | closed before being deleted. Alternatively, you can call the |
| 128 | <code class="methodname">dbstl::close_env()</code> or <code class="methodname">dbstl::close_db()</code> |
| 129 | functions before deleting the <code class="classname">DbEnv</code> or <code class="classname">Db</code> |
| 130 | objects in order to explicitly close the databases or environments. If you do this, |
| 131 | can then delete these objects, and then call <code class="function">dbstl_exit()</code>. |
| 132 | </p> |
| 133 | <p> |
| 134 | In addition, before exiting a thread that uses dbstl API, you can call the <code class="function">dbstl_thread_exit() </code>function to release any Berkeley DB handles if they are not used by other threads. |
| 135 | If you do not call the <code class="function">dbstl_thread_exit() </code>function or call this function only in some threads, all open Berkeley DB handles will be closed by the <code class="function">dbstl_exit()</code>function. |
| 136 | You must call the <code class="function">dbstl_exit() </code>function before the process exits, to avoid memory leak and database update loss, if you do not have transactions and persistent log files. |
| 137 | </p> |
| 138 | </div> |
| 139 | <div class="sect3" lang="en" xml:lang="en"> |
| 140 | <div class="titlepage"> |
| 141 | <div> |
| 142 | <div> |
| 143 | <h4 class="title"><a id="idp51590296"></a>DbstlDbt</h4> |
| 144 | </div> |
| 145 | </div> |
| 146 | </div> |
| 147 | <p> |
| 148 | Only when you are storing raw bytes (such as a bitmap) do you have to |
| 149 | store and retrieve data by using the <code class="classname">DbstlDbt</code> helper class. Although you |
| 150 | also can do so simply by using the Berkeley DB <code class="classname">Dbt</code> class, the |
| 151 | <code class="classname">DbstlDbt</code> class offers more convenient memory management behavior. |
| 152 | </p> |
| 153 | <p> |
| 154 | When you are storing <code class="classname">DbstlDbt</code> objects (such as |
| 155 | <code class="classname">db_vector<DbstlDbt></code>), you <span class="emphasis"><em>must</em></span> allocate |
| 156 | heap memory explicitly using the <code class="function">malloc()</code> function for the |
| 157 | <code class="classname">DbstlDbt</code> object to reference, but you do not need to free the memory |
| 158 | – it is automatically freed by the <code class="classname">DbstlDbt</code> object that owns it |
| 159 | by calling the standard C library <code class="function">free()</code> function. |
| 160 | </p> |
| 161 | <p> |
| 162 | However, because dbstl supports storing any type of object or primitive data, it is rare |
| 163 | that you would have to store data using <code class="classname">DbstlDbt</code> objects while using |
| 164 | dbstl. Examples of storing <code class="classname">DbstlDbt</code> objects can be found in the |
| 165 | <code class="methodname">StlAdvancedFeaturesExample::arbitrary_object_storage()</code> and |
| 166 | <code class="methodname">StlAdvancedFeaturesExample::char_star_string_storage()</code> methods. |
| 167 | </p> |
| 168 | </div> |
| 169 | </div> |
| 170 | </div> |
| 171 | <div class="navfooter"> |
| 172 | <hr /> |
| 173 | <table width="100%" summary="Navigation footer"> |
| 174 | <tr> |
| 175 | <td width="40%" align="left"><a accesskey="p" href="stl_efficienct_use.html">Prev</a> </td> |
| 176 | <td width="20%" align="center"> |
| 177 | <a accesskey="u" href="stl.html">Up</a> |
| 178 | </td> |
| 179 | <td width="40%" align="right"> <a accesskey="n" href="stl_misc.html">Next</a></td> |
| 180 | </tr> |
| 181 | <tr> |
| 182 | <td width="40%" align="left" valign="top">Using dbstl efficiently </td> |
| 183 | <td width="20%" align="center"> |
| 184 | <a accesskey="h" href="index.html">Home</a> |
| 185 | </td> |
| 186 | <td width="40%" align="right" valign="top"> Dbstl miscellaneous notes</td> |
| 187 | </tr> |
| 188 | </table> |
| 189 | </div> |
| 190 | </body> |
| 191 | </html> |