Skip to content

Add const raw pointer model getters, deprecate the UB shared_ptr ones - #6820

Merged
Fedr merged 12 commits into
masterfrom
obj-const-ptr
Sep 10, 2026
Merged

Add const raw pointer model getters, deprecate the UB shared_ptr ones#6820
Fedr merged 12 commits into
masterfrom
obj-const-ptr

Conversation

@Fedr

@Fedr Fedr commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

ObjectMeshHolder::mesh(), ObjectLinesHolder::polyline() and ObjectPointsHolder::pointCloud() return

return reinterpret_cast< const std::shared_ptr<const Mesh>& >( data_.mesh );

std::shared_ptr<T> and std::shared_ptr<const T> are unrelated types, so this is a strict-aliasing violation - undefined behaviour that every implementation happens to get away with only because the two have identical layout. GCC sees through it and each of the three functions needs its own #pragma GCC diagnostic ignored "-Wstrict-aliasing" block, commented "Fingers crossed."

Almost every caller only dereferences the result, so this PR:

  • adds a raw pointer getter pair - const and mutable - next to each of the three, named after the existing varMesh() / mesh() pair: meshPtr() / varMeshPtr(), polylinePtr() / varPolylinePtr(), pointCloudPtr() / varPointCloudPtr();
  • switches all 222 call sites in the repository to the const one;
  • marks the three casting functions [[deprecated]], so nothing new starts using them and they can be removed later.

The only caller that really needs shared ownership is SaveSelectedMenuItem::action(), which fills MeshSave::NamedXfMesh and captures it in a ProgressBar lambda; it now takes the shared_ptr from varMesh() instead.

Two knock-on changes outside the call sites:

  • the deprecated three stay exposed to Python, C and C#; mrbind wraps every call to a deprecated entity in MRBINDC_IGNORE_DEPRECATION, so the generated bindings need nothing from us. scripts/mrbind/aliases.cpp resolves extractMesh / extractLines / extractPoints by name at import time, and those now point at the new getters so they keep working once the deprecated ones are removed.
  • pythonGetSelectedModels in mrviewerpy takes the getter as a member pointer, so it decided whether to dereference from MR::Meta::SharedPtrTraits; it now unwraps a raw pointer as well as a shared_ptr.

Verified locally with MSVC cl /Zs and /we4996, i.e. any remaining use of a deprecated getter is a compile error: MRMesh (305 TUs), MRViewer (153), MRVoxels (37), mrviewerpy (3) and the remaining 166 TUs under source/ are clean apart from the sweep's own known artifacts (MRSparsePolynomial's missing <iterator> without the project PCH, the win32 TUs that need /FI windows.h, and dllimport errors from not passing each library's <Lib>_EXPORTS).

Fedr added 8 commits September 9, 2026 20:09
… the shared_ptr getters

ObjectMeshHolder::mesh(), ObjectLinesHolder::polyline() and
ObjectPointsHolder::pointCloud() return a reference to the stored
std::shared_ptr<T> reinterpret_cast to std::shared_ptr<const T>, which is
undefined behaviour and needs a -Wstrict-aliasing suppression on GCC.

Almost every caller only dereferences the result, so add a plain const raw
pointer getter next to each of them, switch all callers to it, and mark the
three casting functions [[deprecated]].

The single caller that really needs shared ownership (SaveSelectedMenuItem
filling MeshSave::NamedXfMesh) now takes it from varMesh().
… C4996

MR_BIND_IGNORE keeps the deprecation diagnostic live in the generated C bindings; the price is that mesh()/polyline()/pointCloud() disappear from the Python, C and C# APIs right away.
pythonGetSelectedModels took the getter as a member pointer, so the earlier call-site sweep missed it; the template now unwraps a raw pointer as well as a shared_ptr.
extractMesh/extractLines/extractPoints are resolved by name at import time, so hiding mesh()/polyline()/pointCloud() from the bindings broke 'import meshlib.mrmeshpy'.
Their Python/C/C# spelling stays available; MeshLibC2 mutes C4996 again for the generated code that calls them.
… in the generated C bindings"""

This reverts commit 84bce22.
…ames

Each of the three holders now offers both a const and a mutable raw pointer to
its model, following the varMesh()/mesh() naming already used for the shared_ptr
accessors.
@Fedr
Fedr merged commit a55be14 into master Sep 10, 2026
55 checks passed
@Fedr
Fedr deleted the obj-const-ptr branch September 10, 2026 10:47
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

Successfully merging this pull request may close these issues.

3 participants