mystor / rust-cpp

Embed C++ directly inside your rust code!
Apache License 2.0
795 stars 44 forks source link

class on free aborts on invalid pointer #83

Open andyjsbell opened 4 years ago

andyjsbell commented 4 years ago

Using the cpp_class! macro with a simple class aborts on freeing the resource. This is happening on Ubuntu 1804. Code snippet reproducing the issue:

use cpp::cpp;
use cpp::cpp_class;

cpp!{{
    #include <iostream>

    using std::string;

    class Test {
    public:
        Test() {
            std::cout << "creating" << std::endl;
        }
        virtual ~Test() {
            std::cout << "destroying" << std::endl;
        }

    protected:
        std::string message; // -> removing this and we don't get the issue
    };
}}

cpp_class!(pub unsafe struct Test as "Test");
impl Test {
    fn new() -> Self {
        unsafe { cpp!([] -> Test as "Test" { return Test(); }) }
    }
}

fn main() {
    let test = Test::new();
}
ogoffart commented 4 years ago

Yeah, cpp_class only work with class that are relocatable.

This is documented there: https://docs.rs/cpp/0.5.5/cpp/macro.cpp_class.html#relocatable-classes

Perhaps the documentation should be more explicit, or extra warning should be in order.

andyjsbell commented 4 years ago

I decided to test this out on OSX and Windows. Interestingly both Windows and OSX did not have the issue.

ogoffart commented 4 years ago

This depends on the implementation of the standard library.

LastLightSith commented 2 years ago

Hi, out of curiosity, why test is relocatable immediately ? it is used only once.

ogoffart commented 2 years ago

Internally it is returned from the Test::new() function so that's one possible relocation (even if it might be optimized) Also internally in the cpp! macro