Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

str_intp: autofree memory leak fix 1 #23549

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kbkpbot
Copy link
Contributor

@kbkpbot kbkpbot commented Jan 22, 2025

This PR try to fix memory leak cause by str_intp().

leak.v:

struct Test {
        a string
}

fn main() {
        cmd1 := 'ls'
        cmd2 := 'rm'
        _ := Test {
                a : 'cmd ${cmd1} ${cmd2} failed'
        }
}

without autofree, generate c:

VV_LOCAL_SYMBOL void main__main(void) {
        string cmd1 = _SLIT("ls");
        string cmd2 = _SLIT("rm");
        {main__Test _ = ((main__Test){.a = str_intp(3, _MOV((StrIntpData[]){{_SLIT("cmd "), 0xfe10, {.d_s = cmd1}}, {_SLIT(" "), 0xfe10, {.d_s = cmd2}}, {_SLIT(" failed"), 0, { .d_c = 0 }}})),});}
        ;
}

with autofree, generate c:

VV_LOCAL_SYMBOL void main__main(void) {
        string cmd1 = _SLIT("ls");
        string cmd2 = _SLIT("rm");
        string _t1 = str_intp(3, _MOV((StrIntpData[]){{_SLIT("cmd "), 0xfe10, {.d_s = cmd1}}, {_SLIT(" "), 0xfe10, {.d_s = cmd2}}, {_SLIT(" failed"), 0, { .d_c = 0 }}}));
        {main__Test _ = ((main__Test){.a = _t1,});}
        ;
        string_free(&_t1); // autofreed var main false
        string_free(&cmd2); // autofreed var main false
        string_free(&cmd1); // autofreed var main false
}

and run valgrind will get a clean result.

valgrind --error-exitcode=1 --leak-check=full --show-reachable=yes ./leak

==510777== Memcheck, a memory error detector
==510777== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==510777== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
==510777== Command: ./leak
==510777==
==510777==
==510777== HEAP SUMMARY:
==510777==     in use at exit: 0 bytes in 0 blocks
==510777==   total heap usage: 3 allocs, 3 frees, 274 bytes allocated
==510777==
==510777== All heap blocks were freed -- no leaks are possible
==510777==
==510777== For lists of detected and suppressed errors, rerun with: -s
==510777== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
#-gc boehm_leak

Copy link

Connected to Huly®: V_0.6-21977

@kbkpbot
Copy link
Contributor Author

kbkpbot commented Jan 22, 2025

It seems conflict to fn call args autofree, fix later...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant