Guillermo Venegas cd2cfdeab0 Allocator: Properly free polymorphic objects
Currently `MEM_delete` frees pointers expecting that they match to the
pointers allocated with `MEM_new`, otherwise it can cause undefined
behavior when freeing memory(using `--debug-memory` flag breaks in
place, if not it can corrupts other data, generating a incorrect back-traces).
However polymorphic objects lifetime can be managed by pointer of their
most derived type or by any pointer in their ancestor tree that defines
a virtual destructor, which sometimes can differ in offset when pointing to
the same object.

This changes ensures the correct pointer is being freed, by using the pointer
to the most derived type (returned by`dynamic_cast<void *>(...);`[0]).

----------

[0] = [dynamic_cast](https://en.cppreference.com/w/cpp/language/dynamic_cast.html):  `a) If expression is a pointer to (possibly cv-qualified) void, the result is a pointer to the most derived object pointed to by expression.`

-----------

As an example, given the followings structs:
```c++
struct A {
  int a;
  virtual ~A() = default;
};

struct B {
  int b;
  virtual ~B() = default;
};

struct Derived : public A , public B {
  int c;
};

std::unique_ptr<A> a_ptr = std::make_unique<Derived>();
std::unique_ptr<B> b_ptr = std::make_unique<Derived>();
```
Using std smart pointers  to manage `Derived` objects can be done with `A`
or `B` pointers.

However if a `Derived` object memory is managed with `MEM_delete`,
using a `B` pointer for freeing the memory currently may silently break Blender,
since it don't accounts for the full object memory layout, the `dynamic_cast<void *>(ptr);`
cast  gives a more safe pointer for freeing the memory.
Note that object destruction is successfully handled through the virtual destructor.

----------

This instead could be an assert to ensure polymorphic objects to be deleted
as the most derived object type.

Pull Request: https://projects.blender.org/blender/blender/pulls/146269
2025-09-23 16:50:43 +02:00
2025-08-11 12:02:20 +02:00
2023-12-08 13:28:13 +11:00
2025-07-10 12:48:33 +10:00
2025-09-23 09:37:03 +10:00

Blender

Blender is the free and open source 3D creation suite. It supports the entirety of the 3D pipeline—modeling, rigging, animation, simulation, rendering, compositing, motion tracking and video editing.

Blender screenshot

Project Pages

Development

License

Blender as a whole is licensed under the GNU General Public License, Version 3. Individual files may have a different but compatible license.

See blender.org/about/license for details.

Description
No description provided
Readme 841 MiB
Languages
C++ 78%
Python 14.9%
C 2.9%
GLSL 1.9%
CMake 1.2%
Other 0.9%