-
Notifications
You must be signed in to change notification settings - Fork 86
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
BAD_ACCESS when accessing mock object with CRTP #188
Comments
OK. Long Easter weekend coming up, so I'll have plenty of time to look into it. Thanks for reporting. |
Hmm, you're slicing your object. Here's a very simple replication. template <typename Derived>
struct base
{
int func() {
return static_cast<Derived*>(this)->f();
}
};
struct D : base<D>
{
int i = 3;
int f() {
return i;
}
};
int func(base<D> obj) // note! copy of the base part of the full D object.
{
return obj.func();
}
int main()
{
D d;
return func(d);
} This error is caught with gcc/clang address-sanitizer, but its error pinpointing leaves a lot to desire. I would've expected the undefined-sanitizer to complain about the call after the cast, but it does not. Neither complains at all if the implemented function does not touch any member variables. If you change One thing I do with CRTP classes to avoid this problem, is to make their copy/move constructors protected. Your template <typename Derived>
struct base
{
base() = default;
int func() {
return static_cast<Derived*>(this)->f();
}
protected:
base(const base&) = default;
base(base&&) = default;
}; The failing code then gets an error:
Another solution is, of course, to keep the entire |
Also note that if you change |
Okay, so I changed it to accept a pointer to the |
So I have some CRTP code that I would like to mock, but I'm getting a bad access when trying to use the mocked object. My code can be boiled down to:
I get bad access when the trompeloeil mock calls the
find
method (that starts at line 3021 intrompeloeil.hpp
, specifically at the first instance ofi.matches(param_name)
on line 3024. This might be related to CRTP mocking as mentioned in #116, but I'm not sure. When I run this in the debugger, I get bad access when trying to accessi
. Based on what I see in the debugger, it looks likei
is a validcall_matcher_base
.What makes this really weird is if I have the following test case:
This works without issue. It seems like the issue is storing the mock object and then using it makes something weird happen.
The text was updated successfully, but these errors were encountered: