Skip to content

[p5.js 2.0+ Bug Report]: remove() accumulates lifecycle hooks across instances #9072

Description

@slash-init

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • WebGPU
  • p5.strands
  • Build process
  • Unit testing
  • Internationalization
  • Friendly errors
  • Other (specify if possible)

p5.js version

v2.3.1

Web browser and version

Firefox 153.0.1

Operating system

Fedora Linux 44 (Workstation Edition)

Steps to reproduce this

  1. Create multiple p5 instances.
  2. Each instance registers an instance-specific remove lifecycle hook.
  3. Call remove() on the instances.
  4. Check the length of p5.lifecycleHooks.remove.

For example:

test('remove hooks accumulate after instances are removed', async function () {
  const before = p5.lifecycleHooks.remove.length;

  let instance1;
  let instance2;

  await Promise.all([
    new Promise(resolve => {
      new p5(p => {
        p.setup = () => {
          instance1 = p;
          resolve();
        };
      });
    }),
    new Promise(resolve => {
      new p5(p => {
        p.setup = () => {
          instance2 = p;
          resolve();
        };
      });
    })
  ]);

  assert.strictEqual(
    p5.lifecycleHooks.remove.length,
    before + 2
  );

  await instance1.remove();
  await instance2.remove();

  assert.strictEqual(
    p5.lifecycleHooks.remove.length,
    before + 2
  );
});

The remove hooks are added to the global p5.lifecycleHooks.remove array when instances are created, but calling remove() does not remove those hook functions from the array. As more p5 instances are created and removed, the array therefore keeps growing.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions