How to solve "double free or corruption (!prev)" error in mixed nonlinear problem?

Hello,
I am attempting to solve a mixed nonlinear problem with conditions enforced by 6 Lagrange multipliers. I implemented the problem successfully in FEniCS legacy. However, after changing the code to FEniCSx I receive the error message shown below.

I am using FEniCSx via conda-forge on Ubuntu 22.04.2. The FEniCSx tutorials are executing without any problems.

This is a MWE that should converge after 0 iterations:

import ufl
from ufl import FiniteElement, MixedElement, TestFunctions, VectorElement, \
    outer, cross, diff, grad, det, tr, dot, dx, split

from petsc4py.PETSc import ScalarType
from mpi4py import MPI
from dolfinx import fem, mesh, nls

# Create mesh
domain = mesh.create_rectangle(MPI.COMM_WORLD, [[- 0.5, - 0.5], [0.5, 0.5]], [20, 20],
                               mesh.CellType.quadrilateral)
x = ufl.SpatialCoordinate(domain)

# Function spaces and functions
V = VectorElement("Lagrange", domain.ufl_cell(), degree=2, dim=3) # Deformation field
Q = FiniteElement("Real", domain.ufl_cell(), 0) # Single Lagrange multiplier
L = MixedElement([Q, Q, Q]) # Field of Lagrange multipliers
W = fem.FunctionSpace(domain, MixedElement([V, L, L]))

w = fem.Function(W)
(u, l, m) = split(w)
#(u, l, m) = w.split()
(d_x, d_l, d_m) = TestFunctions(W)

# Deformed configuration
x_cs = ufl.as_vector([x[0], x[1], 0]) + u

# Multiplicants of the Lagrange multipliers
A = ufl.as_tensor([
    [0, x_cs[2], x_cs[1]],
    [x_cs[2], 0, x_cs[0]],
    [x_cs[1], x_cs[0], 0]
]).T

B = ufl.as_vector([
    x_cs[1] * x_cs[2],
    x_cs[0] * x_cs[2],
    x_cs[0] * x_cs[1]
])

# Weak form functional
Func = (
    dot(l + A * m, d_x) + dot(x_cs, d_l) + dot(B, d_m)
) * dx

Jac = ufl.derivative(Func, w)

# Solving the problem
problem = fem.petsc.NonlinearProblem(Func, w, bcs=[], J=Jac)
solver = nls.petsc.NewtonSolver(MPI.COMM_WORLD, problem)
num_its, converged = solver.solve(w)

Previously I was splitting the Function with (u, l, m) = w.split(), which led to this error, which was reported before:

[0]PETSC ERROR: ------------------------------------------------------------------------
[0]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range
[0]PETSC ERROR: Try option -start_in_debugger or -on_error_attach_debugger
[0]PETSC ERROR: or see https://petsc.org/release/faq/#valgrind
[0]PETSC ERROR: or try http://valgrind.org on GNU/linux and Apple MacOS to find memory corruption errors
[0]PETSC ERROR: configure using --with-debugging=yes, recompile, link, and run 
[0]PETSC ERROR: to get more information on the crash.
[0]PETSC ERROR: Run with -malloc_debug to check if memory corruption is causing the crash.
--------------------------------------------------------------------------
MPI_ABORT was invoked on rank 0 in communicator MPI_COMM_WORLD
with errorcode 59.

NOTE: invoking MPI_ABORT causes Open MPI to kill all MPI processes.
You may or may not see output from other processes, depending on
exactly when Open MPI kills them.
--------------------------------------------------------------------------

Thanks to the advice in the post linked below I changed it to (u, l, m) = split(w), which is also how I am using it in my FEniCS legacy code.

However, now I obtain the following error message:

double free or corruption (!prev)
[mypc:248790] *** Process received signal ***
[mypc:248790] Signal: Aborted (6)
[mypc:248790] Signal code:  (-6)
[mypc:248790] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x42520)[0x7f2ad9642520]
[mypc:248790] [ 1] /lib/x86_64-linux-gnu/libc.so.6(pthread_kill+0x12c)[0x7f2ad9696a7c]
[mypc:248790] [ 2] /lib/x86_64-linux-gnu/libc.so.6(raise+0x16)[0x7f2ad9642476]
[mypc:248790] [ 3] /lib/x86_64-linux-gnu/libc.so.6(abort+0xd3)[0x7f2ad96287f3]
[mypc:248790] [ 4] /lib/x86_64-linux-gnu/libc.so.6(+0x896f6)[0x7f2ad96896f6]
[mypc:248790] [ 5] /lib/x86_64-linux-gnu/libc.so.6(+0xa0d7c)[0x7f2ad96a0d7c]
[mypc:248790] [ 6] /lib/x86_64-linux-gnu/libc.so.6(+0xa2efc)[0x7f2ad96a2efc]
[mypc:248790] [ 7] /lib/x86_64-linux-gnu/libc.so.6(free+0x73)[0x7f2ad96a54d3]
[mypc:248790] [ 8] /home/myuser/anaconda3/envs/fenicsx-env/lib/python3.10/site-packages/dolfinx/cpp.cpython-310-x86_64-linux-gnu.so(+0x23b5c8)[0x7f2acce3b5c8]
[mypc:248790] [ 9] /home/myuser/anaconda3/envs/fenicsx-env/lib/python3.10/site-packages/dolfinx/cpp.cpython-310-x86_64-linux-gnu.so(+0xda526)[0x7f2acccda526]
[mypc:248790] [10] /home/myuser/anaconda3/envs/fenicsx-env/lib/python3.10/site-packages/dolfinx/cpp.cpython-310-x86_64-linux-gnu.so(+0x77ce0)[0x7f2accc77ce0]
[mypc:248790] [11] /home/myuser/anaconda3/envs/fenicsx-env/bin/python(+0x13d2d7)[0x55f8b077c2d7]
[mypc:248790] [12] /home/myuser/anaconda3/envs/fenicsx-env/bin/python(_PyObject_MakeTpCall+0x26b)[0x55f8b0775bdb]
[mypc:248790] [13] /home/myuser/anaconda3/envs/fenicsx-env/bin/python(_PyEval_EvalFrameDefault+0x559e)[0x55f8b0771afe]
[mypc:248790] [14] /home/myuser/anaconda3/envs/fenicsx-env/bin/python(_PyFunction_Vectorcall+0x6f)[0x55f8b077c73f]
[mypc:248790] [15] /home/myuser/anaconda3/envs/fenicsx-env/bin/python(_PyEval_EvalFrameDefault+0x332)[0x55f8b076c892]
[mypc:248790] [16] /home/myuser/anaconda3/envs/fenicsx-env/bin/python(+0x148fa1)[0x55f8b0787fa1]
[mypc:248790] [17] /home/myuser/anaconda3/envs/fenicsx-env/lib/python3.10/site-packages/dolfinx/cpp.cpython-310-x86_64-linux-gnu.so(+0x1d1cc8)[0x7f2accdd1cc8]
[mypc:248790] [18] /home/myuser/anaconda3/envs/fenicsx-env/lib/python3.10/site-packages/dolfinx/../../../libdolfinx.so.0.6(_ZN7dolfinx3nls5petsc12NewtonSolver5solveEP6_p_Vec+0x1a1)[0x7f2accb890f1]
[mypc:248790] [19] /home/myuser/anaconda3/envs/fenicsx-env/lib/python3.10/site-packages/dolfinx/cpp.cpython-310-x86_64-linux-gnu.so(+0x1c7eee)[0x7f2accdc7eee]
[mypc:248790] [20] /home/myuser/anaconda3/envs/fenicsx-env/lib/python3.10/site-packages/dolfinx/cpp.cpython-310-x86_64-linux-gnu.so(+0x77ce0)[0x7f2accc77ce0]
[mypc:248790] [21] /home/myuser/anaconda3/envs/fenicsx-env/bin/python(+0x13d2d7)[0x55f8b077c2d7]
[mypc:248790] [22] /home/myuser/anaconda3/envs/fenicsx-env/bin/python(_PyObject_MakeTpCall+0x26b)[0x55f8b0775bdb]
[mypc:248790] [23] /home/myuser/anaconda3/envs/fenicsx-env/bin/python(+0x149134)[0x55f8b0788134]
[mypc:248790] [24] /home/myuser/anaconda3/envs/fenicsx-env/bin/python(_PyEval_EvalFrameDefault+0x4d1d)[0x55f8b077127d]
[mypc:248790] [25] /home/myuser/anaconda3/envs/fenicsx-env/bin/python(_PyFunction_Vectorcall+0x6f)[0x55f8b077c73f]
[mypc:248790] [26] /home/myuser/anaconda3/envs/fenicsx-env/bin/python(_PyEval_EvalFrameDefault+0x735)[0x55f8b076cc95]
[mypc:248790] [27] /home/myuser/anaconda3/envs/fenicsx-env/bin/python(+0x1d5852)[0x55f8b0814852]
[mypc:248790] [28] /home/myuser/anaconda3/envs/fenicsx-env/bin/python(PyEval_EvalCode+0x87)[0x55f8b0814797]
[mypc:248790] [29] /home/myuser/anaconda3/envs/fenicsx-env/bin/python(+0x206eec)[0x55f8b0845eec]
[mypc:248790] *** End of error message ***
Aborted (core dumped)

Do you have any idea what could be wrong with my code? I struggle to understand what causes this error in FEniCSx, while everything is working fine in FEniCS legacy.

Thank you very much in advance for your help!

As far as Im aware, real elements have not been implemented in DOLFINx yet, i.e.

import ufl
from petsc4py.PETSc import ScalarType
from mpi4py import MPI
from dolfinx import fem, mesh

# Create mesh
domain = mesh.create_rectangle(MPI.COMM_WORLD, [[- 0.5, - 0.5], [0.5, 0.5]], [20, 20],
                               mesh.CellType.quadrilateral)

# Function spaces and functions
Q = ufl.FiniteElement("Real", domain.ufl_cell(), 0) # Single Lagrange multiplier
W = fem.FunctionSpace(domain, Q)

will throw an error when trying to generate this function space