Skip to content

Commit f6aac41

Browse files
zdevitofacebook-github-bot
authored andcommitted
Defining object destructor in c10 (#21984)
Summary: Pull Request resolved: #21984 ghimport-source-id: 5767592e37ed388422eed5639f8ba0722aec66e2 Test Plan: Imported from OSS Differential Revision: D15906530 Pulled By: zdevito fbshipit-source-id: bec8c8b0b5b9dcc2e8fc69b5031fcfa6bb22d54e
1 parent 24a6c32 commit f6aac41

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

aten/src/ATen/core/ivalue_inl.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,19 @@ struct C10_EXPORT ivalue::Future final : c10::intrusive_ptr_target {
277277
// User-defined object.
278278
struct C10_EXPORT ivalue::Object final : c10::intrusive_ptr_target {
279279
public:
280-
Object(std::shared_ptr<ClassType> type, size_t numSlots) : type_(std::move(type)) {
280+
// temporary way to break cyclic dependencies in modules by forcing the deletion
281+
// of functions when the module object is destructed
282+
typedef void (*OnDelete)(ivalue::Object*);
283+
Object(std::shared_ptr<ClassType> type, size_t numSlots, OnDelete on_delete)
284+
: type_(std::move(type)), on_delete_(on_delete) {
281285
slots_.resize(numSlots);
282286
}
283287

284288
static c10::intrusive_ptr<Object> create(
285289
std::shared_ptr<ClassType> type,
286-
size_t numSlots) {
287-
return c10::make_intrusive<Object>(std::move(type), numSlots);
290+
size_t numSlots,
291+
OnDelete on_delete = nullptr) {
292+
return c10::make_intrusive<Object>(std::move(type), numSlots, on_delete);
288293
}
289294

290295
/**
@@ -337,6 +342,7 @@ struct C10_EXPORT ivalue::Object final : c10::intrusive_ptr_target {
337342
void resizeObject(size_t slot);
338343
std::shared_ptr<ClassType> type_;
339344
std::vector<IValue> slots_;
345+
OnDelete on_delete_;
340346
};
341347

342348
std::vector<std::pair<IValue, IValue>> iterationOrder(const c10::DictPtr<IValue, IValue>& dict);

aten/src/ATen/core/type.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
#include <c10/macros/Macros.h>
66
namespace c10 {
77

8-
#ifdef C10_ANDROID
98
namespace ivalue {
10-
Object::~Object() {}
9+
Object::~Object() {
10+
if (on_delete_) {
11+
on_delete_(this);
12+
}
13+
}
1114
} // namespace ivalue
12-
#endif
1315

1416
std::ostream& operator<<(std::ostream & out, const Type & t) {
1517
if(auto value = t.cast<CompleteTensorType>()) {

torch/csrc/jit/script/class_type.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@ namespace c10 {
77
// This file exists because we need to reference module.h, which we can't from
88
// c10. Sigh...
99

10-
#ifndef C10_ANDROID
11-
namespace ivalue {
12-
Object::~Object() {
13-
if (type_->is_module()) {
14-
type_->compilation_unit()->drop_all_functions();
15-
}
16-
}
17-
} // namespace ivalue
18-
#endif
19-
2010
std::shared_ptr<Function> ClassType::getMethod(const std::string& name) const {
2111
return compilation_unit_->find_function(name);
2212
}

torch/csrc/jit/script/module.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ struct TORCH_API Method {
104104
// first-class function in class_compilation_unit()
105105
std::shared_ptr<Function> function_;
106106
};
107+
static void clearMethods(c10::ivalue::Object* self) {
108+
self->type()->compilation_unit()->drop_all_functions();
109+
}
107110

108111
struct TORCH_API Module {
109112
Module(std::string class_name)
@@ -112,7 +115,8 @@ struct TORCH_API Module {
112115
QualifiedName(std::move(class_name)),
113116
std::make_shared<CompilationUnit>(),
114117
/*is_module=*/true),
115-
0)) {}
118+
0,
119+
clearMethods)) {}
116120
Module() : Module("$Module") {}
117121
Module(ModulePtr module_value) : module_value_(std::move(module_value)) {}
118122
~Module() {}

0 commit comments

Comments
 (0)