Skip to content

Commit

Permalink
Fix the resource releasing on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
ttldtor committed Oct 31, 2023
1 parent 39b53b7 commit 120bcfa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 51 deletions.
30 changes: 0 additions & 30 deletions include/dxfeed_graal_cpp_api/internal/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,36 +85,6 @@ constexpr To bit_cast(const From &from)
return to;
}

// final_action allows you to ensure something gets run at the end of a scope
template <class F> class final_action {
public:
explicit final_action(const F &ff) noexcept : f{ff} {
}
explicit final_action(F &&ff) noexcept : f{std::move(ff)} {
}

~final_action() noexcept {
if (invoke)
f();
}

final_action(final_action &&other) noexcept : f(std::move(other.f)), invoke(std::exchange(other.invoke, false)) {
}

final_action(const final_action &) = delete;
void operator=(const final_action &) = delete;
void operator=(final_action &&) = delete;

private:
F f;
bool invoke = true;
};

// finally() - convenience function to generate a final_action
template <class F> auto finally(F &&f) noexcept {
return final_action<std::decay_t<F>>{std::forward<F>(f)};
}

/// Lightweight implementation of "nullable bool"
enum class Tristate : std::uint8_t {
FALSE = 0,
Expand Down
40 changes: 19 additions & 21 deletions src/internal/Isolate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,11 @@ std::string InstrumentProfileReader::resolveSourceURL(const std::string &address
},
nullptr, address);

finally([resolvedURL] {
String::release(resolvedURL);
});
auto result = dxfcpp::toString(resolvedURL);

return dxfcpp::toString(resolvedURL);
String::release(resolvedURL);

return result;
}

/* dxfg_ipf_collector_t* */ void *InstrumentProfileCollector::create() noexcept {
Expand Down Expand Up @@ -429,11 +429,11 @@ std::string InstrumentProfileConnection::getAddress(
return dxfcpp::String::EMPTY;
}

finally([address] {
String::release(address);
});
auto result = dxfcpp::toString(address);

String::release(address);

return dxfcpp::toString(address);
return result;
}

std::int64_t InstrumentProfileConnection::getUpdatePeriod(
Expand Down Expand Up @@ -684,11 +684,11 @@ std::string Day::toString(/* dxfg_day_t* */ void *day) noexcept {
return dxfcpp::String::EMPTY;
}

finally([string] {
String::release(string);
});
auto result = dxfcpp::toString(string);

String::release(string);

return dxfcpp::toString(string);
return result;
}

/* dxfg_session_filter_t* */ void *SessionFilter::getInstance(std::uint32_t code) noexcept {
Expand Down Expand Up @@ -752,11 +752,11 @@ std::string Session::toString(/* dxfg_session_t* */ void *session) noexcept {
return dxfcpp::String::EMPTY;
}

finally([string] {
String::release(string);
});
auto result = dxfcpp::toString(string);

String::release(string);

return dxfcpp::toString(string);
return result;
}

/* dxfg_day_filter_t* */ void *DayFilter::getInstance(std::uint32_t code) noexcept {
Expand Down Expand Up @@ -817,18 +817,16 @@ std::vector<std::string> Schedule::getTradingVenues(/* dxfg_instrument_profile_t
},
nullptr, dxfcpp::bit_cast<dxfg_instrument_profile_t *>(instrumentProfile));

finally([graalStringList] {
StringList::release(graalStringList);
});

if (!graalStringList || graalStringList->size == 0) {
if (!graalStringList) {
return result;
}

for (auto i = 0; i < graalStringList->size; i++) {
result.push_back(dxfcpp::toString(graalStringList->elements[i]));
}

StringList::release(graalStringList);

return result;
};

Expand Down

0 comments on commit 120bcfa

Please sign in to comment.