Is it time to release the BufferGeometry?

My scene objects are structured around a single root Object3D, with data loaded as a tree of Object3Ds branching from this root. Meshes are attached to the leaf Object3Ds using BufferGeometry/MeshPhongMaterial. To clear the existing tree structure, I use the following method:

clearScene:
    function (obj) {
        if (obj instanceof THREE.Mesh)
        {
            obj.geometry.dispose();
            obj.geometry = undefined;
            obj.material.dispose();
            obj.material = undefined;
            obj = undefined;
        }
        else
        {
            if (obj.children !== undefined) {
                while (obj.children.length > 0) {
                    this.clearScene(obj.children[0]); // removing children changes the length of the array.
                    obj.remove(obj.children[0]);
                }
            }
        }
    }

Let's consider a simple tree structure:

  • Scene (Scene)
    • Root (Object3D)
      • Branch (Object3D)
        • Leaf (Mesh)

After adding this structure to the scene, observation of the heap in Chrome's dev tools reveals 3 Object3Ds and 2 Mesh objects (apart from prototypes).

Upon calling clearScene(Root), the traversal through the tree removes Object3Ds and cleans up meshes. However, inspection of the heap shows that although the Object3Ds have been removed, the 2 Mesh objects (along with their associated BufferGeometry and Material objects) persist. Reloading the data after clearing results in 3 Object3Ds (as expected), but 4 Meshes (not intended).

This indicates a lingering reference issue, despite no retainers being found in the heap.

It seems like there is something else at play causing these objects to remain active.

r69dev (I was seeing the same in r68), testing in Chrome 36.0.1985.125

Answer №1

Reported concern on github: https://github.com/mrdoob/three.js/issues/5175

In version r69dev, it is necessary to explicitly call the dispose method for meshes in order to properly remove references held by the renderer.

Here is a sample of working code:

clearScene:
function (obj) {
    if (obj instanceof THREE.Mesh)
    {
        obj.geometry.dispose();
        obj.geometry = null;
        obj.material.dispose();
        obj.material = null;
        obj.dispose(); // required in r69dev to remove references from the renderer.
        obj = null;
    }
    else
    {
        if (obj.children !== undefined) {
            while (obj.children.length > 0) {
                this.clearScene(obj.children[0]);
                obj.remove(obj.children[0]);
            }
        }
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Middleware that handles form post requests quickly became overwhelmed and resulted in a RangeError

Currently, I am working with a form using ejs templates to collect data. When accessing the "/" route, my application renders the "main" view and utilizes a middleware to handle the form data. However, I encountered an error stating "RangeError: Maximum ca ...

Error in React Material UI: 'theme' variable is not defined - no-undef

In the process of developing a basic React application with material-ui, I am incorporating MuiThemeProvider and ThemeProvider for themes. App.js import React from 'react'; import { createMuiTheme, MuiThemeProvider } from '@material-ui/co ...

Tips for incorporating flow and TypeScript typings into an NPM module

Are there any resources available for adding both flow and typescript typings to an NPM module at the same time? I've been struggling to find a comprehensive guide on this topic, and it seems to be a common issue faced by open source library maintain ...

What is the best way to display an image and text side by side within a single row in react-table?

I am trying to display an image and text side by side within a single column in a React table. I have successfully added two texts together in a single column using the code below: Header: "Name", id: "User", accessor: (d) => ...

Tips on reducing the number of "$routeProvider.when" statements in a complex application

Is there a more efficient way to handle routing in AngularJS, specifically for loading html templates based on $location.path? My current approach involves a long list of "Whens" that will become unmanageable as my application grows. .config(['$rout ...

Error in React Native - Invalid component type provided

I'm encountering an issue while building a React Native app with Expo CLI. Every time I test it, I receive an error message. How can I troubleshoot and resolve this problem? Error: Element type is invalid: expected a string (for built-in components) ...

What is the process for modifying the headers of a post request in Express when

I have a Node/Express application and I'm looking to incorporate an image upload feature into an order form. While I can successfully use the action link in the HTML form to perform a POST request, I also want my JavaScript file associated with this ...

Retrieving information from embedded JavaScript code

The web page I am scraping contains inline JavaScript that dynamically generates telephone numbers inside a script tag. These numbers are not visible in the page source, making it challenging to scrape using common methods like x-path and beautiful soup. U ...

The loading of charts and animations is sluggish on mobile devices

My Chart.js chart starts with 0 values and updates upon clicking submit to load data from an external database. While this works efficiently on a computer browser, the load time is significantly longer when accessing the page on a mobile device. It takes a ...

Create a function that binds a select dropdown to each table column header using JavaScript Object Notation (JSON), and then populate an HTML table with the

I have a dynamic table populated with JSON data, and I would like to add a select dropdown for each column. The challenge is that the number of columns is not fixed and they are also populated by JSON. Therefore, I want the select dropdown at the top of ea ...

Unable to modify the appearance of text on the canvas

Trying to customize the font style of canvas text with Press Start 2P The URL has been imported into a CSS file as follows: @import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap'); .canvasClass{ font-family: ...

Automatic Textbox Closer

Connected to this AngularJS issue on IE10+ where textarea with placeholder causes "Invalid argument." and also this AngularJS 1.1.5 problem in Internet Explorer with ng-show and objects (Bug), we had to resort to using the textarea element as a self-closin ...

How to utilize a PHP array within a Vue.js template

I have been exploring the realms of Laravel and vue.js recently and have encountered a challenge. Within my Laravel model, I have a PHP method that retrieves data from a database and organizes it into objects stored in an array. Now, my goal is to access t ...

Choose and Send pictures through a Form with JavaScript

Currently, I am exploring different methods for allowing users to submit a form with the images they have selected. My idea is to present users with a set of images from which they can choose multiple options. Potential Solution 1: One approach could invo ...

Blogger Extension - Cross Domain Communication

As I work on creating a blogger plugin, my goal is to send information from the blog page for analytic purposes and then display the results on the visitor's page. Initially, I tried sending the page content using an ajax call but encountered an error ...

Concealing a Vuejs dropdown when clicking outside of the component

I am currently working on a Vuejs project where I am creating a menu component. This menu consists of 2 dropdowns, and I have already implemented some methods and used Vue directives to ensure that when one dropdown is clicked, the other hides and vice ver ...

What is the process of intersecting a 3D object in THREE.js when provided with a coordinate pair (X, Y)?

Is it possible to determine if points within a scene with (X, Y) coordinates intersect a specific object using the RayCaster in Three.js? Additionally, how can we find out the updated (X, Y, Z) coordinates after the intersection has occurred? ...

Can the ngx-chips library be used to alter the language of chips?

Currently, I am working with the ngx-chips library and encountering a particular issue. Here is an image representation of the problem: https://i.sstatic.net/GL3Fd.png The challenge I am facing involves updating the language of the chips based on the sele ...

What is the best way to transfer data between controllers in my particular situation?

Currently, I am working on developing a factory for the restful services. The main challenge I'm facing is how to transfer data from one controller to another in AngularJS. Specifically, I need to use the data obtained from the first service call to ...

Getting a JWT token from Express to Angular using ngResource: A step-by-step guide

Currently, I am utilizing a jwt token for user registration validation. A unique URL is generated and sent to the user via email, which leads them to the authentication page. On the server side, the token is decoded and I need to transmit this JSON data to ...