I think this fixes the memory leak

I did not wait for a reply to my previous message to you (through moodle.org) I just went head and looked at the code, and it seemed quite easy to devise a fix by changing to use a real function.

That said, I have only done minimal testing on this (using a simple link to manywater.pdb). You probably have more examples you can use to test this.

I hope this is helpful.

Note, I changed the code I moved to more closely match the Moodle coding style, but I didn't reformat anything else. When you have a spare moment, you might like to use the tool https://github.com/moodlehq/moodle-local_codechecker/ on the code.