Closed zhlongfj closed 1 year ago
Don't create the mock on the heap, create it on the stack. And you need to specialize it otherwise it doesn't work.
That means I can't use shared_ptr to wrap the point from mock().get()?
You can, delete operator is overrided so it does nothing.
Yes override the delete operator
My code:
Mock mock1;
When(Method(mock1, resId)).AlwaysReturn("1");
auto meta = shared_ptr(mock1->get(), [](auto){});
Might be useful having this in the docs, though.
What i mean is that you don't need to do anything. It works without any workaround.
Mock mock1;
When(Method(mock1, resId)).AlwaysReturn("1");
auto meta = shared_ptr(mock1.get());
It's enough.
Yes override the delete operator
My code: Mock mock1; When(Method(mock1, resId)).AlwaysReturn("1"); auto meta = shared_ptr(mock1->get(), [](auto){});
It works, thanks
You can, delete operator is overrided so it does nothing.
It works, thanks
I've looked a bit more into the source code to understand how it works, the summary (for GCC) is:
For visual studio it's a bit the same but instead of two destructors in the vtable there is only one that point to a wrapper that call "~Class()", etc (and free).
In the end, what you need to remember is that you have nothing special to do, use the pointer without worrying about any potential double free. You don't need to set any custom deleter / destructor.
And also I wasn't able to reproduce the crash with a mock allocated on the heap : https://godbolt.org/z/dcTosana8
I mean, it isn't necessary so you shouldn't do it anyway but I still wonder why it crashed for you.
the follow code is crashed in my VS2019(C++17), but It's OK in: https://godbolt.org/z/dcTosana8
class Demo
{
public:
shared_ptr
void testcase()
{
Mock
crash info: 0x0000000000000000 Exception raised at TimelineUnitTest.exe: 0xC0000005: Access conflict occurred at execution location 0x0000000000000000.
I'll double check tomorrow on my machine - also with VS 2019.
I've also found I need to do this on VS 2019/v142. There may be a bug with it on VS
This may also be fixed by this I think. https://github.com/eranpeer/FakeIt/pull/289
Fixed by #289 in FakeIt 2.3.1.
My code: auto mock1 = new Mock();
When(Method(*mock1, resId)).AlwaysReturn("1");
auto meta = shared_ptr(&mock1->get());
error info: FATAL ERROR: test case CRASHED: SIGSEGV - Segmentation violation signal error in "method(c);" (line: 5541)