<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Got it, thanks a lot!</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br>Carlo</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 22 December 2015 at 18:34, Aleksander Morgado <span dir="ltr"><<a href="mailto:aleksander@aleksander.es" target="_blank">aleksander@aleksander.es</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hey!<br>
<span class=""><br>
On Tue, Dec 22, 2015 at 5:27 PM, Carlo Lobrano <<a href="mailto:c.lobrano@gmail.com">c.lobrano@gmail.com</a>> wrote:<br>
> I am using cinterion and other plugins as reference to implement<br>
> load_supported_bands for Telit plugin, but there is a point which is not<br>
> clear for me.<br>
><br>
> In my reference plugins I've always seen this kind of result assignement<br>
><br>
> supported_bands_ready (...) {<br>
> /* get the results */<br>
> g_simple_async_result_set_op_res_gpointer (simple,<br>
> bands,<br>
> (GDestroyNotify)<br>
> g_array_unref);<br>
> g_simple_async_result_complete (operation_result);<br>
> g_object_unref (operation_result);<br>
> }<br>
><br>
> where bands is a GArray* that might contain the list of supported bands if<br>
> everything went fine or NULL otherwise.<br>
><br>
<br>
</span>Yes, in the specific case of the cinterion plugin a newly allocated<br>
GArray* is returned by mm_cinterion_parse_scfg_test(). What we're<br>
doing here is to put that newly allocated GArray in the result, and<br>
say that whenever the result is disposed, the array should be disposed<br>
as well (i.e. the array is now owned by the result object). There is<br>
no memory leak here, because once the result is unref-ed for the last<br>
time, it will also dispose the GArray.<br>
<span class=""><br>
> My question is: there is any kind of memory leak here? Is it correct to use<br>
> g_array_ref(bands) instead?<br>
><br>
> supported_bands_ready (...) {<br>
> /* get the results */<br>
> g_simple_async_result_set_op_res_gpointer (simple,<br>
> g_array_ref(bands),<br>
> (GDestroyNotify)<br>
> g_array_unref);<br>
> g_simple_async_result_complete (operation_result);<br>
> g_object_unref (operation_result);<br>
> }<br>
<br>
</span>It would be ok to pass a newly created reference with g_array_ref(),<br>
but only if you afterwards dispose the one you got from<br>
mm_cinterion_parse_scfg_test(), or you would be leaking a reference.<br>
In other words, these two blocks would be both ok:<br>
<br>
1) ========<br>
if (mm_cinterion_parse_scfg_test (&bands)) {<br>
g_simple_async_result_set_op_res_gpointer (simple, bands,<br>
(GDestroyNotify) g_array_unref ());<br>
g_simple_async_result_complete (simple);<br>
g_object_unref (simple);<br>
}<br>
<br>
2) ========<br>
if (mm_cinterion_parse_scfg_test (&bands)) {<br>
g_simple_async_result_set_op_res_gpointer (simple, g_array_ref<br>
(bands), (GDestroyNotify) g_array_unref ());<br>
g_array_unref (bands);<br>
g_simple_async_result_complete (simple);<br>
g_object_unref (simple);<br>
}<br>
<br>
In this case, it may be cleaner to use approach #1, to avoid the need<br>
for the extra ref/unrefs. In some other cases, e.g. when the reference<br>
of the object returned is owned by some other thing, like a async<br>
method context helper struct, then it would be better to use approach<br>
#2, as that clearly shows who owns each reference.<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Aleksander<br>
<a href="https://aleksander.es" rel="noreferrer" target="_blank">https://aleksander.es</a><br>
</font></span></blockquote></div><br></div>