jlaurens / synctex

Synchronization for TeX
MIT License
64 stars 19 forks source link

Memory leak when opening files #49

Closed stloeffler closed 1 year ago

stloeffler commented 1 year ago

When calling synctex_scanner_new_with_output_file with parse=1, first a reader is initialized by a call to synctex_reader_init_with_output_file. This allocates a buffer of size SYNCTEX_BUFFER_SIZE (synctex_parser.c#L769-L773, accessible via reader->start. Then, synctex_scanner_parse is called which again allocates the same buffer (synctex_parser.c#L6107) without deallocating the previous memory, leading to the leak.

I see two possible ways to fix this: 1) If the buffer is guaranteed to not be deallocated/reallocated with a different size during the lifetime of the reader, the second allocation could just be removed 2) If that guarantee is not available, the buffer should be properly dellocated/reallocated as necessary in synctex_scanner_parse (or not be allocated in synctex_reader_init_with_output_file in the first place, but that might lead to all sorts of other problems with code expecting the buffer to always be valid)

aminophen commented 1 year ago

I tried to import changes on @jlaurens's '2020' branch into TL tree: https://github.com/TeX-Live/texlive-source/compare/synctex-update

The trial commit https://github.com/TeX-Live/texlive-source/commit/9db3842ee5f2821acac5ff9aa885c8ee7567e5e9 contains almost everything, but the latest "fix" (https://github.com/jlaurens/synctex/commit/17fd9f22ddb7c1afbd3bb0903d6b60c5d834b096) is omitted due to build failure:

gcc -DHAVE_CONFIG_H -I. -I../../../texk/web2c -I./w2c  -I../../../texk/web2c/synctexdir -I/Users/aminophen/tex-devel/texlive-svn/Build/source/Work/libs/zlib/include -DSYNCTEX_USE_LOCAL_HEADER  -Wimplicit -Wreturn-type -Wdeclaration-after-statement -Wno-unknown-pragmas -g -O2 -MT synctexdir/libsynctex_a-synctex_parser.o -MD -MP -MF synctexdir/.deps/libsynctex_a-synctex_parser.Tpo -c -o synctexdir/libsynctex_a-synctex_parser.o `test -f 'synctexdir/synctex_parser.c' || echo '../../../texk/web2c/'`synctexdir/synctex_parser.c
../../../texk/web2c/synctexdir/synctex_parser.c:6121:14: error: use of undeclared label 'bailey'
        goto bailey;
             ^
1 error generated.
make[5]: *** [synctexdir/libsynctex_a-synctex_parser.o] Error 1

so please revisit this issue. thanks -- hironobu

aminophen commented 1 year ago

Thanks for the update, installed on r65582 with other changes.