Skip to content

Commit 9b6d9ee

Browse files
committed
lifetime management
1 parent 8951870 commit 9b6d9ee

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/torchcodec/_core/ops.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,21 +178,22 @@ def encode_audio_to_file_like(
178178
if samples.dtype != torch.float32:
179179
raise ValueError(f"samples must have dtype torch.float32, got {samples.dtype}")
180180

181-
samples_contiguous = samples.contiguous()
182-
183-
data_ptr = samples_contiguous.data_ptr()
184-
shape = list(samples_contiguous.shape)
181+
samples = samples.contiguous()
185182

186183
_pybind_ops.encode_audio_to_file_like(
187-
data_ptr,
188-
shape,
184+
samples.data_ptr(),
185+
list(samples.shape),
189186
sample_rate,
190187
format,
191188
file_like,
192189
bit_rate,
193190
num_channels,
194191
)
195192

193+
# This check is useless but it's critical to keep it to ensures that samples
194+
# is still alive during the call to encode_audio_to_file_like.
195+
assert samples.is_contiguous()
196+
196197

197198
# ==============================
198199
# Abstract impl for the operators. Needed by torch.compile.

0 commit comments

Comments
 (0)