Skip to content

Conversation

@roflmuffin
Copy link
Owner

@roflmuffin roflmuffin commented Jul 17, 2025

Since Teleport is probably the biggest source of Vector based memory leaks, this adds a way to call the Teleport virtual method using System.Numerics.Vector3 while still maintaining backwards compatibility.

To increase the ergonomics of modifying existing vectors coming out of the schema system (like with AbsOrigin), I've added explicit cast operators to allow you to easily convert to a numerics Vector3. This means the following code can be substituted to remove memory leak:

Before:

// Leaks because 2021 Roflmuffin sucks and `new Vector` allocates memory in native side and never frees it
pawn.Teleport(new Vector(pawn.AbsOrigin.X, pawn.AbsOrigin.Y, pawn.AbsOrigin.Z + 100));

After:

// Doesn't leak because the Vector3 is a struct and the pointer passed directly to native and GCd/freed by C# after
pawn.Teleport((Vector3)pawn.AbsOrigin with { Z = pawn.AbsOrigin.Z + 100 });

Unfortunately to ultimately fix memory leaks with regards to the schema system, it will require breaking changes so this will need to be done in a breaking 2.0 release of CS# I imagine.

@roflmuffin
Copy link
Owner Author

@KillStr3aK

@KillStr3aK
Copy link
Contributor

@KillStr3aK

this change/idea may actually reduce the number of leaks, but developers would need to explicitly cast to the struct which doesn't help existing (but abandoned) plugins, also we would need a way to tell people to actually cast their params. I'm thinking about implicit casting that would be handled by the core, just people are already instantiating the vector classes when they call the Teleport function so not sure what would be good there, but we can discuss it pretty sure

I'm looking forward for CS# 2.0

@roflmuffin
Copy link
Owner Author

Yeah I thought about implicit casting, but yes it wouldn't really help anyone either because people would still be instantiating CS# Vector class.

The idea behind this isn't to fix old plugins, but allow an easier path forward for current Devs to write more performant code that would be more future proof.

@roflmuffin roflmuffin requested a review from Copilot July 20, 2025 10:59

This comment was marked as outdated.

@roflmuffin roflmuffin requested a review from Copilot July 20, 2025 11:07
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds explicit cast operators to convert Vector3 and QAngle types to System.Numerics.Vector3, and introduces a new Teleport method overload that accepts Vector3 parameters to reduce memory leaks. The changes aim to provide a more memory-efficient alternative to the existing Vector-based Teleport method while maintaining backwards compatibility.

Key changes:

  • Added explicit cast operators from Vector and QAngle to System.Numerics.Vector3
  • Introduced Vector3-based Teleport method overload with unsafe pointer operations
  • Updated exception message for consistency between Teleport overloads

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
Vector.cs Adds explicit cast operator to convert Vector to System.Numerics.Vector3
QAngle.cs Adds explicit cast operator to convert QAngle to System.Numerics.Vector3 and imports System.Numerics namespace
CBaseEntity.cs Adds new Teleport overload accepting Vector3 parameters and updates exception message

@roflmuffin roflmuffin enabled auto-merge (squash) July 20, 2025 11:12
@roflmuffin roflmuffin merged commit 9491732 into main Jul 20, 2025
5 checks passed
@roflmuffin roflmuffin deleted the feature/add-numerics-overload branch July 20, 2025 11:15
This was referenced Aug 4, 2025
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

Successfully merging this pull request may close these issues.

3 participants