Skip to content

Removed static variables#366

Open
najlkin wants to merge 3 commits intomasterfrom
refactor-static-vars
Open

Removed static variables#366
najlkin wants to merge 3 commits intomasterfrom
refactor-static-vars

Conversation

@najlkin
Copy link
Contributor

@najlkin najlkin commented Mar 6, 2026

Refactored code to remove static variables from VisualizationScenes, which are fine in the native app when declared as thread_local (not always the case), but fail in glvis-js, where a new instance of visualization scene is started (in the same window and thread). Initialization does not happen again and the values are carried over.
Bonus: Changed key handlers and variables to private.

@najlkin najlkin self-assigned this Mar 6, 2026
@najlkin najlkin added the bug label Mar 6, 2026
Copy link
Contributor

@camierjs camierjs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @najlkin, neat!

Copy link

@ragnar-howler ragnar-howler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review: Removed static variables

Summary

This PR addresses an important architectural issue where static variables with thread_local work correctly in the native app but fail in glvis-js, where new visualization scene instances are started in the same window/thread without reinitialization.

Changes Analysis

1. Static Member Migration (Good)

  • Moving vsdata, vssol, vssol3d, vsvector, vsvector3d from global statics to static thread_local class members is proper encapsulation
  • Thread safety is preserved - thread_local ensures per-thread isolation

2. Key Handler Refactoring (Good)

  • Converting free functions to static member functions improves code organization
  • Making them private hides implementation details from public API
  • All key handlers properly access scene instance via the static thread_local pointer

3. Instance Variable Fixes (Critical Fix)

Moving these from static globals to instance variables correctly fixes state carryover:

  • magic_key_pressed (vssolution3d)
  • ianim, ianimmax, key_u_func (vsvector)
  • arrows_nl, vector_h, vector_hh (vsvector3d - replaces static locals)

This ensures each scene instance maintains its own state.

4. Plane Constructor Change

// Before:
Plane(double A, double B, double C, double D);
// After:
Plane(const double (&eqn_)[4], const VisualizationScene::Box &bb);
  • Passing bounding box by reference avoids accessing global state
  • Array reference for equation parameters is cleaner
  • The named struct Box in openglvis.hpp enables this

5. Logic Corrections

  • vsvector.cpp: ChangeDisplacement() correctly handles wraparound with modulo arithmetic
  • vsvector3d.cpp: ArrowsDrawOrNot() now uses arrows_nl member (initialized to -1) instead of first_time static
  • KeyrPressed/KeyRPressed now correctly use vsvector3d-> instead of window->vs->

Verdict

Approve

This is a well-executed refactoring that:

  • Fixes the glvis-js state carryover bug
  • Improves encapsulation
  • Maintains thread safety
  • Preserves all existing functionality

The changes are minimal and focused on the specific issue without unnecessary scope creep.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants