Repository: pyvista/pyvista

Issue:
Wrong result - Rename the "implicit_distance" after compute_implicit_distance filter
### Describe the bug, what's wrong, and what you expected.

After the _implicit distance_ of a dataset is computed, I tried to rename it using the method `rename_array`.  But I got a different data array result from the original one which means not only the name was changed.
I noticed the `rename_array` uses the `pop` method of` point_data` or `cell_data`. Then I did more research and found it seems there is a bug in the `pop` method.
I did a simple example (check the code in the next block) and got the same bug in my project. However, the strange thing is that in my data array can work well with the method `rename_array`, only the _implicit distance_ data array cannot work.

### Steps to reproduce the bug.

Here is a simple example
```python
import pyvista as pv

sphere = pv.Sphere(radius=5)
plane = pv.Plane()

new_sphere = sphere.compute_implicit_distance(plane)
new_sphere["pd"] = range(new_sphere.n_points)
new_sphere.plot(scalars="implicit_distance")

new_sphere.rename_array("implicit_distance", "im")
new_sphere.rename_array("pd", "pdc")
new_sphere.plot(scalars="im")
```
The bug is obvious when running the above example.

### System Information

```shell
Date: Tue Nov 28 19:39:51 2023

                OS : Windows
            CPU(s) : 16
           Machine : AMD64
      Architecture : 64bit
               RAM : 15.8 GiB
       Environment : Python
        GPU Vendor : Intel
      GPU Renderer : Intel(R) UHD Graphics
       GPU Version : 4.5.0 - Build 31.0.101.1999
  MathText Support : False

  Python 3.11.5 | packaged by conda-forge | (main, Aug 27 2023, 03:23:48) [MSC
  v.1936 64 bit (AMD64)]

           pyvista : 0.42.3
               vtk : 9.2.6
             numpy : 1.24.3
        matplotlib : 3.7.1
            scooby : 0.7.2
             pooch : v1.7.0
            pillow : 10.0.0
           imageio : 2.31.2
           IPython : 8.14.0
             scipy : 1.11.1
            meshio : 5.3.4
      nest_asyncio : 1.5.6

  Intel(R) oneAPI Math Kernel Library Version 2023.1-Product Build 20230303
  for Intel(R) 64 architecture applications
```


### Screenshots

Original Data
![original_data](https://github.com/pyvista/pyvista/assets/114913030/6c48a178-af58-458e-80d0-426fa2c11f58)
Renamed Data
![renamed_data](https://github.com/pyvista/pyvista/assets/114913030/a1fc76f4-af12-4534-83c2-d5c8722a78f4)


Discussion:
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/pyvista/pyvista/pull/5246?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyvista) Report
> Merging [#5246](https://app.codecov.io/gh/pyvista/pyvista/pull/5246?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyvista) (dc9edaa) into [main](https://app.codecov.io/gh/pyvista/pyvista/commit/71b33d105ab00f779ca224f41071512b0c4805e9?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyvista) (71b33d1) will **decrease** coverage by `7.12%`.
> The diff coverage is `100.00%`.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #5246      +/-   ##
==========================================
- Coverage   96.20%   89.08%   -7.12%     
==========================================
  Files         134      134              
  Lines       22634    22637       +3     
==========================================
- Hits        21774    20166    -1608     
- Misses        860     2471    +1611     
```

</details>

Patch:
diff --git a/pyvista/core/dataset.py b/pyvista/core/dataset.py
index 3021e386..cdd36783 100644
--- a/pyvista/core/dataset.py
+++ b/pyvista/core/dataset.py
@@ -771,13 +771,21 @@ class DataSet(DataSetFilters, DataObject):
         if self.active_scalars_name == old_name:
             was_active = True
         if field == FieldAssociation.POINT:
-            self.point_data[new_name] = self.point_data.pop(old_name)
+            data = self.point_data
         elif field == FieldAssociation.CELL:
-            self.cell_data[new_name] = self.cell_data.pop(old_name)
+            data = self.cell_data
         elif field == FieldAssociation.NONE:
-            self.field_data[new_name] = self.field_data.pop(old_name)
+            data = self.field_data
         else:
             raise KeyError(f'Array with name {old_name} not found.')
+
+        arr = data.pop(old_name)
+        # Update the array's name before reassigning. This prevents taking a copy of the array
+        # in `DataSetAttributes._prepare_array` which can lead to the array being garbage collected.
+        # See issue #5244.
+        arr.VTKObject.SetName(new_name)
+        data[new_name] = arr
+
         if was_active and field != FieldAssociation.NONE:
             self.set_active_scalars(new_name, preference=field)
         return None


Test patch:
diff --git a/tests/core/test_dataset.py b/tests/core/test_dataset.py
index 67422fe6..a9fc1469 100644
--- a/tests/core/test_dataset.py
+++ b/tests/core/test_dataset.py
@@ -649,31 +649,59 @@ def test_set_active_scalars_name(grid):
 def test_rename_array_point(grid):
     point_keys = list(grid.point_data.keys())
     old_name = point_keys[0]
+    orig_vals = grid[old_name].copy()
     new_name = 'point changed'
     grid.set_active_scalars(old_name, preference='point')
     grid.rename_array(old_name, new_name, preference='point')
     assert new_name in grid.point_data
     assert old_name not in grid.point_data
     assert new_name == grid.active_scalars_name
+    assert np.array_equal(orig_vals, grid[new_name])
 
 
 def test_rename_array_cell(grid):
     cell_keys = list(grid.cell_data.keys())
     old_name = cell_keys[0]
+    orig_vals = grid[old_name].copy()
     new_name = 'cell changed'
     grid.rename_array(old_name, new_name)
     assert new_name in grid.cell_data
     assert old_name not in grid.cell_data
+    assert np.array_equal(orig_vals, grid[new_name])
 
 
 def test_rename_array_field(grid):
     grid.field_data['fieldfoo'] = np.array([8, 6, 7])
     field_keys = list(grid.field_data.keys())
     old_name = field_keys[0]
+    orig_vals = grid[old_name].copy()
     new_name = 'cell changed'
     grid.rename_array(old_name, new_name)
     assert new_name in grid.field_data
     assert old_name not in grid.field_data
+    assert np.array_equal(orig_vals, grid[new_name])
+
+
+def test_rename_array_doesnt_delete():
+    # Regression test for issue #5244
+    def make_mesh():
+        m = pv.Sphere()
+        m.point_data['orig'] = np.ones(m.n_points)
+        return m
+
+    mesh = make_mesh()
+    was_deleted = [False]
+
+    def on_delete(*_):
+        # Would be easier to throw an exception here but even though the exception gets printed to stderr
+        # pytest reports the test passing. See #5246 .
+        was_deleted[0] = True
+
+    mesh.point_data['orig'].VTKObject.AddObserver('DeleteEvent', on_delete)
+    mesh.rename_array('orig', 'renamed')
+    assert not was_deleted[0]
+    mesh.point_data['renamed'].VTKObject.RemoveAllObservers()
+    assert (mesh.point_data['renamed'] == 1).all()
 
 
 def test_change_name_fail(grid):