Fix pointer offset loss in OwnerPtr::Cast and ShadowPtr::Shadow under multiple inheritance - #10
Merged
Merged
Conversation
…wPtr Agent-Logs-Url: https://github.com/PrettyCADStudio/PrettyMemory/sessions/ebbc70ec-98a8-4e4e-9ac3-4bae158cccb0 Co-authored-by: zhuuuoyue <32356359+zhuuuoyue@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix pointer offset issue in OwnerPtr cast
Fix pointer offset loss in OwnerPtr::Cast and ShadowPtr::Shadow under multiple inheritance
Apr 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When casting through a multiple-inheritance hierarchy, converting
VT*→void*→VT2*viastatic_castdiscards the pointer offset, producing an incorrect pointer and corrupting all member access on the result.Root cause:
ControlBlock::Datastored the pointer asvoid*, andGet()re-derived the typed pointer viastatic_cast<VT*>(void*). This loses the offset adjustment that compilers apply when navigating multiple-inheritance bases.Fix: dual-pointer design (mirrors
std::shared_ptr)m_pTyped— a correctly-typedVT*added directly to bothOwnerPtrandShadowPtr, used exclusively for object access (Get(),->,*)ControlBlock::Data— unchanged; continues to hold the original allocation pointer for the deleter and acts as the liveness indicator forShadowPtrKey method updates:
Get()returnsm_pTypeddirectly — no morestatic_castfromvoid*Cast<VT2>()stores thedynamic_cast<VT2*>(m_pTyped)result in the newOwnerPtr'sm_pTypedShadow<VT2>()propagatesstatic_cast<VT2*>(m_pTyped)into the newShadowPtrTransfer(),Release(),Swap(),Destroy(), andEnableShadowFromThis::ShadowFromThis()updated accordinglyExample (previously broken):
Pre-existing compile errors also fixed (were blocking the build on GCC/Clang with C++17):
using DeleterType = DTinOwnerPtrT*vsstd::nullptr_tinShadowPtrandOwnerPtrcomparison operators (replaced withstatic_cast<ConstPointer>(nullptr))