# GC of closures

**URL:** <https://discourse.ats-lang.org/t/gc-of-closures/35>\
**Category:** Google Group\
**Created:** [December 28, 2015, 10:13pm UTC](https://discourse.ats-lang.org/t/gc-of-closures/35 "2015-12-28T22:13:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Mike\_Jones](https://discourse.ats-lang.org/u/Mike_Jones)\
**Post date:** [December 28, 2015, 10:13pm UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/1 "2015-12-28T22:13:53Z")

</div>

Ref 3.13 of Introduction, ifold2…

Do closures of in general:

- Gen code with a malloc call?
- Gen code with free after exiting scope?

Meaning, if I am not linking a library with malloc and free I can’t accidentally use it, and if I do have a library, I can’t accidentally produce a leak?

For example, the function example sqrmodsum in the text: does it produce an error when malloc is not available, and if available, does this example leak? Or does it make one heap object that is reused on each call?

I guess I want to know under what circumstances a nieve maintainer can get into trouble. My hope is the behavior is either fail to compile, or no leak. If I am correct, then in which ways can I create a closure that leaks so I can r cognizant them?

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [December 29, 2015, 6:40pm UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/2 "2015-12-29T18:40:05Z")

</div>

The annotation ‘’ means that IF the function is passed as an  
argument  
(to another function), then a closure is formed. If the function is never  
passed as  
an argument, there is no closure formation.

This code is of ATS1-style. I think ‘’ can be erased here without  
causing  
any problems.On Tuesday, December 29, 2015 at 1:21:21 PM UTC-5, Mike Jones wrote:

> There is another closure example in the introduction book I want to  
> understand. Section 6.5 has code below.
> 
> The fun definitions have , and do not follow the typical syntax  
> of a value bound to a lam. So does this mean a closure is returned? Does it  
> mean the fun is a closure? Is any thing on the heap? And what is the  
> purpose of it in this example?
> 
> fun{
> 
> a:t@ype
> 
> } insertion\_sort
> 
> (
> 
> A: arrszref (a)
> 
> , cmp: (a, a) → int
> 
> ) : void = let
> 
> val n = g0uint2int\_size\_int(A.size())
> 
> fun ins (x: a, i: int): void =
> 
> if i \>= 0 then
> 
> (
> 
> if cmp (x, A[i]) \< 0
> 
> then (A[i+1] := A[i]; ins (x, i-1)) else A[i+1] := x
> 
> // end of [if]
> 
> ) else A[0] := x // end of [if]
> 
> // end of [ins]
> 
> fun loop (i: int): void =
> 
> if i \< n then (ins (A[i], i-1); loop (i+1)) else ()
> 
> // end of [loop]
> 
> in
> 
> loop (1)

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [December 29, 2015, 3:22pm UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/3 "2015-12-29T15:22:49Z")

</div>

Try to compile the one calls ‘malloc’ and the one that does not, and you  
will  
see the difference clearly:

[https://github.com/githwxi/ATS-Postiats-test/blob/master/contrib/hwxi/TEST30/test31.datsOn](https://github.com/githwxi/ATS-Postiats-test/blob/master/contrib/hwxi/TEST30/test31.datsOn) Tuesday, December 29, 2015 at 10:10:44 AM UTC-5, gmhwxi wrote:

> To use malloc, you need to compile with a flag looking like  
> -DATS\_MEMALLOC\_???
> 
> For instance,
> 
> patscc -DATS\_MEMALLOC\_LIBC -o foo foo.dats // using malloc/free in libc  
> patscc -DATS\_MEMALLOC\_GCBDW -o foo foo.dats -lgc // using Bohem GC
> 
> If you use malloc but do not use -DATS\_MEMALLOC\_???, then you will see  
> the following error message issued by the linker:
> 
> undefined reference to `atsruntime\_malloc\_undef’
> 
> If you can compile your ATS code to executable without using  
> -DATS\_MEMALLOC\_???,  
> then your ATS code cannot generate memory leaks (because it does not even  
> call malloc).
> 
> On Tue, Dec 29, 2015 at 9:56 AM, Mike jones \<proc…\> wrote:
> 
> > I assume that if there is no alloc/malloc library to link, there cannot  
> > be links, because I kind of remember that for ATS the GC operates via this  
> > API. I would like to confirm that.
> > 
> > The danger for me is my application layers on ThreadX, wrappers by  
> > Cypress, but I think it exposes the alloc/malloc. Would a simple search of  
> > the generated C be enough to determine no leaks? Or are there other calls I  
> > need to check?
> > 
> > Sent from my iPad
> > 
> > On Dec 28, 2015, at 5:30 PM, gmhwxi \<gm…\> wrote:
> > 
> > To recognize whether the C code generated from ATS source  
> > does something bad (e.g., leaks memory), one needs to analyze the  
> > C code. Tools need to be developed for doing such analysis.
> > 
> > On Monday, December 28, 2015 at 7:28:14 PM UTC-5, gmhwxi wrote:
> > 
> > > In general, malloc needs to be called to construct a closure.  
> > > However, ‘free’ is _not_ called automatically. If you use a linear  
> > > closure (cloptr) but do not call ‘free’, you will get a type-error  
> > > somewhere.  
> > > If you use a persistent closure (cloref), you can not free the closure  
> > > explicitly  
> > > (in a type-safe manner). Instead, GC is needed to reclaim the closure.
> > > 
> > > In other words, if you create a cloref but do not run GC, you have a  
> > > potential  
> > > memory leak.
> > > 
> > > On Monday, December 28, 2015 at 5:13:53 PM UTC-5, Mike Jones wrote:
> > > 
> > > > Ref 3.13 of Introduction, ifold2…
> > > > 
> > > > Do closures of in general:
> > > > 
> > > > - Gen code with a malloc call?
> > > > - Gen code with free after exiting scope?
> > > > 
> > > > Meaning, if I am not linking a library with malloc and free I can’t  
> > > > accidentally use it, and if I do have a library, I can’t accidentally  
> > > > produce a leak?
> > > > 
> > > > For example, the function example sqrmodsum in the text: does it  
> > > > produce an error when malloc is not available, and if available, does this  
> > > > example leak? Or does it make one heap object that is reused on each call?
> > > > 
> > > > I guess I want to know under what circumstances a nieve maintainer can  
> > > > get into trouble. My hope is the behavior is either fail to compile, or no  
> > > > leak. If I am correct, then in which ways can I create a closure that leaks  
> > > > so I can r cognizant them?
> > > > 
> > > > –  
> > > > You received this message because you are subscribed to a topic in the  
> > > > Google Groups “ats-lang-users” group.  
> > > > To unsubscribe from this topic, visit  
> > > > [https://groups.google.com/d/topic/ats-lang-users/zht\_Kex7y-0/unsubscribe](https://groups.google.com/d/topic/ats-lang-users/zht_Kex7y-0/unsubscribe).  
> > > > To unsubscribe from this group and all its topics, send an email to  
> > > > [ats-lang-user...@googlegroups.com](mailto:ats-lang-user...@googlegroups.com).  
> > > > To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> > > > Visit this group at [https://groups.google.com/group/ats-lang-users](https://groups.google.com/group/ats-lang-users).  
> > > > To view this discussion on the web visit  
> > > > [https://groups.google.com/d/msgid/ats-lang-users/eba42b74-d4a6-4d89-987d-a244dbccd36a%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/eba42b74-d4a6-4d89-987d-a244dbccd36a%40googlegroups.com)  
> > > > [https://groups.google.com/d/msgid/ats-lang-users/eba42b74-d4a6-4d89-987d-a244dbccd36a%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/eba42b74-d4a6-4d89-987d-a244dbccd36a%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > .
> > 
> > –  
> > You received this message because you are subscribed to the Google Groups  
> > “ats-lang-users” group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [ats-lang-user...@googlegroups.com](mailto:ats-lang-user...@googlegroups.com).  
> > To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> > Visit this group at [https://groups.google.com/group/ats-lang-users](https://groups.google.com/group/ats-lang-users).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/ats-lang-users/EEAA785F-0807-48A8-8126-18A012B9CBDA%40gmail.com](https://groups.google.com/d/msgid/ats-lang-users/EEAA785F-0807-48A8-8126-18A012B9CBDA%40gmail.com)  
> > [https://groups.google.com/d/msgid/ats-lang-users/EEAA785F-0807-48A8-8126-18A012B9CBDA%40gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/EEAA785F-0807-48A8-8126-18A012B9CBDA%40gmail.com?utm_medium=email&utm_source=footer)  
> > .

---

<div class="post-metadata">

**Author:** ![Mike\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Mike\_Jones](https://discourse.ats-lang.org/u/Mike_Jones)\
**Post date:** [December 29, 2015, 5:10pm UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/4 "2015-12-29T17:10:05Z")

</div>

Since Bohem GC replaces alloc, does the ATS compiler really generate different code for GC or does it just add some GC initiallization code?

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [December 29, 2015, 12:28am UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/5 "2015-12-29T00:28:14Z")

</div>

In general, malloc needs to be called to construct a closure.  
However, ‘free’ is _not_ called automatically. If you use a linear  
closure (cloptr) but do not call ‘free’, you will get a type-error  
somewhere.  
If you use a persistent closure (cloref), you can not free the closure  
explicitly  
(in a type-safe manner). Instead, GC is needed to reclaim the closure.

In other words, if you create a cloref but do not run GC, you have a  
potential  
memory leak.On Monday, December 28, 2015 at 5:13:53 PM UTC-5, Mike Jones wrote:

> Ref 3.13 of Introduction, ifold2…
> 
> Do closures of in general:
> 
> - Gen code with a malloc call?
> - Gen code with free after exiting scope?
> 
> Meaning, if I am not linking a library with malloc and free I can’t  
> accidentally use it, and if I do have a library, I can’t accidentally  
> produce a leak?
> 
> For example, the function example sqrmodsum in the text: does it produce  
> an error when malloc is not available, and if available, does this example  
> leak? Or does it make one heap object that is reused on each call?
> 
> I guess I want to know under what circumstances a nieve maintainer can get  
> into trouble. My hope is the behavior is either fail to compile, or no  
> leak. If I am correct, then in which ways can I create a closure that leaks  
> so I can r cognizant them?

---

<div class="post-metadata">

**Author:** ![Mike\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Mike\_Jones](https://discourse.ats-lang.org/u/Mike_Jones)\
**Post date:** [January 1, 2016, 4:50am UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/6 "2016-01-01T04:50:57Z")

</div>

BTW, how does the compiler know what type to cast to?

---

<div class="post-metadata">

**Author:** ![Mike\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Mike\_Jones](https://discourse.ats-lang.org/u/Mike_Jones)\
**Post date:** [December 29, 2015, 5:34pm UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/7 "2015-12-29T17:34:00Z")

</div>

This shows how to put it on heap/GC, and stack, how do you put it in static memory? I assume you create a standard function or instantiated template. Then do you have to cast it?

---

<div class="post-metadata">

**Author:** ![Mike\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Mike\_Jones](https://discourse.ats-lang.org/u/Mike_Jones)\
**Post date:** [December 29, 2015, 8:23pm UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/8 "2015-12-29T20:23:54Z")

</div>

What does the & mean?

---

<div class="post-metadata">

**Author:** ![Yannick\_Duchene](https://avatars.discourse-cdn.com/v4/letter/y/b77776/32.png) [@Yannick\_Duchene](https://discourse.ats-lang.org/u/Yannick_Duchene)\
**Post date:** [December 29, 2015, 12:58am UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/9 "2015-12-29T00:58:21Z")

</div>

> Meaning, if I am not linking a library with malloc and free I can’t  
> accidentally use it, and if I do have a library, I can’t accidentally  
> produce a leak?

> To recognize whether the C code generated from ATS source  
> does something bad (e.g., leaks memory), one needs to analyze the  
> C code. Tools need to be developed for doing such analysis.

I can’t find back the thread, I just remember exceptions may produce memory  
leaks too.

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [December 29, 2015, 12:48am UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/10 "2015-12-29T00:48:29Z")

</div>

Yes, you can allocate the closure on the stack:

> <https://github.com/githwxi/ATS-Postiats-test/blob/master/contrib/hwxi/TEST30/test31.dats>

Note that this is static allocation; it is _not_ done by alloca.On Monday, December 28, 2015 at 5:20:04 PM UTC-5, Mike Jones wrote:

> A related question is why wouldn’t the example sqrmodsum put the closure  
> on the stack? It is not needed outside the scope of this function as it is  
> not in any way returned, and there is no lazy evaluation.

---

<div class="post-metadata">

**Author:** ![Mike\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Mike\_Jones](https://discourse.ats-lang.org/u/Mike_Jones)\
**Post date:** [December 28, 2015, 10:20pm UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/11 "2015-12-28T22:20:03Z")

</div>

A related question is why wouldn’t the example sqrmodsum put the closure on the stack? It is not needed outside the scope of this function as it is not in any way returned, and there is no lazy evaluation.

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [December 29, 2015, 12:30am UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/12 "2015-12-29T00:30:34Z")

</div>

To recognize whether the C code generated from ATS source  
does something bad (e.g., leaks memory), one needs to analyze the  
C code. Tools need to be developed for doing such analysis.On Monday, December 28, 2015 at 7:28:14 PM UTC-5, gmhwxi wrote:

> In general, malloc needs to be called to construct a closure.  
> However, ‘free’ is _not_ called automatically. If you use a linear  
> closure (cloptr) but do not call ‘free’, you will get a type-error  
> somewhere.  
> If you use a persistent closure (cloref), you can not free the closure  
> explicitly  
> (in a type-safe manner). Instead, GC is needed to reclaim the closure.
> 
> In other words, if you create a cloref but do not run GC, you have a  
> potential  
> memory leak.
> 
> On Monday, December 28, 2015 at 5:13:53 PM UTC-5, Mike Jones wrote:
> 
> > Ref 3.13 of Introduction, ifold2…
> > 
> > Do closures of in general:
> > 
> > - Gen code with a malloc call?
> > - Gen code with free after exiting scope?
> > 
> > Meaning, if I am not linking a library with malloc and free I can’t  
> > accidentally use it, and if I do have a library, I can’t accidentally  
> > produce a leak?
> > 
> > For example, the function example sqrmodsum in the text: does it produce  
> > an error when malloc is not available, and if available, does this example  
> > leak? Or does it make one heap object that is reused on each call?
> > 
> > I guess I want to know under what circumstances a nieve maintainer can  
> > get into trouble. My hope is the behavior is either fail to compile, or no  
> > leak. If I am correct, then in which ways can I create a closure that leaks  
> > so I can r cognizant them?

---

<div class="post-metadata">

**Author:** ![Mike\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Mike\_Jones](https://discourse.ats-lang.org/u/Mike_Jones)\
**Post date:** [January 1, 2016, 4:06am UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/13 "2016-01-01T04:06:01Z")

</div>

Can you add the cast version in the code or write it here? I think it is important because one might need to pass stack allocated lams to prexisting higher ordered functions. As much as I don’t like casting, for embedded code, this seems the only way, unless there is something not said here.

---

<div class="post-metadata">

**Author:** ![Yannick\_Duchene](https://avatars.discourse-cdn.com/v4/letter/y/b77776/32.png) [@Yannick\_Duchene](https://discourse.ats-lang.org/u/Yannick_Duchene)\
**Post date:** [December 29, 2015, 10:06pm UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/14 "2015-12-29T22:06:20Z")

</div>

> ‘lam@’ means to form a closure in a given memory space.

Excuse my question: does that mean one can use statically allocated  
closures, I mean, closure without any dynamic memory allocation?

---

<div class="post-metadata">

**Author:** ![Mike\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Mike\_Jones](https://discourse.ats-lang.org/u/Mike_Jones)\
**Post date:** [December 29, 2015, 5:31pm UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/15 "2015-12-29T17:31:03Z")

</div>

I need some help with this code:

= I assume overrides a default of

lam@ I’m not sure what @ means. Does it mean pass tuple, anything, is something non-transparent being made transparent?

Cast does not use {}, why not? Is the compiler figuring it out on its own? If so, should it be {ptr} if explicit?

Cast because by not being ref, it has to be converted to a ptr? Seem messy not to just create a closure as pointer in the first place, so that type checking does its job.

fun{}  
sqrmodsum2  
(n: int, d: int): int = let  
//  
var  
fopr =  
lam@(res: int, x: int): int =  
if x mod d = 0 then res + x \* x else res  
//  
in  
ifold2(10, $UNSAFE.cast(addr@fopr), 0)  
end // end of [sqrmodsum2]

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [December 29, 2015, 6:26pm UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/16 "2015-12-29T18:26:53Z")

</div>

‘lam@’ means to form a closure in a given memory space.

In order words, any closure created with the keyword ‘lam@’ is flat.

I was cutting a bit of corner in my original code. No casting is needed if  
ifold2 is replaced with ifold2\_:

fun  
ifold2\_( n: int, f: &(int, int) - int, ini: int) : int =  
if n \> 0 then f(ifold2\_ (n-1, f, ini), n) else ini// end of [ifold2]On Tuesday, December 29, 2015 at 12:31:03 PM UTC-5, Mike Jones wrote:

> I need some help with this code:
> 
> = I assume overrides a default of
> 
> lam@ I’m not sure what @ means. Does it mean pass tuple, anything, is  
> something non-transparent being made transparent?
> 
> Cast does not use {}, why not? Is the compiler figuring it out on its own?  
> If so, should it be {ptr} if explicit?
> 
> Cast because by not being ref, it has to be converted to a ptr? Seem messy  
> not to just create a closure as pointer in the first place, so that type  
> checking does its job.
> 
> fun{}  
> sqrmodsum2  
> (n: int, d: int): int = let  
> //  
> var  
> fopr =  
> lam@(res: int, x: int): int =  
> if x mod d = 0 then res + x \* x else res  
> //  
> in  
> ifold2(10, $UNSAFE.cast(addr@fopr), 0)  
> end // end of [sqrmodsum2]

---

<div class="post-metadata">

**Author:** ![Mike\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Mike\_Jones](https://discourse.ats-lang.org/u/Mike_Jones)\
**Post date:** [December 29, 2015, 8:45pm UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/17 "2015-12-29T20:45:51Z")

</div>

So (…) - means closure on heap which is ptr, and &(…) - means ptr to closure that is not already ptr, on stack or static. Wouldn’t the representation be the same, such that a function taking a closure as argument could take either, assuming something like ptr to tuple with an anonymous function and captured data?

And can you pass a closure by value? That assumes a tuple copy. Not efficient, but curious.

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [December 29, 2015, 5:15pm UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/18 "2015-12-29T17:15:37Z")

</div>

ATS compiler generates the same code. It is just that certain functions  
(malloc, free, calloc, and realloc) are  
hooked to different implementations (provided by different libraries).On Tue, Dec 29, 2015 at 12:10 PM, Mike Jones [proc...@gmail.com](mailto:proc...@gmail.com) wrote:

> Since Bohem GC replaces alloc, does the ATS compiler really generate  
> different code for GC or does it just add some GC initiallization code?
> 
> –  
> You received this message because you are subscribed to the Google Groups  
> “ats-lang-users” group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [ats-lang-user...@googlegroups.com](mailto:ats-lang-user...@googlegroups.com).  
> To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> Visit this group at [https://groups.google.com/group/ats-lang-users](https://groups.google.com/group/ats-lang-users).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/ats-lang-users/c14d350a-f275-4fa0-9eae-7586e59d850f%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/c14d350a-f275-4fa0-9eae-7586e59d850f%40googlegroups.com)  
> .

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [December 29, 2015, 3:10pm UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/19 "2015-12-29T15:10:44Z")

</div>

To use malloc, you need to compile with a flag looking like  
-DATS\_MEMALLOC\_???

For instance,

patscc -DATS\_MEMALLOC\_LIBC -o foo foo.dats // using malloc/free in libc  
patscc -DATS\_MEMALLOC\_GCBDW -o foo foo.dats -lgc // using Bohem GC

If you use malloc but do not use -DATS\_MEMALLOC\_???, then you will see  
the following error message issued by the linker:

undefined reference to `atsruntime\_malloc\_undef’

If you can compile your ATS code to executable without using  
-DATS\_MEMALLOC\_???,  
then your ATS code cannot generate memory leaks (because it does not even  
call malloc).On Tue, Dec 29, 2015 at 9:56 AM, Mike jones [proc...@gmail.com](mailto:proc...@gmail.com) wrote:

> I assume that if there is no alloc/malloc library to link, there cannot be  
> links, because I kind of remember that for ATS the GC operates via this  
> API. I would like to confirm that.
> 
> The danger for me is my application layers on ThreadX, wrappers by  
> Cypress, but I think it exposes the alloc/malloc. Would a simple search of  
> the generated C be enough to determine no leaks? Or are there other calls I  
> need to check?
> 
> Sent from my iPad
> 
> On Dec 28, 2015, at 5:30 PM, gmhwxi [gmh...@gmail.com](mailto:gmh...@gmail.com) wrote:
> 
> To recognize whether the C code generated from ATS source  
> does something bad (e.g., leaks memory), one needs to analyze the  
> C code. Tools need to be developed for doing such analysis.
> 
> On Monday, December 28, 2015 at 7:28:14 PM UTC-5, gmhwxi wrote:
> 
> > In general, malloc needs to be called to construct a closure.  
> > However, ‘free’ is _not_ called automatically. If you use a linear  
> > closure (cloptr) but do not call ‘free’, you will get a type-error  
> > somewhere.  
> > If you use a persistent closure (cloref), you can not free the closure  
> > explicitly  
> > (in a type-safe manner). Instead, GC is needed to reclaim the closure.
> > 
> > In other words, if you create a cloref but do not run GC, you have a  
> > potential  
> > memory leak.
> > 
> > On Monday, December 28, 2015 at 5:13:53 PM UTC-5, Mike Jones wrote:
> > 
> > > Ref 3.13 of Introduction, ifold2…
> > > 
> > > Do closures of in general:
> > > 
> > > - Gen code with a malloc call?
> > > - Gen code with free after exiting scope?
> > > 
> > > Meaning, if I am not linking a library with malloc and free I can’t  
> > > accidentally use it, and if I do have a library, I can’t accidentally  
> > > produce a leak?
> > > 
> > > For example, the function example sqrmodsum in the text: does it produce  
> > > an error when malloc is not available, and if available, does this example  
> > > leak? Or does it make one heap object that is reused on each call?
> > > 
> > > I guess I want to know under what circumstances a nieve maintainer can  
> > > get into trouble. My hope is the behavior is either fail to compile, or no  
> > > leak. If I am correct, then in which ways can I create a closure that leaks  
> > > so I can r cognizant them?
> > > 
> > > –  
> > > You received this message because you are subscribed to a topic in the  
> > > Google Groups “ats-lang-users” group.  
> > > To unsubscribe from this topic, visit  
> > > [https://groups.google.com/d/topic/ats-lang-users/zht\_Kex7y-0/unsubscribe](https://groups.google.com/d/topic/ats-lang-users/zht_Kex7y-0/unsubscribe).  
> > > To unsubscribe from this group and all its topics, send an email to  
> > > [ats-lang-user...@googlegroups.com](mailto:ats-lang-user...@googlegroups.com).  
> > > To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> > > Visit this group at [https://groups.google.com/group/ats-lang-users](https://groups.google.com/group/ats-lang-users).  
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/ats-lang-users/eba42b74-d4a6-4d89-987d-a244dbccd36a%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/eba42b74-d4a6-4d89-987d-a244dbccd36a%40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/ats-lang-users/eba42b74-d4a6-4d89-987d-a244dbccd36a%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/eba42b74-d4a6-4d89-987d-a244dbccd36a%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .
> 
> –  
> You received this message because you are subscribed to the Google Groups  
> “ats-lang-users” group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [ats-lang-user...@googlegroups.com](mailto:ats-lang-user...@googlegroups.com).  
> To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> Visit this group at [https://groups.google.com/group/ats-lang-users](https://groups.google.com/group/ats-lang-users).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/ats-lang-users/EEAA785F-0807-48A8-8126-18A012B9CBDA%40gmail.com](https://groups.google.com/d/msgid/ats-lang-users/EEAA785F-0807-48A8-8126-18A012B9CBDA%40gmail.com)  
> [https://groups.google.com/d/msgid/ats-lang-users/EEAA785F-0807-48A8-8126-18A012B9CBDA%40gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/EEAA785F-0807-48A8-8126-18A012B9CBDA%40gmail.com?utm_medium=email&utm_source=footer)  
> .

---

<div class="post-metadata">

**Author:** ![Yannick\_Duchene](https://avatars.discourse-cdn.com/v4/letter/y/b77776/32.png) [@Yannick\_Duchene](https://discourse.ats-lang.org/u/Yannick_Duchene)\
**Post date:** [December 29, 2015, 10:09pm UTC](https://discourse.ats-lang.org/t/gc-of-closures/35/20 "2015-12-29T22:09:50Z")

</div>

Please Professor, can you add the “documentation” tag to this thread?Le mardi 29 décembre 2015 19:40:05 UTC+1, gmhwxi a écrit :

> The annotation ‘’ means that IF the function is passed as an  
> argument  
> (to another function), then a closure is formed. If the function is never  
> passed as  
> an argument, there is no closure formation.
> 
> This code is of ATS1-style. I think ‘’ can be erased here without  
> causing  
> any problems.

[Next page](https://discourse.ats-lang.org/t/gc-of-closures/35.md?page=2)
