Skip to content

Commit 1252ea8

Browse files
committed
Fixed crash due to free tensor too early.
1 parent fae5a2d commit 1252ea8

2 files changed

Lines changed: 29 additions & 96 deletions

File tree

‎src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs‎

Lines changed: 24 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -561,122 +561,50 @@ private unsafe IntPtr AllocateWithMemoryCopy(NDArray nd, TF_DataType? tensorDTyp
561561
return tfHandle;
562562
}
563563

564+
private GCHandle gcHandle;
565+
private IntPtr NewTensor<T>(T[] array, TF_DataType dataType, long[] dims, int len, int element_size)
566+
{
567+
gcHandle = GCHandle.Alloc(array, GCHandleType.Pinned);
568+
IntPtr p = gcHandle.AddrOfPinnedObject();
569+
return c_api.TF_NewTensor(dataType,
570+
dims,
571+
dims.Length,
572+
p,
573+
(UIntPtr)(len * element_size),
574+
_nothingDeallocator,
575+
ref _deallocatorArgs);
576+
// free gcHandle at DisposeManagedState(), free here causes random crash
577+
// TF_NewTensor already copied data into native, still don't know why we can't free immediately.
578+
}
579+
564580
private unsafe IntPtr Allocate(NDArray nd, TF_DataType? tensorDType = null)
565581
{
566-
IntPtr dotHandle = IntPtr.Zero;
567582
IntPtr tfHandle = IntPtr.Zero;
568-
int buffersize = nd.size * nd.dtypesize;
569-
570583
var dataType = ToTFDataType(nd.dtype);
571584
// shape
572585
var dims = nd.shape.Select(x => (long)x).ToArray();
573586
switch (nd.dtype.Name)
574587
{
575588
case "Boolean":
576-
{
577-
var boolVals = Array.ConvertAll(nd.Data<bool>(), x => Convert.ToByte(x));
578-
var array = nd.Data<byte>();
579-
fixed (byte* h = &array[0])
580-
{
581-
tfHandle = c_api.TF_NewTensor(dataType,
582-
dims,
583-
dims.Length,
584-
new IntPtr(h),
585-
(UIntPtr)buffersize,
586-
_nothingDeallocator,
587-
ref _deallocatorArgs);
588-
}
589-
}
589+
tfHandle = NewTensor(nd.Data<bool>(), dataType, dims, nd.size, nd.dtypesize);
590590
break;
591591
case "Int16":
592-
{
593-
var array = nd.Data<short>();
594-
fixed (short* h = &array[0])
595-
{
596-
tfHandle = c_api.TF_NewTensor(dataType,
597-
dims,
598-
dims.Length,
599-
new IntPtr(h),
600-
(UIntPtr)buffersize,
601-
_nothingDeallocator,
602-
ref _deallocatorArgs);
603-
}
604-
}
592+
tfHandle = NewTensor(nd.Data<short>(), dataType, dims, nd.size, nd.dtypesize);
605593
break;
606594
case "Int32":
607-
{
608-
var array = nd.Data<int>();
609-
fixed (int* h = &array[0])
610-
{
611-
tfHandle = c_api.TF_NewTensor(dataType,
612-
dims,
613-
dims.Length,
614-
new IntPtr(h),
615-
(UIntPtr)buffersize,
616-
_nothingDeallocator,
617-
ref _deallocatorArgs);
618-
}
619-
}
595+
tfHandle = NewTensor(nd.Data<int>(), dataType, dims, nd.size, nd.dtypesize);
620596
break;
621597
case "Int64":
622-
{
623-
var array = nd.Data<long>();
624-
fixed (long* h = &array[0])
625-
{
626-
tfHandle = c_api.TF_NewTensor(dataType,
627-
dims,
628-
dims.Length,
629-
new IntPtr(h),
630-
(UIntPtr)buffersize,
631-
_nothingDeallocator,
632-
ref _deallocatorArgs);
633-
}
634-
}
598+
tfHandle = NewTensor(nd.Data<long>(), dataType, dims, nd.size, nd.dtypesize);
635599
break;
636600
case "Single":
637-
{
638-
var array = nd.Data<float>();
639-
fixed (float* h = &array[0])
640-
{
641-
tfHandle = c_api.TF_NewTensor(dataType,
642-
dims,
643-
dims.Length,
644-
new IntPtr(h),
645-
(UIntPtr)buffersize,
646-
_nothingDeallocator,
647-
ref _deallocatorArgs);
648-
}
649-
}
601+
tfHandle = NewTensor(nd.Data<float>(), dataType, dims, nd.size, nd.dtypesize);
650602
break;
651603
case "Double":
652-
{
653-
var array = nd.Data<double>();
654-
fixed (double* h = &array[0])
655-
{
656-
tfHandle = c_api.TF_NewTensor(dataType,
657-
dims,
658-
dims.Length,
659-
new IntPtr(h),
660-
(UIntPtr)buffersize,
661-
_nothingDeallocator,
662-
ref _deallocatorArgs);
663-
}
664-
}
604+
tfHandle = NewTensor(nd.Data<double>(), dataType, dims, nd.size, nd.dtypesize);
665605
break;
666606
case "Byte":
667-
{
668-
var array = nd.Data<byte>();
669-
fixed (byte* h = &array[0])
670-
{
671-
tfHandle = c_api.TF_NewTensor(dataType,
672-
dims,
673-
dims.Length,
674-
new IntPtr(h),
675-
(UIntPtr)buffersize,
676-
_nothingDeallocator,
677-
ref _deallocatorArgs);
678-
}
679-
}
607+
tfHandle = NewTensor(nd.Data<byte>(), dataType, dims, nd.size, nd.dtypesize);
680608
break;
681609
case "String":
682610
return new Tensor(UTF8Encoding.UTF8.GetBytes(nd.Data<string>(0)), TF_DataType.TF_STRING);
@@ -808,7 +736,7 @@ internal static void FreeGCHandle(IntPtr dataPtr, IntPtr len, ref DeallocatorArg
808736
if (args.deallocator_called || args.gc_handle == IntPtr.Zero)
809737
return;
810738
// note: since the ptr given to tensorflow is just the addr of the pinned object we can not directly free it! we need to free the gcHandle instead
811-
GCHandle.FromIntPtr(args.gc_handle).Free();
739+
//args.gchandle.Free();
812740
args.deallocator_called = true;
813741
}
814742

‎src/TensorFlowNET.Core/Tensors/Tensor.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,11 @@ public override string ToString()
392392
return $"tf.Tensor '{name}' shape=({string.Join(",", shape)}) dtype={dtype}";
393393
}
394394

395+
protected override void DisposeManagedState()
396+
{
397+
if (gcHandle.IsAllocated)
398+
gcHandle.Free();
399+
}
395400
protected override void DisposeUnManagedState(IntPtr handle)
396401
{
397402
if(handle != IntPtr.Zero)

0 commit comments

Comments
 (0)