holzschu / lib-tex

pdftex and luatex as a library. Developped for iOS, but should work on all systems.
72 stars 8 forks source link

About Memory Leak #8

Closed mc-public closed 1 year ago

mc-public commented 1 year ago

Hi, @holzschu. I found that after calling pdftex and luatex, there is a certain amount of memory leak. For luatex, this is more noticeable. Since I can't create new processes for them on iOS, the app will crash after calling them multiple times. Is there any way to fix this leak?

Also, if I want to port more tex engines to iOS (e.g. xetex and ptex), what do I probably need to do?

Looking forward to your answer!

holzschu commented 1 year ago

Hi, If you look at the code, you can see the specific __IPHONE__ parts, which mostly deal with cleaning the data when we exit TeX. It's a delicate balance: free data as much as possible, but avoid a crash because something tries to access data that has already been cleaned. You can probably improve the data cleaning, especially if you know what you're looking for.

To port more engines, the first step is to remove the --disable-engine line in the build_command.sh, then start the command. Then you will see error messages that you can work on in order to make the commands compile. The code for xetex is very MacOS-specific, and it is very difficult to adapt for iOS (unless you are already an expert on iOS and MacOS). Since luatex has actually more features than xetex, I don't see it as a priority (but you're welcome to try). ptex will probably be easier.

Once the engines compile, the next steps are:

mc-public commented 1 year ago

Thank you for your answer. If I use some garbage collection repository (e.g https://github.com/mkirchner/gc ) and replace all memory management functions such as malloc and free in texlive, is it possible to solve the problem of memory leakage?

holzschu commented 1 year ago

It' worth a try, but the issue is not just memory allocation but also ensuring that everything is reset to the default value so that it is reallocated in the next run. You do not want the program to try to access a pointer that was allocated and freeed in the previous run. Since luatex is already clearing most of the memory structures allocated, it might be more efficient to track down the remaining non-freeed structures.

mc-public commented 1 year ago

After trying hard for a period of time, I successfully transplanted xetex to the iOS platform. The key point in cross compilation is to use the fontconfig library, rather than the relevant header files with "Mac" in the xetexdir folder. When cross compiling, I created an Xcode project. First, use web2c to manually convert xetex.web into c language files, and add these files to the Xcode project. Then, I use GNU autotools to manually configure under the kpathsea folder, and add the configured c file to the Xcode project. Simply process the dependent libraries of xetex such as TECkit, huffbuzz, and icu4c, and then drag them into the Xcode project. Finally, I added some CFlags to the xcode project, set the header search path, and then compiled successfully after simple modification.