isl-org / isl-org/Open3D

Weird memory behaviour with KDTreeFlann and numpy array

Open
#4,934 3 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
14k
Forks
2.6k
Avg merge
5d 18h
Merged PRs (30d)
6

Description

### Checklist

- [X] I have searched for [similar issues](https://github.com/isl-org/Open3D/issues).
- [X] For Python issues, I have tested with the [latest development wheel](http://www.open3d.org/docs/latest/getting_started.html#development-version-pip).
- [X] I have checked the [release documentation](http://www.open3d.org/docs/release/) and the [latest documentation](http://www.open3d.org/docs/latest/) (for `master` branch).

### Describe the issue

If I create an empty numpy array arr1 with shape [2, N] and set its values, then create a kd tree using KDTreeFlann, then search for the nearest neighbour to a given point, I get the correct value. If I then create a completely different numpy array arr2 with similar shape e.g. [2, N] (it has also happened with shapes like [2, N-20] as long as they are relatively same size), then set the values of arr2 to anything (e.g. all 1s), then if I perform the exact same query on the unchanged arr1 and the unchanged kd tree for arr1 with the same point, I get a different, incorrect result. The only thing that changed was creating a separate empty numpy array arr2 and setting its values.

I assume this is some kind of weird memory thing where numpy.empty is maybe using memory that is being used by KDTreeFlann then when the values are changed within the empty arr2 it overwrites some data being used by KDTreeFlann?

Some debugging info:

When I originally encountered this with an array of shape [2, 107] this happened every time I ran my code, but when I created the simplified example with array shape [2, 4] it only happens like 20% of the times that I run my code.

If I put the example into a loop that loops 500 times, I would expect the incorrect result about 100 times, but what actually happens is 80% of the time the program runs and it loops 500 times with NO incorrect results, and 20% of the time the program runs and loops 500 times with ONE incorrect result, on the very first loop. I have absolutely no idea what this behaviour indicates.

### Steps to reproduce the bug

```python
import numpy as np
import open3d as o3d

arr1 = np.empty([2, 4])
# I will set the coords below to be (1,1) (3,3) (8,8) (12,12)

arr1[0][0]=1
arr1[1][0]=1

arr1[0][1]=3
arr1[1][1]=3

arr1[0][2]=8
arr1[1][2]=8

arr1[0][3]=12
arr1[1][3]=12

arr1_tree = o3d.geometry.KDTreeFlann(arr1)

point = (4, 4)

[k, idx, _] = arr1_tree.search_knn_vector_xd(point, 1)

print(idx[0]) # index 1 as expected
print((arr1[0][idx[0]], arr1[1][idx[0]])) # coord at index 1 is (3, 3) which is the closest coord to point (4, 4)

arr2 = np.empty([2, 4]) # completely new array of similar/same size to arr1
for i in range(4): # setting the (apparently) unallocated memory numpy chose to 1s
arr2[0][i] = 1
arr2[1][i] = 1

# exact same query as previously
point = (4, 4)

[k, idx, _] = arr1_tree.search_knn_vector_xd(point, 1)

print(idx[0]) # I get index 0 (about 1 in every 5 times running this code)
print((arr1[0][idx[0]], arr1[1][idx[0]])) # coord at index 0 is (1, 1) which is definitely incorrect
```

### Error message

_No response_

### Expected behavior

I expect the same result when using the same query on the same data, in this case index 1 i.e. coord (3, 3)

### Open3D, Python and System information

```markdown
- Operating system: Windows 10 64-bit
- Python version: Python 3.7
- Open3D version: 0.15.1
- System architecture: x64
- Is this a remote workstation?: no
- How did you install Open3D?: pip
- Compiler version (if built from source):
```

### Additional information

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.