Modifiers and allocated buffers with copy-on-write

Hey there,

I’ve been fighting with this modifier addition for a few days now and I just don’t understand what I’m missing.

I’m making a modifier that copies the positions of points from a different mesh. It’s very simple, and the base version (where it looks up the points by index) works just fine.

I added the ability to bind verts by using the BVHTree API and allocating an int* array with MEM_mallocN. This works great, and finds all the verts, and I can blend it off with a vertex group.

If I move the target mesh, things are fine. However, if I move the object that the modifier is on, I lose my pointer and the bindings.

I’m really not sure what I’m doing wrong. I think it has to do with the copying, and I’m not copying a pointer properly, but I’ve been using MOD_meshdeform.c as a reference and I don’t think I’m doing anything differently. I tried adding an operator in C to trigger the eval, and I tried writing to my modifier’s mod->modifier->orig_modifier_data when I can. I’ve run debugs and traced values. When things are working everything looks great, but I move the object that has the modifier the modifier struct passed in doesn’t contain the same data.

Is there anyone around who could help me on this? I’m posting the code here:
https://pastebin.com/TvJcB9MZ

It contains the stuff I added to DNA_modifier_types.h (the main struct), MOD_vertexsnap.c, and object_modifier.c (for the bind operator). The allocated memory gets stuffed into VertexSnapModifierData->bindings.

If this is too hard, I can fork the current master and work this code in. I’m not posting my branch because it contains a lot of things unrelated to this issue.

Thanks in advance!