Skip to content
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

destructor and constructor not work #10

Closed
wangzhankun opened this issue Apr 22, 2022 · 6 comments
Closed

destructor and constructor not work #10

wangzhankun opened this issue Apr 22, 2022 · 6 comments

Comments

@wangzhankun
Copy link

As the title described, the destructor and constructor function do not work. You can write a print in the destructor and constructor function to test.

@wangzhankun
Copy link
Author

I‘m saying that although I use the new/delete operator, the constructor/destructor also do not work.

@jxy-s
Copy link
Owner

jxy-s commented Apr 22, 2022

I'm going to need more information than this to assist.

@wangzhankun
Copy link
Author

It's my fault. I test the new/delete again. I find only the delete operator not work.
image
When new an object, the constructor is invoked. But the delete will cause blue screen. I don't know how to use the delete operator and I find no example in your project.

@wangzhankun
Copy link
Author

I write a test in your project. But also delete causes blue screen.
image

@jxy-s
Copy link
Owner

jxy-s commented Apr 22, 2022

Ah, sorry that isn't clear. vcrtl implements a delete operator which catches this and bugchecks the system. This is an evil necessity unfortunately, and there is no way to explicitly call the overridden delete with a pool/tag and achieve the proper destruction of the object. I would prefer for that method of deletion (delete a;) not exist to avoid confusion and force explicit pool/tags. I should also apologize for leaving artifacts in alloc.hpp/cpp that are misleading.

Realistically, the safest way to approach allocations using the library is to use the tag/pool allocators objects or a container to achieve the same goal.

You can achieve the same result with a unique smart pointer:

auto a = jxy::make_unique<A, NonPagedPool, '0GAT'>();

This also gives you the convenience of RAII cleaning up the resource when it leaves scope.

Further, if you wish to detach the memory from the smart pointer then re-attach it later, these are the mechanics:

auto rawPointer = a.release();

// some place/time later...

jxy::unique_ptr<A, NonPagedPool, '0GAT'> resource;
resource.reset(rawPointer);

Finally, I'll leave this here if you must choose to use new - but again I advise use of smart pointers instead. This is the way to achieve what you want using the library:

auto a = new (NonPagedPool, '0GAT') A();
jxy::default_delete<A, NonPagedPool, '0GAT'>()(a);

@wangzhankun
Copy link
Author

Thanks for your help

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