Skip to content

Commit

Permalink
Fixed memory leaks in info_buffer
Browse files Browse the repository at this point in the history
And what a messy-looking fix it is, too.
  • Loading branch information
Mark Carter committed Apr 20, 2018
1 parent 5718c9c commit 9212686
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
49 changes: 32 additions & 17 deletions src/info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,27 @@

using namespace std::string_literals;

std::map<std::string, struct info_buffer *> info_buffers_1;
std::map<std::string, info_buffer_t*> info_buffers_1;

//////////////////////////////////////////////////////////////////////////
// atexit cleanup
// There must surely be a better way of doing this: probably by
// declaring info_buffer as a class, and making info_buffers_1
// be a map of info_buffer's rather than their pointers.
//
class cleanup_buffers { public: ~cleanup_buffers(); };

cleanup_buffers::~cleanup_buffers()
{
for(auto it = info_buffers_1.begin() ; it != info_buffers_1.end(); ++it) {
auto buf = it->second;
clear_info(buf);
delete buf;
}
}

static cleanup_buffers buffer_cleaner;
//////////////////////////////////////////////////////////////////////////


struct info_buffer *
Expand All @@ -53,21 +73,22 @@ find_info(const char * name)
}


struct info_buffer *
info_buffer_t *
find_or_make_info(const char * name)
{
if constexpr(false) log_debug("find_or_make_info:"s + name);
struct info_buffer * buf = find_info(name);
if (buf)
return buf;

buf = ((struct info_buffer *)
ck_malloc (sizeof (struct info_buffer) + strlen(name) + 1));
buf->name = (char *)buf + sizeof (*buf);
strcpy (buf->name, name);
buf->len = 0;
buf->text = 0;
info_buffers_1[buf->name] = buf;
//buf = ((struct info_buffer *) ck_malloc (sizeof (struct info_buffer) + strlen(name) + 1));
buf = new info_buffer_t;
buf->name = name;
//buf->name = (char *)buf + sizeof (*buf);
//strcpy (buf->name, name);
//buf->len = 0;
//buf->text = 0;
info_buffers_1[name] = buf;
return buf;
}

Expand Down Expand Up @@ -116,12 +137,6 @@ print_info (struct info_buffer * buf, const char * format, ...)
va_end (ap);
len = strlen (txt);

/*
++buf->len; // Number of lines in the buffer
buf->text = (char **)ck_remalloc (buf->text, buf->len * sizeof (char *));
buf->text[buf->len - 1] = (char *)ck_malloc (len + 1);
bcopy (txt, buf->text[buf->len - 1], len + 1);
*/
fill_info_buffer(buf, txt, len);
}

Expand All @@ -133,8 +148,8 @@ static struct info_buffer * the_text_buf;
void
io_text_start (void)
{
the_text_buf = find_or_make_info ("_text");
clear_info (the_text_buf);
the_text_buf = find_or_make_info ("_text");
clear_info (the_text_buf);
}


Expand Down
13 changes: 8 additions & 5 deletions src/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
struct info_buffer

#include <string>

typedef struct info_buffer
{
char * name;
int len;
char ** text;
};
std::string name;
int len = 0;
char ** text = nullptr;
} info_buffer_t;

struct info_buffer* find_info(const char * name);
struct info_buffer* find_or_make_info(const char * name);
Expand Down

0 comments on commit 9212686

Please sign in to comment.