Note that there are some explanatory texts on larger screens.

plurals
  1. POFree memory for elements of GArray
    text
    copied!<p>I create <code>drvm *drv</code> structure in my function. This structure itself contains fields which contains <code>malloc()-ed</code> fields (<code>uint32_t *buffer</code>). The code which do that is similar to that:</p> <pre><code>... size_t elm_size = sizeof(model*); uint32_t length = *(uint32_t*)len; GArray *models = g_array_sized_new(FALSE, FALSE, elm_size, length); model *mod; for (int i = 0; i &lt; length; ++i) { mod = create_model(...); g_array_append_val(models, mod); } </code></pre> <p>This piece of code doesn't contain errors and is highly tested. At the start of program I register function <code>free_all()</code> (by <code>atexit()</code>) which should clean all resources (especially memory) when <code>exit()</code> is performed.</p> <p>Inside this function I'm trying freeing memory of <code>elements of GArray*</code> (<code>model *</code> structure) <strong>and</strong> memory for <code>GArray *</code> itself:</p> <pre><code>GArray *models; g_array_set_clear_func(models, clean_model); if(!g_array_free(models, FALSE)) { //OK } </code></pre> <p>The problem is that when <code>clean_model(void *data)</code> is called inside <code>glib</code> library I suggest it contains pointer to one <code>model *</code> element. But the address is wrong, it doesn't seem point to any correct value. Neither <code>GArray*</code>, nor <code>model*</code>.</p> <p>Furthermore <code>GArray *models</code> in <code>free_all()</code> function is correct (the same as when I created it) and when I iterate through all <code>GArray *</code> elements in <code>free_all()</code> by </p> <pre><code>for (int i = 0; i &lt; len; ++i) { mod = g_array_index(models, model*, i); // Here I get correct pointer to model* clean_model(mod); } </code></pre> <p>I get expected values.</p> <p><strong>Question:</strong> What's wrong? How should I free memory of elements of <code>GArray *</code> if these elements contain <code>malloc()-ed</code> memory?</p> <p><strong>Part of header:</strong></p> <pre><code>struct _info { uint32_t *buffer; uint32_t len; }; typedef struct _info info; struct _prod { uint32_t *buffer; uint32_t len; }; typedef struct _prod prod; struct _model { uint32_t name; prod product; info inform; }; typedef struct _model model; struct _drvm { GArray *models; GArray *strings; }; typedef struct _drvm drvm; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload