Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7550201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:09:09+00:00 2026-05-30T10:09:09+00:00

When using Direct3D in c++ I can write a Cube class for example, that

  • 0

When using Direct3D in c++ I can write a “Cube” class for example, that contains a “ID3D11Buffer* vertexBuffer_” and ensure that the destructor for that Cube object calls vertexBuffer_->Release().

I can have a “Scene” class containing a “unique_ptr cube_” object. So that I know that when I delete my scene, the cube will be deleted, and that will consequently call release on the D3D resources it is using.

In D I can’t do this. I can write destructors but I have no idea when they will be called. If the GC doesn’t require the memory they may never be called…

So what is the best way to handle this kind of thing in D? I could add a “Free” member function to each of the objects that frees all of it’s own resources and calls “Free” on any objects it owns, but this seems an error prone manual operation and a step backwards from C++.

Is there a better way to handle this kind of thing in D?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:09:10+00:00Added an answer on May 30, 2026 at 10:09 am

    You could use a struct on the stack. That has deterministic destruction. You can even have it be refcounted by using std.typecons.RefCounted. Don’t use a struct on the heap though if you want to guarantee that the destructor runs. At the moment, I don’t think that structs’ destructors ever get run if they’re put on the heap, because the GC doesn’t have the information that it needs to do so (that should be fixed at some point in the future though).

    But if you insist on putting it in on the heap in a class, and you want to explicitly destroy the object, then you can call clear on it:

    clear(obj);
    

    That will call the object’s destructor and then put it in an invalid state, and anything that tries to use it after that should blow up (IIRC, the virtual table gets zeroed out). But the memory isn’t actually freed. That’s the GC’s job. And don’t use delete. It’s going to be deprecated. I’m actually surprised that it hasn’t been yet, since it’s been planned for ages to get rid of it.

    And of course, one option is to have an explicit function that you call to release the resources. Whether that’s a good idea or not depends on what you’re doing. But regardless, classes are intended to be collected by the GC and not be freed whenever you choose to.

    Work is being done on custom allocators, which would give you more options for how to allocate a class, and one of those would probably allow you to have more deterministic destruction of classes, but that’s not ready yet.

    And if you’re feeling crazy, you can use std.typecons.scoped, which replaces the soon to be deprecated type modifier scope (though scope is sticking around in other contexts – such as scope statements). It puts a class on the stack. But that’s unsafe (which is why scope is going away in this context), and you probably might as well just use a struct if you’re going to stick the object on the stack.

    EDIT: You could also use malloc and free with std.conv.emplace to put the object in a non-GC allocated chunk of memory like you’d have in C++, but I think that you’d have to explicitly call the destructor to get it run, since free doesn’t understand about destructors (it being a C function). That would have the advantage of making the memory go away along with the resource (whereas using clear on an object on the GC heap would just destroy the object’s contents, not free the memory), but I don’t know that that buys you much over using clear on a GC-allocated object.

    However, you could then create a free function similar to new which does the malloc and emplace for you, and then have a free function similar to delete which calls the destructor and free, which would give you the same situation as C++. In fact, I wonder if that would be useful enough to make it into the standard library. That’s probably the sort of thing that’ll end up in the custom allocators though. So, it wouldn’t surprise me at all if in the relatively near future, you could use a custom allocator to do something like

    auto obj = customAllocObj.create!MyObj(args);
    //Do stuff...
    customAllocObj.destroy(obj);
    

    And I would think that that would solve your problem fairly well given that that’s essentially the same thing that you’d have in C++, just with library functions rather than with the built-in new and delete. I think that I’ll bring it up on the newsgroup. I expect that there are at least some folks who would like such a feature, and that does seem to fit in nicely with custom allocators.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using direct3d 9 and using D3DXCreateFont to create a font object that I
In my Direct3D application, the camera can be moved using the mouse or arrow
Using PyObjC , you can use Python to write Cocoa applications for OS X.
I've looked, and I can't find any material relating to using Direct3d 10 or
How can we detect if a directed graph is cyclic? I thought using breadth
I am using Direct3D to display a number of I-sections used in steel construction.
I am trying to render a model in Direct3D using DrawIndexedPrimitives . However, I
When using wireframe fill mode in Direct3D, all rectangular faces display a diagonal running
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using C# .NET 3.5 and WCF, I'm trying to write out some of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.