From 73eb737f5da7a209ed038b0c27a788595a652617 Mon Sep 17 00:00:00 2001 From: randomdan Date: Fri, 10 Jan 2014 21:37:46 +0000 Subject: Improvements to popenrw and add popenrwe for the stderr pipe too Improve exception when loading a library fails Fix a handful of ingeniously located memory leaks --- project2/basics/preload.cpp | 6 ++++-- project2/cgi/testCgi.cpp | 5 +++++ project2/common/Jamfile.jam | 1 + project2/common/library.cpp | 11 ++++------- project2/common/library.h | 5 +++++ project2/common/optionsSource.h | 8 ++++---- project2/common/scripts.h | 3 +++ project2/processes/processStream.cpp | 9 ++++----- project2/url/curlHelper.h | 5 ++++- project2/xml/transformText.cpp | 7 +++---- 10 files changed, 37 insertions(+), 23 deletions(-) diff --git a/project2/basics/preload.cpp b/project2/basics/preload.cpp index b701d8d..104dc22 100644 --- a/project2/basics/preload.cpp +++ b/project2/basics/preload.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -23,8 +24,9 @@ class Preload { Logger()->messagebf(LOG_DEBUG, "Loaded library '%s'", librarypath.as()); } else { - Logger()->messagebf(LOG_ERR, "Failed to load library '%s' (%s)", librarypath.as(), dlerror()); - throw std::runtime_error("module load failed"); + const char * dlerr = dlerror(); + Logger()->messagebf(LOG_ERR, "Failed to load library '%s' (%s)", librarypath.as(), dlerr); + throw LoadLibraryFailed(librarypath, dlerr); } libs[librarypath.as()] = boost::shared_ptr(handle, &dlclose); diff --git a/project2/cgi/testCgi.cpp b/project2/cgi/testCgi.cpp index 80c3f01..6644102 100644 --- a/project2/cgi/testCgi.cpp +++ b/project2/cgi/testCgi.cpp @@ -32,6 +32,11 @@ class TestInput : public cgicc::CgiInput, public CgiEnvInput { OptionsSources::Add("_2", new CommandLineArguments(argc, argv, [](const char * url) { urls.push_back(url); })); } + ~TestInput() + { + OptionsSources::Remove("_1"); + OptionsSources::Remove("_2"); + } std::string getenv(const std::string & varName) const { diff --git a/project2/common/Jamfile.jam b/project2/common/Jamfile.jam index 541e714..93c9754 100644 --- a/project2/common/Jamfile.jam +++ b/project2/common/Jamfile.jam @@ -27,5 +27,6 @@ lib p2common : : : . boost_system + ../lib//p2lib ; diff --git a/project2/common/library.cpp b/project2/common/library.cpp index 738803f..0c4a83b 100644 --- a/project2/common/library.cpp +++ b/project2/common/library.cpp @@ -1,27 +1,24 @@ #include #include #include "scriptStorage.h" -#include "exceptions.h" #include "scripts.h" #include "library.h" #include "variables.h" -SimpleMessageException(LoadLibraryFailed); -SimpleMessageException(UnloadLibraryFailed); - Library::Library(ScriptNodePtr p) : SourceObject(p), - handle(dlopen(Variable(p, "path")(NULL), RTLD_NOW)) + path(p, "path"), + handle(dlopen(path(NULL), RTLD_NOW)) { if (!handle) { - throw LoadLibraryFailed(dlerror()); + throw LoadLibraryFailed(path(NULL), dlerror()); } } Library::~Library() { if (dlclose(handle)) { - throw UnloadLibraryFailed(dlerror()); + throw UnloadLibraryFailed(path(NULL), dlerror()); } } diff --git a/project2/common/library.h b/project2/common/library.h index c6d9278..7db730b 100644 --- a/project2/common/library.h +++ b/project2/common/library.h @@ -2,6 +2,10 @@ #define LIBRARY_LOADER_H #include "scriptLoader.h" +#include "exceptions.h" + +SimpleMessage2Exception(LoadLibraryFailed); +SimpleMessage2Exception(UnloadLibraryFailed); class Library : public SourceObject { public: @@ -9,6 +13,7 @@ class Library : public SourceObject { ~Library(); private: + Variable path; void * handle; }; extern STORAGEOF(Library) libraries; diff --git a/project2/common/optionsSource.h b/project2/common/optionsSource.h index 53df0ea..97a3566 100644 --- a/project2/common/optionsSource.h +++ b/project2/common/optionsSource.h @@ -25,10 +25,10 @@ typedef boost::intrusive_ptr OptionsSourcePtr; typedef PluginsSameBase OptionsSources; #define DECLARE_OPTIONSSOURCE(Id, Inst) \ - static void init_optionsSource_##Type() __attribute__ ((constructor(200))); \ - static void init_optionsSource_##Type() { OptionsSources::Add(Id, Inst); } \ - static void kill_optionsSource_##Type() __attribute__ ((destructor(200))); \ - static void kill_optionsSource_##Type() { OptionsSources::Remove(Id); } + static void init_optionsSource() __attribute__ ((constructor(200))); \ + static void init_optionsSource() { OptionsSources::Add(Id, Inst); } \ + static void kill_optionsSource() __attribute__ ((destructor(200))); \ + static void kill_optionsSource() { OptionsSources::Remove(Id); } #endif diff --git a/project2/common/scripts.h b/project2/common/scripts.h index b7e9cdb..92151f6 100644 --- a/project2/common/scripts.h +++ b/project2/common/scripts.h @@ -30,6 +30,7 @@ class Scripts { class ScriptNode : public IntrusivePtrBase { public: ScriptNode(ScriptReaderPtr); + virtual ~ScriptNode() { } typedef std::vector ScriptNodes; typedef boost::function LiteralCallback; @@ -62,6 +63,8 @@ class ScriptNode : public IntrusivePtrBase { class ScriptReader : public virtual IntrusivePtrBase { public: + virtual ~ScriptReader() { } + virtual ScriptNodePtr root() const = 0; virtual void load(const CommonObjects *, bool childrenOnly) const; virtual bool isCurrent() const = 0; diff --git a/project2/processes/processStream.cpp b/project2/processes/processStream.cpp index f2c678f..1ed8388 100644 --- a/project2/processes/processStream.cpp +++ b/project2/processes/processStream.cpp @@ -23,13 +23,12 @@ class ProcessStream : public Stream, IHaveParameters { void runStream(const Sink & sink, ExecContext * ec) const { - const char * callProc[parameters.size() + 2]; - callProc[0] = path(ec); - int pidx = 1; + std::vector callProc; + callProc.reserve(parameters.size() + 2); + callProc.push_back(path(ec)); BOOST_FOREACH(const Parameters::value_type & p, parameters) { - callProc[pidx++] = p.second(ec); + callProc.push_back(p.second(ec)); } - callProc[pidx] = NULL; int fds[2]; popenrw(callProc, fds); diff --git a/project2/url/curlHelper.h b/project2/url/curlHelper.h index 10c6c69..76ac48f 100644 --- a/project2/url/curlHelper.h +++ b/project2/url/curlHelper.h @@ -6,8 +6,11 @@ class Curl : public CurlHandle { public: + virtual ~Curl() { } + typedef boost::function2 ReadHandler; typedef boost::function2 SendHandler; + void performRead(const ReadHandler &); void setReadHandler(const ReadHandler &); void performSend(const SendHandler &); @@ -23,7 +26,7 @@ typedef boost::intrusive_ptr CurlPtr; class CurlHelper { public: CurlHelper(); - ~CurlHelper(); + virtual ~CurlHelper(); virtual CurlPtr newCurl(ExecContext *) const; diff --git a/project2/xml/transformText.cpp b/project2/xml/transformText.cpp index 179007d..f92e8ef 100644 --- a/project2/xml/transformText.cpp +++ b/project2/xml/transformText.cpp @@ -47,18 +47,17 @@ class TransformHtmlToText : public TransformImpl { xmlDoc * doc = const_cast(cdoc->doc); str->doc.clear(); int fds[2]; - std::vector callLynx; + std::vector callLynx; callLynx.push_back("/usr/bin/lynx"); callLynx.push_back("-dump"); callLynx.push_back("-stdin"); std::string widthArg = "-width=" + width.as(); - callLynx.push_back(widthArg.c_str()); + callLynx.push_back(widthArg); if (!links) { callLynx.push_back("-nonumbers"); callLynx.push_back("-nolist"); } - callLynx.push_back(NULL); - popenrw(&callLynx.front(), fds); + popenrw(callLynx, fds); FILE * lynxIn = fdopen(fds[0], "w"); // Fixed encoding as we want the result to go back into a ustring htmlNodeDumpFileFormat(lynxIn, doc, xmlDocGetRootElement(doc), "utf-8", 0); -- cgit v1.2.3