Skip to content

Strange Arc::ptr_eq behaviour (duplicate vtables?) #63021

Closed
@TrionProg

Description

@TrionProg

Hello. This issue is simmilar to #48795 and #46139 but have more strange behaviour and suggestion.

The problem is located when we clone Arc to Arc<Trait> in one function, return Arc and then clone Arc to Arc<Trait> in "parent" function, but Arc::ptr_eq says, that Arc<Trait>(called function) != Arc<Trait>(parent function).

Where is example of this problem, but Arc::ptr_eq works!
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=95eef8742b08ab94f3fbf3048dc514b2

We see, that data pointer is equal, but vtables are different, but implementations are correct.

In my case I have more traits, etc, project in 14k lines, and Arc::ptr_eq do not works, but this objects/traits are located in same crate. Note this problem is still occured in build --release cause, and implementations of vtables eat memory.

So..

  1. Why Arc::ptr_eq match pointers to vtable, but I just need to know, points Arc to object that I search. This behaviour looks strange for user and may cause problems, when project will be large, as in my case (previously it worked correctly).
  2. How should I match Arc-s?
let ptr_equal = unsafe {
                    let (a,_):(usize, usize) = std::mem::transmute_copy(&node.widget_instance);
                    let (b,_):(usize, usize) = std::mem::transmute_copy(widget);

                    a==b // Addresses of ArcInner
                };

or

impl WidgetInstanceTrait {
    pub fn ref_eq(&self, other:&Arc<WidgetTrait>) -> bool {
        self.ptr_eq(&**other)
    }

    pub fn ptr_eq(&self, other:&WidgetTrait) -> bool {
        unsafe {
            let (a,_):(usize, usize) = std::mem::transmute_copy(&self);
            let (b,_):(usize, usize) = std::mem::transmute_copy(&other);

            a==b // Addresses of data (self)
        }
    }
}

and

if node.widget_instance.ptr_eq(&**widget) { ... }
if node.widget_instance.ptr_eq(widget) { ... }

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions