Skip to content

Commit

Permalink
Mono: Change BindingsGenerator singleton to avoid StringName leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
neikeq committed Jan 1, 2018
1 parent b271aa4 commit fe39139
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
8 changes: 8 additions & 0 deletions modules/mono/csharp_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ void CSharpLanguage::finish() {

finalizing = true;

#ifdef TOOLS_ENABLED
// Must be here, to avoid StringName leaks
if (BindingsGenerator::singleton) {
memdelete(BindingsGenerator::singleton);
BindingsGenerator::singleton = NULL;
}
#endif

// Release gchandle bindings before finalizing mono runtime
gchandle_bindings.clear();

Expand Down
11 changes: 6 additions & 5 deletions modules/mono/editor/bindings_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ const char *BindingsGenerator::TypeInterface::DEFAULT_VARARG_C_IN = "\t%0 %1_in

bool BindingsGenerator::verbose_output = false;

BindingsGenerator *BindingsGenerator::singleton = NULL;

static String snake_to_pascal_case(const String &p_identifier, bool p_input_is_upper = false) {

String ret;
Expand Down Expand Up @@ -2466,8 +2468,7 @@ void BindingsGenerator::_populate_global_constants() {
}
}

BindingsGenerator::BindingsGenerator() :
name_cache(NameCache::get_singleton()) {
void BindingsGenerator::initialize() {

EditorHelp::generate_doc();

Expand Down Expand Up @@ -2509,7 +2510,7 @@ void BindingsGenerator::handle_cmdline_args(const List<String> &p_cmdline_args)
const List<String>::Element *path_elem = elem->next();

if (path_elem) {
if (get_singleton().generate_glue(path_elem->get()) != OK)
if (get_singleton()->generate_glue(path_elem->get()) != OK)
ERR_PRINT("Mono glue generation failed");
elem = elem->next();
} else {
Expand All @@ -2523,7 +2524,7 @@ void BindingsGenerator::handle_cmdline_args(const List<String> &p_cmdline_args)
const List<String>::Element *path_elem = elem->next();

if (path_elem) {
if (get_singleton().generate_cs_core_project(path_elem->get()) != OK)
if (get_singleton()->generate_cs_core_project(path_elem->get()) != OK)
ERR_PRINT("Generation of solution and C# project for the Core API failed");
elem = elem->next();
} else {
Expand All @@ -2538,7 +2539,7 @@ void BindingsGenerator::handle_cmdline_args(const List<String> &p_cmdline_args)

if (path_elem) {
if (path_elem->next()) {
if (get_singleton().generate_cs_editor_project(path_elem->get(), path_elem->next()->get()) != OK)
if (get_singleton()->generate_cs_editor_project(path_elem->get(), path_elem->next()->get()) != OK)
ERR_PRINT("Generation of solution and C# project for the Editor API failed");
elem = path_elem->next();
} else {
Expand Down
23 changes: 13 additions & 10 deletions modules/mono/editor/bindings_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class BindingsGenerator {
}

MethodInterface() {
return_type = NameCache::get_singleton().type_void;
return_type = BindingsGenerator::get_singleton()->name_cache.type_void;
is_vararg = false;
is_virtual = false;
requires_object_call = false;
Expand Down Expand Up @@ -469,16 +469,11 @@ class BindingsGenerator {
enum_Error = StaticCString::create("Error");
}

static NameCache &get_singleton() {
static NameCache singleton;
return singleton;
}

NameCache(const NameCache &);
NameCache &operator=(const NameCache &);
};

const NameCache &name_cache;
NameCache name_cache;

const List<InternalCall>::Element *find_icall_by_name(const String &p_name, const List<InternalCall> &p_list) {
const List<InternalCall>::Element *it = p_list.front();
Expand Down Expand Up @@ -525,18 +520,26 @@ class BindingsGenerator {

Error _save_file(const String &path, const List<String> &content);

BindingsGenerator();
BindingsGenerator() {}

BindingsGenerator(const BindingsGenerator &);
BindingsGenerator &operator=(const BindingsGenerator &);

friend class CSharpLanguage;
static BindingsGenerator *singleton;

public:
Error generate_cs_core_project(const String &p_output_dir, bool p_verbose_output = true);
Error generate_cs_editor_project(const String &p_output_dir, const String &p_core_dll_path, bool p_verbose_output = true);
Error generate_glue(const String &p_output_dir);

static BindingsGenerator &get_singleton() {
static BindingsGenerator singleton;
void initialize();

_FORCE_INLINE_ static BindingsGenerator *get_singleton() {
if (!singleton) {
singleton = memnew(BindingsGenerator);
singleton->initialize();
}
return singleton;
}

Expand Down
6 changes: 3 additions & 3 deletions modules/mono/editor/godotsharp_builds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,12 @@ bool GodotSharpBuilds::make_api_sln(GodotSharpBuilds::APIType p_api_type) {
#error "How am I supposed to generate the bindings?"
#endif

BindingsGenerator &gen = BindingsGenerator::get_singleton();
BindingsGenerator *gen = BindingsGenerator::get_singleton();
bool gen_verbose = OS::get_singleton()->is_stdout_verbose();

Error err = p_api_type == API_CORE ?
gen.generate_cs_core_project(api_sln_dir, gen_verbose) :
gen.generate_cs_editor_project(api_sln_dir, core_api_assembly, gen_verbose);
gen->generate_cs_core_project(api_sln_dir, gen_verbose) :
gen->generate_cs_editor_project(api_sln_dir, core_api_assembly, gen_verbose);

if (err != OK) {
show_build_error_dialog("Failed to generate " + api_name + " solution. Error: " + itos(err));
Expand Down

0 comments on commit fe39139

Please sign in to comment.