Skip to content

Commit b3b692a

Browse files
ezyangfacebook-github-bot
authored andcommitted
Don't have malloc-free pairs that cross DLL boundaries. (pytorch#17302)
Summary: See https://blogs.msdn.microsoft.com/oldnewthing/20060915-04/?p=29723 for more background on this requirement on Windows. Fixes pytorch#17239. Signed-off-by: Edward Z. Yang <ezyang@fb.com> cc xkszltl peterjc123 Pull Request resolved: pytorch#17302 Differential Revision: D14150067 Pulled By: ezyang fbshipit-source-id: 9dc16ca781ff17515b8df1bb55492477e7843d4c
1 parent c063a33 commit b3b692a

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

aten/src/TH/THGeneral.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ void* THRealloc(void *ptr, ptrdiff_t size)
196196

197197
void THFree(void *ptr)
198198
{
199-
free(ptr);
199+
c10::free_cpu(ptr);
200200
}
201201

202202
double THLog10(const double x)

c10/core/CPUAllocator.cpp

+11-13
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ void* alloc_cpu(size_t nbytes) {
7676
return data;
7777
}
7878

79+
void free_cpu(void* data) {
80+
#ifdef _MSC_VER
81+
_aligned_free(data);
82+
#else
83+
free(data);
84+
#endif
85+
}
86+
7987
// A virtual struct that is used to report C10's memory allocation and
8088
// deallocation status
8189
class C10_API MemoryAllocationReporter {
@@ -99,32 +107,22 @@ struct C10_API DefaultCPUAllocator final : at::Allocator {
99107
getMemoryAllocationReporter().New(data, nbytes);
100108
return {data, data, &ReportAndDelete, at::Device(at::DeviceType::CPU)};
101109
}
102-
return {data, data, &Delete, at::Device(at::DeviceType::CPU)};
110+
return {data, data, &free_cpu, at::Device(at::DeviceType::CPU)};
103111
}
104112

105-
#ifdef _MSC_VER
106-
static void Delete(void* data) {
107-
_aligned_free(data);
108-
}
109-
#else
110-
static void Delete(void* data) {
111-
free(data);
112-
}
113-
#endif
114-
115113
static void ReportAndDelete(void* ptr) {
116114
if (!ptr) {
117115
return;
118116
}
119117
getMemoryAllocationReporter().Delete(ptr);
120-
Delete(ptr);
118+
free_cpu(ptr);
121119
}
122120

123121
at::DeleterFnPtr raw_deleter() const override {
124122
if (FLAGS_caffe2_report_cpu_memory_usage) {
125123
return &ReportAndDelete;
126124
}
127-
return &Delete;
125+
return &free_cpu;
128126
}
129127

130128
protected:

c10/core/CPUAllocator.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ C10_API void NoDelete(void*);
2828
C10_API void memset_junk(void* data, size_t num);
2929

3030
C10_API void* alloc_cpu(size_t nbytes);
31+
C10_API void free_cpu(void* data);
3132

3233
// Get the CPU Alloctor.
3334
C10_API at::Allocator* GetCPUAllocator();

0 commit comments

Comments
 (0)