Skip to content

Destructor missing for templated class (like smart pointer) #1017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
usamec opened this issue Sep 22, 2017 · 1 comment
Closed

Destructor missing for templated class (like smart pointer) #1017

usamec opened this issue Sep 22, 2017 · 1 comment

Comments

@usamec
Copy link

usamec commented Sep 22, 2017

Input C/C++ Header

template<class T>
class Pointer {
 private:
  T* data;
  char xx;
  char yy;
 public:
  Pointer(T* data);
  ~Pointer();

  int method();

  T& operator*();
  T* operator->();
};


class A {
 public:
  int b, c, d;
  Pointer<A> x;
};

Pointer<A> MakeA();

Bindgen Invocation

$ bindgen test_bind.hpp -o src/bindings.rs -- -x c++ -std=c++11

Actual Results

#[repr(C)]
#[derive(Debug)]
pub struct Pointer<T> {
    pub data: *mut T,
    pub xx: ::std::os::raw::c_char,
    pub yy: ::std::os::raw::c_char,
    pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
#[repr(C)]
#[derive(Debug)]
pub struct A {
    pub b: ::std::os::raw::c_int,
    pub c: ::std::os::raw::c_int,
    pub d: ::std::os::raw::c_int,
    pub x: Pointer<A>,
}
#[test]
fn bindgen_test_layout_A() {
    assert_eq!(::std::mem::size_of::<A>() , 32usize , concat ! (
               "Size of: " , stringify ! ( A ) ));
    assert_eq! (::std::mem::align_of::<A>() , 8usize , concat ! (
                "Alignment of " , stringify ! ( A ) ));
    assert_eq! (unsafe { & ( * ( 0 as * const A ) ) . b as * const _ as usize
                } , 0usize , concat ! (
                "Alignment of field: " , stringify ! ( A ) , "::" , stringify
                ! ( b ) ));
    assert_eq! (unsafe { & ( * ( 0 as * const A ) ) . c as * const _ as usize
                } , 4usize , concat ! (
                "Alignment of field: " , stringify ! ( A ) , "::" , stringify
                ! ( c ) ));
    assert_eq! (unsafe { & ( * ( 0 as * const A ) ) . d as * const _ as usize
                } , 8usize , concat ! (
                "Alignment of field: " , stringify ! ( A ) , "::" , stringify
                ! ( d ) ));
    assert_eq! (unsafe { & ( * ( 0 as * const A ) ) . x as * const _ as usize
                } , 16usize , concat ! (
                "Alignment of field: " , stringify ! ( A ) , "::" , stringify
                ! ( x ) ));
}
extern "C" {
    #[link_name = "_Z5MakeAv"]
    pub fn MakeA() -> Pointer<A>;
}
#[test]
fn __bindgen_test_layout_Pointer_open0_A_close0_instantiation() {
    assert_eq!(::std::mem::size_of::<Pointer<A>>() , 16usize , concat ! (
               "Size of template specialization: " , stringify ! ( Pointer<A>
               ) ));
    assert_eq!(::std::mem::align_of::<Pointer<A>>() , 8usize , concat ! (
               "Alignment of template specialization: " , stringify ! (
               Pointer<A> ) ));
}
#[test]
fn __bindgen_test_layout_Pointer_open0_A_close0_instantiation_1() {
    assert_eq!(::std::mem::size_of::<Pointer<A>>() , 16usize , concat ! (
               "Size of template specialization: " , stringify ! ( Pointer<A>
               ) ));
    assert_eq!(::std::mem::align_of::<Pointer<A>>() , 8usize , concat ! (
               "Alignment of template specialization: " , stringify ! (
               Pointer<A> ) ));
}

Expected Results

Destructor for Pointer is missing.
Same behaviour happens with ordinary c++ shared_ptr.

@fitzgen
Copy link
Member

fitzgen commented Sep 22, 2017

Thanks for the bug report! This is, unfortunately, expected for two reasons:

  1. The destructor is inline (I was inspired to start an FAQ to explain this: Create an FAQ section in the book #1020)

  2. We don't know what monomorphizations of the function exist (or will exist!) and we aren't a C++ compiler, so we can't generate new monomorphizations. We do have Generate bindings to explicitly instantiated function templates #492 open to generate bindings to explicitly instantiated templates.

I'm going to close this issue, since I think #492 covers all the actionable bits.

@fitzgen fitzgen closed this as completed Sep 22, 2017
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

No branches or pull requests

2 participants