Skip to content

Commit

Permalink
Fix leak ref #49 (#52)
Browse files Browse the repository at this point in the history
* Fix leak

valgrind indicates no leak anymore; but we have the remaining issue of `charsub` with "Invalid read of size 1"

* Fix valgrind invalid read

But might have performance impact; need to check
  • Loading branch information
chainsawriot authored Sep 28, 2023
1 parent c993967 commit 8a8ec9f
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/adaR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ std::string charsub(const ada_string stringi) {
const char* res = stringi.data;
size_t len = stringi.length;
ada_owned_string stringi_new = ada_idna_to_unicode(res, len);
res = stringi_new.data;
len = stringi_new.length;
return std::string(res, 0, len);
std::string_view output(stringi_new.data, stringi_new.length);
std::string output2 = {output.begin(), output.end()};
ada_free_owned_string(stringi_new);
return output2;
}

// [[Rcpp::export]]
Expand All @@ -24,8 +25,8 @@ DataFrame Rcpp_ada_parse(const CharacterVector& input_vec) {
CharacterVector hash(n);
for (unsigned int i = 0; i < n; i++) {
String s = input_vec[i];
const char* input = s.get_cstring();
ada_url url = ada_parse(input, std::strlen(input));
std::string_view input(s.get_cstring());
ada_url url = ada_parse(input.data(), input.length());
if (ada_is_valid(url)) {
href[i] = charsub(ada_get_href(url));
protocol[i] = charsub(ada_get_protocol(url));
Expand All @@ -49,6 +50,7 @@ DataFrame Rcpp_ada_parse(const CharacterVector& input_vec) {
search[i] = NA_STRING;
hash[i] = NA_STRING;
}
ada_free(url);
}
return (DataFrame::create(Named("href") = href, _["protocol"] = protocol,
_["username"] = username, _["password"] = password,
Expand All @@ -63,13 +65,14 @@ LogicalVector Rcpp_ada_has(const CharacterVector& url_vec, std::function<bool(ad
LogicalVector out(n);
for (unsigned int i = 0; i < n; i++) {
String s = url_vec[i];
const char* input = s.get_cstring();
ada_url url = ada_parse(input, std::strlen(input));
std::string_view input(s.get_cstring());
ada_url url = ada_parse(input.data(), input.length());
if (!ada_is_valid(url)) {
out[i] = NA_LOGICAL;
} else {
out[i] = func(url);
}
ada_free(url);
}
return out;
}
Expand Down Expand Up @@ -120,13 +123,14 @@ CharacterVector Rcpp_ada_get(const CharacterVector& url_vec, std::function<ada_s
CharacterVector out(n);
for (int i = 0; i < url_vec.length(); i++) {
String s = url_vec[i];
const char* input = s.get_cstring();
ada_url url = ada_parse(input, std::strlen(input));
std::string_view input(s.get_cstring());
ada_url url = ada_parse(input.data(), input.length());
if (!ada_is_valid(url)) {
out[i] = NA_STRING;
} else {
out[i] = charsub(func(url));
}
ada_free(url);
}
return (out);
}
Expand Down

0 comments on commit 8a8ec9f

Please sign in to comment.