Internal memory allocations

I’m doing some work with ctypes where there are some internal memory allocations (not PyObjects) when processing C structures and unions. These allocations happen in a loop where they happen for some loop iterations but not for others, and the loop count is determined by user input (essentially the number of fields in the struct or union being processed). If we hit an out of memory error (e.g. PyMem_New returns NULL) during a particular loop iteration, apart from setting PyErr_NoMemory and returning a suitable value, are we expected to keep track of all allocations done earlier (including earlier loop iterations) and free them? If so, are there any particular patterns or utility code someone can point me to for doing this? I don’t want to come up with something and then find that’s not the way it’s meant to be done.

Yes, you should free any allocations that you’ve made if you then abandon the task.

If they’re temporary and internal, maybe link them together so you can just walk back from the last successful one? I don’t think this particular one has any reasonable limit we can compile in.