On Sat, Mar 13, 2010 at 9:46 PM, Carsten Haitzler <ras...@rasterman.com> wrote: > On Sat, 13 Mar 2010 18:16:17 -0300 Gustavo Sverzut Barbieri > <barbi...@profusion.mobi> said: > >> On Sat, Mar 13, 2010 at 6:01 PM, Enlightenment SVN >> <no-re...@enlightenment.org> wrote: >> > Log: >> > Use eina_stringshare_replace to avoid crashes. >> >> And a special note, particularly to Raster that takes some time to get >> used to new APIs ;-) >> >> the problem with the old approach is that the reference count can drop >> to zero, so you must first inc then dec, to avoid that. See the >> following test case: >> >> const char *a = eina_stringshare_add("unique string"); // refcount = 1 >> >> // using the old and incorrect way: setting "a" where "a" as used: >> eina_stringshare_del(a); // refcount = 0 >> eina_stringshare_add(a); // refcount = 1, but bogus... since the >> content of a was freed already. > > whatever code did this was at fault as a stgringshare del is the equivalent of > a free - and so you're using freed memory (though in practice it only got > freed > on refcount becoming 0). > >> // using the correct approach, the same that replace does: >> tmp = eina_stringshare_add(a); // refcount = 2 >> eina_stringshare_del(a); // refcount = 1 >> a = tmp; // refcount = 1 > > yes - that'd be more correct. > >> This is a simplification, using the same variable names it is clearly >> stupid, but you have different variables with the same pointer in real >> code. > > actually code that does this probably should just do: if (newstr != oldstr) > { stringshare_del; stringshare_add; } :) - no need for a replace :)
well, that optimization could go into replace, yes... but the other call will also save you couple of lines, so good anyway. and it is static inline, so no extra cost doing such call. ;-) -- Gustavo Sverzut Barbieri http://profusion.mobi embedded systems -------------------------------------- MSN: barbi...@gmail.com Skype: gsbarbieri Mobile: +55 (19) 9225-2202 ------------------------------------------------------------------------------ Download Intel® Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev _______________________________________________ enlightenment-devel mailing list enlightenment-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/enlightenment-devel