File tree Expand file tree Collapse file tree 4 files changed +19
-17
lines changed
Expand file tree Collapse file tree 4 files changed +19
-17
lines changed Original file line number Diff line number Diff line change @@ -277,14 +277,19 @@ struct C10_EXPORT ivalue::Future final : c10::intrusive_ptr_target {
277277// User-defined object.
278278struct 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
342348std::vector<std::pair<IValue, IValue>> iterationOrder (const c10::DictPtr<IValue, IValue>& dict);
Original file line number Diff line number Diff line change 55#include < c10/macros/Macros.h>
66namespace c10 {
77
8- #ifdef C10_ANDROID
98namespace ivalue {
10- Object::~Object () {}
9+ Object::~Object () {
10+ if (on_delete_) {
11+ on_delete_ (this );
12+ }
13+ }
1114} // namespace ivalue
12- #endif
1315
1416std::ostream& operator <<(std::ostream & out, const Type & t) {
1517 if (auto value = t.cast <CompleteTensorType>()) {
Original file line number Diff line number Diff 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-
2010std::shared_ptr<Function> ClassType::getMethod (const std::string& name) const {
2111 return compilation_unit_->find_function (name);
2212}
Original file line number Diff line number Diff 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
108111struct 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 () {}
You can’t perform that action at this time.
0 commit comments