More memory management.
I'm seeing a lot of potential memory leaks of various FLTK components... Unless their ctors register themselves into some global garbage collector... It's a mess.
This commit is contained in:
@ -32,6 +32,8 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
#include <FL/Fl.H>
|
#include <FL/Fl.H>
|
||||||
#include <FL/fl_ask.H>
|
#include <FL/fl_ask.H>
|
||||||
@ -104,7 +106,7 @@ class DLItem {
|
|||||||
char *shortname, *fullname;
|
char *shortname, *fullname;
|
||||||
char *esc_url;
|
char *esc_url;
|
||||||
char *cookies_path;
|
char *cookies_path;
|
||||||
char *target_dir;
|
std::string target_dir;
|
||||||
size_t log_len, log_max;
|
size_t log_len, log_max;
|
||||||
int log_state;
|
int log_state;
|
||||||
char *log_text;
|
char *log_text;
|
||||||
@ -191,20 +193,18 @@ public:
|
|||||||
* FLTK cannot be dissuaded from interpreting '@' in a tooltip
|
* FLTK cannot be dissuaded from interpreting '@' in a tooltip
|
||||||
* as indicating a symbol unless we escape it.
|
* as indicating a symbol unless we escape it.
|
||||||
*/
|
*/
|
||||||
static char *escape_tooltip(const char *buf, ssize_t len)
|
static std::string escape_tooltip(const char *buf, ssize_t len)
|
||||||
{
|
{
|
||||||
if (len < 0)
|
if (len < 0)
|
||||||
len = 0;
|
len = 0;
|
||||||
|
|
||||||
char *ret = (char *) malloc(2 * len + 1);
|
//char *ret = (char *) malloc(2 * len + 1);
|
||||||
char *dest = ret;
|
std::string ret;
|
||||||
|
|
||||||
while (len-- > 0) {
|
while (len-- > 0) {
|
||||||
if (*buf == '@')
|
if (*buf == '@') ret.push_back( *buf );
|
||||||
*dest++ = *buf;
|
ret.push_back( *buf++ );
|
||||||
*dest++ = *buf++;
|
|
||||||
}
|
}
|
||||||
*dest = '\0';
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -304,7 +304,7 @@ DLItem::DLItem(const char *full_filename, const char *url, const char *user_agen
|
|||||||
p = strrchr(fullname, '/');
|
p = strrchr(fullname, '/');
|
||||||
shortname = (p) ? dStrdup(p + 1) : dStrdup("??");
|
shortname = (p) ? dStrdup(p + 1) : dStrdup("??");
|
||||||
p = strrchr(full_filename, '/');
|
p = strrchr(full_filename, '/');
|
||||||
target_dir= p ? dStrndup(full_filename,p-full_filename+1) : dStrdup("??");
|
target_dir= p ? std::string_view{ full_filename, p - full_filename + 1 } : "??";
|
||||||
|
|
||||||
log_len = 0;
|
log_len = 0;
|
||||||
log_max = 0;
|
log_max = 0;
|
||||||
@ -433,7 +433,6 @@ DLItem::~DLItem()
|
|||||||
{
|
{
|
||||||
free(shortname);
|
free(shortname);
|
||||||
dFree(fullname);
|
dFree(fullname);
|
||||||
dFree(target_dir);
|
|
||||||
free(log_text);
|
free(log_text);
|
||||||
dFree(esc_url);
|
dFree(esc_url);
|
||||||
dFree(cookies_path);
|
dFree(cookies_path);
|
||||||
@ -490,12 +489,12 @@ void DLItem::update_prSize(int newsize)
|
|||||||
void DLItem::log_text_add(const char *buf, ssize_t st)
|
void DLItem::log_text_add(const char *buf, ssize_t st)
|
||||||
{
|
{
|
||||||
const char *p;
|
const char *p;
|
||||||
char *esc_str, *q, *d, num[64];
|
char *q, *d, num[64];
|
||||||
size_t esc_len;
|
size_t esc_len;
|
||||||
|
|
||||||
// WORKAROUND: We have to escape '@' in FLTK tooltips.
|
// WORKAROUND: We have to escape '@' in FLTK tooltips.
|
||||||
esc_str = escape_tooltip(buf, st);
|
auto esc_str = escape_tooltip(buf, st);
|
||||||
esc_len = strlen(esc_str);
|
esc_len = esc_str.size();
|
||||||
|
|
||||||
// Make room...
|
// Make room...
|
||||||
if (log_len + esc_len >= log_max) {
|
if (log_len + esc_len >= log_max) {
|
||||||
@ -507,7 +506,7 @@ void DLItem::log_text_add(const char *buf, ssize_t st)
|
|||||||
|
|
||||||
// FSM to remove wget's "dot-progress" (i.e. "^ " || "^[0-9]+K")
|
// FSM to remove wget's "dot-progress" (i.e. "^ " || "^[0-9]+K")
|
||||||
q = log_text + log_len;
|
q = log_text + log_len;
|
||||||
for (p = esc_str; (size_t)(p - esc_str) < esc_len; ++p) {
|
for (p = esc_str.data(); (size_t)(p - esc_str.data()) < esc_len; ++p) {
|
||||||
switch (log_state) {
|
switch (log_state) {
|
||||||
case ST_newline:
|
case ST_newline:
|
||||||
if (*p == ' ') {
|
if (*p == ' ') {
|
||||||
@ -543,8 +542,6 @@ void DLItem::log_text_add(const char *buf, ssize_t st)
|
|||||||
*q = 0;
|
*q = 0;
|
||||||
log_len = strlen(log_text);
|
log_len = strlen(log_text);
|
||||||
|
|
||||||
free(esc_str);
|
|
||||||
|
|
||||||
// Now scan for the length of the file
|
// Now scan for the length of the file
|
||||||
if (total_bytesize == -1) {
|
if (total_bytesize == -1) {
|
||||||
p = strstr(log_text, "\nLength: ");
|
p = strstr(log_text, "\nLength: ");
|
||||||
|
|||||||
Reference in New Issue
Block a user