@@ -11,7 +11,7 @@ namespace EmbreeSharp
11
11
public class EmbreeDevice : IDisposable
12
12
{
13
13
private GCHandle _gcHandle ;
14
- private readonly RTCDevice _device ;
14
+ private RTCDeviceHandle _device ;
15
15
private ErrorFunction ? _errorFunc ;
16
16
private MemoryMonitorFunction ? _memMonitor ;
17
17
private bool _disposedValue = false ;
@@ -24,7 +24,7 @@ public RTCDevice NativeDevice
24
24
{
25
25
ThrowUtility . ObjectDisposed ( ) ;
26
26
}
27
- return _device ;
27
+ return new RTCDevice ( ) { Ptr = _device . DangerousGetHandle ( ) } ;
28
28
}
29
29
}
30
30
public bool IsDisposed => _disposedValue ;
@@ -34,7 +34,8 @@ public EmbreeDevice()
34
34
_gcHandle = GCHandle . Alloc ( this ) ;
35
35
unsafe
36
36
{
37
- _device = EmbreeNative . rtcNewDevice ( null ) ;
37
+ var device = EmbreeNative . rtcNewDevice ( null ) ;
38
+ _device = new RTCDeviceHandle ( device ) ;
38
39
}
39
40
}
40
41
@@ -46,7 +47,8 @@ public unsafe EmbreeDevice(string config)
46
47
Encoding . UTF8 . GetBytes ( config , configBytes ) ;
47
48
fixed ( byte * ptr = configBytes )
48
49
{
49
- _device = EmbreeNative . rtcNewDevice ( ptr ) ;
50
+ var device = EmbreeNative . rtcNewDevice ( ptr ) ;
51
+ _device = new RTCDeviceHandle ( device ) ;
50
52
}
51
53
}
52
54
@@ -66,12 +68,13 @@ protected virtual void Dispose(bool disposing)
66
68
}
67
69
unsafe
68
70
{
69
- EmbreeNative . rtcSetDeviceErrorFunction ( _device , null , null ) ;
70
- EmbreeNative . rtcSetDeviceMemoryMonitorFunction ( _device , null , null ) ;
71
+ EmbreeNative . rtcSetDeviceErrorFunction ( NativeDevice , null , null ) ;
72
+ EmbreeNative . rtcSetDeviceMemoryMonitorFunction ( NativeDevice , null , null ) ;
71
73
}
72
74
_gcHandle . Free ( ) ;
73
75
_gcHandle = default ;
74
76
_device . Dispose ( ) ;
77
+ _device = null ! ;
75
78
_disposedValue = true ;
76
79
}
77
80
}
@@ -88,7 +91,7 @@ public long GetProperty(RTCDeviceProperty prop)
88
91
{
89
92
ThrowUtility . ObjectDisposed ( ) ;
90
93
}
91
- var result = EmbreeNative . rtcGetDeviceProperty ( _device , prop ) ;
94
+ var result = EmbreeNative . rtcGetDeviceProperty ( NativeDevice , prop ) ;
92
95
return result . ToInt64 ( ) ;
93
96
}
94
97
@@ -98,7 +101,7 @@ public void SetProperty(RTCDeviceProperty prop, long value)
98
101
{
99
102
ThrowUtility . ObjectDisposed ( ) ;
100
103
}
101
- EmbreeNative . rtcSetDeviceProperty ( _device , prop , new nint ( value ) ) ;
104
+ EmbreeNative . rtcSetDeviceProperty ( NativeDevice , prop , new nint ( value ) ) ;
102
105
}
103
106
104
107
public RTCError GetError ( )
@@ -107,7 +110,7 @@ public RTCError GetError()
107
110
{
108
111
ThrowUtility . ObjectDisposed ( ) ;
109
112
}
110
- return EmbreeNative . rtcGetDeviceError ( _device ) ;
113
+ return EmbreeNative . rtcGetDeviceError ( NativeDevice ) ;
111
114
}
112
115
113
116
private static unsafe void ErrorFunctionImpl ( void * userPtr , RTCError code , byte * str )
@@ -133,11 +136,11 @@ public unsafe void SetErrorFunction(ErrorFunction? func)
133
136
_errorFunc = func ;
134
137
if ( func == null )
135
138
{
136
- EmbreeNative . rtcSetDeviceErrorFunction ( _device , null , null ) ;
139
+ EmbreeNative . rtcSetDeviceErrorFunction ( NativeDevice , null , null ) ;
137
140
}
138
141
else
139
142
{
140
- EmbreeNative . rtcSetDeviceErrorFunction ( _device , ErrorFunctionImpl , GCHandle . ToIntPtr ( _gcHandle ) . ToPointer ( ) ) ;
143
+ EmbreeNative . rtcSetDeviceErrorFunction ( NativeDevice , ErrorFunctionImpl , GCHandle . ToIntPtr ( _gcHandle ) . ToPointer ( ) ) ;
141
144
}
142
145
}
143
146
@@ -157,11 +160,11 @@ public unsafe void SetMemoryMonitorFunction(MemoryMonitorFunction? func)
157
160
_memMonitor = func ;
158
161
if ( func == null )
159
162
{
160
- EmbreeNative . rtcSetDeviceMemoryMonitorFunction ( _device , null , null ) ;
163
+ EmbreeNative . rtcSetDeviceMemoryMonitorFunction ( NativeDevice , null , null ) ;
161
164
}
162
165
else
163
166
{
164
- EmbreeNative . rtcSetDeviceMemoryMonitorFunction ( _device , MemoryMonitorFunctionImpl , GCHandle . ToIntPtr ( _gcHandle ) . ToPointer ( ) ) ;
167
+ EmbreeNative . rtcSetDeviceMemoryMonitorFunction ( NativeDevice , MemoryMonitorFunctionImpl , GCHandle . ToIntPtr ( _gcHandle ) . ToPointer ( ) ) ;
165
168
}
166
169
}
167
170
}
0 commit comments