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

Compiling string flag with -autofree ends in heap-corruption #9815

Open
bstnbuck opened this issue Apr 19, 2021 · 1 comment
Open

Compiling string flag with -autofree ends in heap-corruption #9815

bstnbuck opened this issue Apr 19, 2021 · 1 comment
Labels
Autofree Bugs/feature requests, that are related to -autofree. Bug This tag is applied to issues which reports bugs. Unit: Memory Management Bugs/feature requests, that are related to the memory management of the compiler.

Comments

@bstnbuck
Copy link
Contributor

V version: 0.2.2 4a1f75c
OS: all

What did you do?

import flag
import os

fn main() {
	mut fp := flag.new_flag_parser(os.args)
	test := fp.string('test', 0, '', 'this is a test')

	_ := fp.finalize() or {
		println(fp.usage())
		return
	}
	println(test)
}

Built it with: v -autofree -prod -cc [gcc, msvc, tcc] test.v
Executed with: test.exe --test sometext

What did you expect to see?
sometext

What did you see instead?
some garbage and after repeating several times (on Windows):

Unhandled Exception 0xC0000374
11: RtlIsZeroMemory            ?? : address = 0x61c4dce108
10: RtlpNtSetValueKey          ?? : address = 0x61c4dce108
9 : RtlpNtSetValueKey          ?? : address = 0x61c4dce108
8 : RtlpNtSetValueKey          ?? : address = 0x61c4dce108
7 : RtlGetCurrentServiceSessionId  ?? : address = 0x61c4dce108
6 : RtlFreeHeap                ?? : address = 0x61c4dce108
5 : free_base                  ?? : address = 0x61c4dce108
4 : flag__FlagParser_usage     ?? : address = 0x61c4dce108
3 : wmain                      ?? : address = 0x61c4dce108
2 : __scrt_common_main_seh     d:\agent\_work\63\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
1 : BaseThreadInitThunk        ?? : address = 0x61c4dce108
0 : RtlUserThreadStart         ?? : address = 0x61c4dce108

I think the error is related to os.args and -autofree

@bstnbuck bstnbuck added the Bug This tag is applied to issues which reports bugs. label Apr 19, 2021
@bstnbuck
Copy link
Contributor Author

Compiling with -o test.c gives the following C-Code and I think the error too.

VV_LOCAL_SYMBOL void main__main(void) {
	flag__FlagParser* fp = flag__new_flag_parser(_const_os__args);
	string test = flag__FlagParser_string(fp, _SLIT("test"), 0, _SLIT(""), _SLIT("this is a test"));
	Option_Array_string _t352 = flag__FlagParser_finalize(/*rec*/*fp);
	if (_t352.state != 0) { /*or block*/ 
		IError err = _t352.err;
string _arg_expr_println_1_156 = flag__FlagParser_usage(/*rec*/*fp);// new af2 pre/* inserted before */
				println(/*af arg2*/_arg_expr_println_1_156);
		// free before return (no values returned)
		// autofree_scope_vars(pos=177 line_nr=9 scope.pos=152 scope.end_pos=186)
		// var "err" var.pos=152 var.line_nr=7
		// var "_arg_expr_println_1_156" var.pos=156 var.line_nr=8
			string_free(&_arg_expr_println_1_156); // autofreed var main false
		// af parent scope:
		// var "fp" var.pos=40 var.line_nr=4
			flag__FlagParser_free(fp); // autofreed ptr var
		// var "test" var.pos=77 var.line_nr=5
			string_free(&test); // autofreed var main false
		// af parent scope:
		// af parent scope:
		return;
	// autofree_scope_vars(pos=155 line_nr=8 scope.pos=152 scope.end_pos=186)
	// var "err" var.pos=152 var.line_nr=7
	// var "_arg_expr_println_1_156" var.pos=156 var.line_nr=8
		string_free(&_arg_expr_println_1_156); // autofreed var main false
	};
	println(test);
// autofree_scope_vars(pos=42 line_nr=4 scope.pos=26 scope.end_pos=203)
// var "fp" var.pos=40 var.line_nr=4
	flag__FlagParser_free(fp); // autofreed ptr var    <=== THIS pointer should be freed after
// var "test" var.pos=77 var.line_nr=5
	string_free(&test); // autofreed var main false   <=== THAT one
}

@danieldaeschle danieldaeschle added the Unit: Memory Management Bugs/feature requests, that are related to the memory management of the compiler. label Jul 22, 2021
@felipensp felipensp added the Autofree Bugs/feature requests, that are related to -autofree. label Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Autofree Bugs/feature requests, that are related to -autofree. Bug This tag is applied to issues which reports bugs. Unit: Memory Management Bugs/feature requests, that are related to the memory management of the compiler.
Projects
None yet
Development

No branches or pull requests

3 participants