Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"double free or corruption (!prev)" in quadric_simplify_mesh #6

Closed
gllmflndn opened this issue Nov 28, 2022 · 5 comments
Closed

"double free or corruption (!prev)" in quadric_simplify_mesh #6

gllmflndn opened this issue Nov 28, 2022 · 5 comments

Comments

@gllmflndn
Copy link

With the exemplar attached mesh (20480 faces), quadric_simplify_mesh runs successfully at reducing the number of faces from 20479 to 3750 but crashes when requesting 3749 faces (aggressiveness set to 7.0):

 iteration 0 - triangles 20480 threshold 2.187e-06
 iteration 1 - triangles 11952 threshold 1.6384e-05
 iteration 2 - triangles 7700 threshold 7.8125e-05
 iteration 3 - triangles 4790 threshold 0.000279936
 iteration 4 - triangles 3748 threshold 2.22045e-16
double free or corruption (!prev)

sphere.zip

@neurolabusc
Copy link
Owner

I am unable to replicate this issue. This project takes a NIfTI volume as input, not an obj file. Can you create a github repository with your source project to demonstrate some issues, and also provide details regarding the computer and compiler used (e.g. MacOS with gcc).

This code is a pure C implementation of this C++ code that creates a minimal tool for simplifying obj meshes. It does not exhibit the behavior you describe (on a Windows computer with the cl compiler).

@gllmflndn
Copy link
Author

I am currently using the C++ implementation here but would happily swap it for a C implementation so I wrote another MEX wrapper around quadric_simplify_mesh and that's where I observe the problem (Linux with gcc 9.4). I could share this wrapper but would you prefer a C only function that reproduces the error instead?

@neurolabusc
Copy link
Owner

@gllmflndn I have been able to replicate and fix your issue. To replicate and de-bug the issue I added the -t threshold parameter to the obj2mesh program and compiled it in sanitized mode to identify any memory issues:

gcc  -O1 -g -fsanitize=address -fno-omit-frame-pointer   -DNII2MESH -DHAVE_ZLIB MarchingCubes.c obj2mesh.c isolevel.c meshify.c quadric.c base64.c bwlabel.c radixsort.c -o obj2mesh -lz -lm
./obj2mesh -t 3749 -v 2 sphere.obj smoother.obj

This revealed that the refs array was too small to grow. In my C code, I pre-allocate this array with a worst case scenario size as there is a big performance penalty for growing its size. Clearly, my calculation for worst case scenario was not conservative enough, so I increased the buffer and the code now works without any memory exceptions and to a nice completion.

neurolabusc added a commit to rordenlab/nii2meshWeb that referenced this issue Dec 4, 2022
neurolabusc added a commit to rordenlab/niimath that referenced this issue Dec 4, 2022
@gllmflndn
Copy link
Author

Thanks @neurolabusc I confirm that with your fix I could reduce my example mesh for any value between 20480 and 1 and it ran smoothly.

On a very minor note, I notice a warning when compiling with -Wall:

quadric.c:452:7: warning: ‘iterationStartCount’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  452 |    if (iterationStartCount == (triangle_count-deleted_triangles)) break;
      |       ^

It's not a very pertinent warning here but I silence it with int iterationStartCount = 0;

neurolabusc added a commit that referenced this issue Dec 5, 2022
neurolabusc added a commit to rordenlab/nii2meshWeb that referenced this issue Dec 5, 2022
neurolabusc added a commit to rordenlab/niimath that referenced this issue Dec 5, 2022
@neurolabusc
Copy link
Owner

neurolabusc commented Dec 5, 2022

Thanks! I am closing this issue. The sphere is an interesting edge case in that initially all vertices have identical collapse costs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants