www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Freeing memory allocated at C function

reply Pedro Lacerda <kanvuanza gmail.com> writes:
--047d7b15af4d09340f04bbe31d7d
Content-Type: text/plain; charset=ISO-8859-1

I'm using some C functions like these:

    char *str = allocateNewString();

And this:

    Object *obj = constructObject();
    // etc
    freeObject(obj);


Do I need to free the memory in both cases? Can I someway register them on
GC?

--047d7b15af4d09340f04bbe31d7d
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

I&#39;m using some C functions like these:<div><br></div><div>=A0 =A0 char =
*str =3D allocateNewString();</div><div><br></div><div>And this:</div><div>=
<div><br></div><div>=A0 =A0 Object *obj =3D constructObject();</div><div>=
=A0 =A0=A0// etc</div>
<div>=A0 =A0=A0freeObject(obj);</div></div><div><br></div><div><br></div><d=
iv>Do I need to free the memory in both cases? Can I someway register them =
on GC?</div>

--047d7b15af4d09340f04bbe31d7d--
Mar 22 2012
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 03/22/2012 11:27 PM, Pedro Lacerda wrote:
 I'm using some C functions like these:

      char *str = allocateNewString();

 And this:

      Object *obj = constructObject();
      // etc
      freeObject(obj);


 Do I need to free the memory in both cases? Can I someway register 

 GC?

You can register on GC if you wrap the resources in a class. Then the class object's destructor would call the clean up code. The problem is, it is undeterministic when the destructor will be called, or will it be called at all! Or you can wrap in a struct which has deterministic destruction like in C++, when leaving scopes. A better thing to do in this case is to use the scope() statement. Depending of when you want the cleanup to happen: - scope(success): if the scope is being exited successfully - scope(failure): if the scope is being exited with an exception - scope(exit): when exiting the scope regardless For example, if you want the cleanup only if there is an exception: int allocate() { return 42; } void deallocate(int) {} void foo() { int resource = allocate(); scope(failure) deallocate(resource); // ... an exception may be thrown here ... } void main() { foo(); } Ali
Mar 22 2012
prev sibling parent Pedro Lacerda <kanvuanza gmail.com> writes:
--20cf303bf94c021a7504bbf271d8
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On Fri, Mar 23, 2012 at 3:43 AM, Ali =C7ehreli <acehreli yahoo.com> wrote:

 You can register on GC if you wrap the resources in a class. Then the
 class object's destructor would call the clean up code. The problem is, i=

 is undeterministic when the destructor will be called, or will it be call=

 at all!

 Or you can wrap in a struct which has deterministic destruction like in
 C++, when leaving scopes.

The object is long lived, so the class wrapper is the way to go. Thanks --20cf303bf94c021a7504bbf271d8 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Fri, Mar 23, 2012 at 3:43 AM, Ali =C7ehreli <= span dir=3D"ltr">&lt;<a href=3D"mailto:acehreli yahoo.com">acehreli yahoo.c= om</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"marg= in:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <div class=3D"HOEnZb"><div class=3D"h5">You can register on GC if you wrap = the resources in a class. Then the class object&#39;s destructor would call= the clean up code. The problem is, it is undeterministic when the destruct= or will be called, or will it be called at all!</div> </div> <br> Or you can wrap in a struct which has deterministic destruction like in C++= , when leaving scopes.<br></blockquote><div><br></div><div><br></div><div>T= he object is long lived, so the class wrapper is the way to go.</div><div> Thanks</div><div><br></div></div> --20cf303bf94c021a7504bbf271d8--
Mar 23 2012