ammarbinfaisal / sahl

a programming language with channels and coroutines/threads
MIT License
10 stars 1 forks source link

memory leaks #7

Closed ammarbinfaisal closed 1 year ago

ammarbinfaisal commented 1 year ago

owner attribute in Obj struct doesn't fix the problem. There has to be way to ensure programs below dont result in memory leak

fun idx(arr: [int], i: int) -> int {
    return arr[i]
}

fun main() {
    print(idx([1, 2, 3, ], 1)) # the array allocated on the heap would be lost
}
fun log(s: string) {
    print(s)
}

fun main() {
    log("sahl seems nice") # the string allocated on the heap would be lost
}

./target/release/sahl samples/sample.sahl -c

valgrind --leak-check=full -s ./sahl exe.bin

==12148== HEAP SUMMARY:
==12148==     in use at exit: 3,247 bytes in 98 blocks
==12148==   total heap usage: 168 allocs, 70 frees, 39,280 bytes allocated
==12148== 
==12148== 1,104 (80 direct, 1,024 indirect) bytes in 2 blocks are definitely lost in loss record 3 of 4
==12148==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==12148==    by 0x10AF34: run (sahl.c:680)
==12148==    by 0x10B5A9: main (sahl.c:783)
==12148== 
==12148== 2,143 (1,880 direct, 263 indirect) bytes in 47 blocks are definitely lost in loss record 4 of 4
==12148==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==12148==    by 0x10A5A6: run (sahl.c:559)
==12148==    by 0x10B5A9: main (sahl.c:783)
==12148== 
==12148== LEAK SUMMARY:
==12148==    definitely lost: 1,960 bytes in 49 blocks
==12148==    indirectly lost: 1,287 bytes in 49 blocks
==12148==      possibly lost: 0 bytes in 0 blocks
==12148==    still reachable: 0 bytes in 0 blocks
==12148==         suppressed: 0 bytes in 0 blocks
==12148== 
==12148== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
ammarbinfaisal commented 1 year ago

garbage collection https://github.com/abooishaaq/sahl/commit/ad7358251a5d24f79b62338e8e06b299e1259c76 fixes this