Permalink
Browse files

fix memory allocation and releasing in constructors and destructors

  • Loading branch information...
LipuFei committed Sep 4, 2014
1 parent 53f955a commit ae6006e90cc546abcea1b44dc025f0a5da99971d
Showing with 32 additions and 10 deletions.
  1. +4 −2 content.cpp
  2. +5 −0 storage.cpp
  3. +23 −8 transfer.cpp
View
@@ -32,7 +32,7 @@ uint64_t ContentTransfer::cleancounter = 0;
#define TRACKER_RETRY_INTERVAL_MAX (1800*TINT_SEC) // 30 minutes
ContentTransfer::ContentTransfer(transfer_t ttype) : ttype_(ttype),
swarm_id_(), mychannels_(), callbacks_(), picker_(NULL),
swarm_id_(), mychannels_(), callbacks_(), picker_(NULL), hashtree_(NULL),
speedupcount_(0), speeddwcount_(0), trackerurl_(),
tracker_retry_interval_(TRACKER_RETRY_INTERVAL_START),
tracker_retry_time_(NOW),
@@ -50,8 +50,10 @@ ContentTransfer::~ContentTransfer()
{
dprintf("%s F%d content deconstructor\n",tintstr(),td_);
CloseChannels(mychannels_,true);
if (storage_ != NULL)
if (storage_ != NULL) {
delete storage_;
storage_ = NULL;
}
if (ext_tracker_client_ != NULL) {
delete ext_tracker_client_;
View
@@ -83,6 +83,11 @@ Storage::Storage(std::string ospathname, std::string destdir, int td, uint64_t l
dprintf("%s %s storage: Found multifile-spec, will seed it.\n", tintstr(), roothashhex().c_str());
StorageFile *sf = new StorageFile(MULTIFILE_PATHNAME,0,fsize,filename);
if (!sf->IsOperational()) {
print_error("storage: multi-file spec file is not operational");
SetBroken();
return;
}
sfs_.push_back(sf);
if (ParseSpec(sf) < 0) {
print_error("storage: error parsing multi-file spec");
View
@@ -75,6 +75,14 @@ FileTransfer::FileTransfer(int td, std::string filename, const Sha1Hash& root_ha
// MULTIFILE
storage_ = new Storage(filename,destdir,td_,0,meta_mfspec_filename);
if (!storage_->IsOperational()) {
fprintf(stderr, "[WARN] [1]\n");
delete storage_;
fprintf(stderr, "[WARN] [2]\n");
storage_ = NULL;
SetBroken();
return;
}
// Arno, 2013-02-25: Create HashTree even when PROT_NONE to enable
// automatic size determination via peak hashes.
@@ -110,15 +118,22 @@ void FileTransfer::UpdateOperational()
FileTransfer::~FileTransfer()
{
delete hashtree_;
hashtree_ = NULL;
if (hashtree_ != NULL) {
delete hashtree_;
hashtree_ = NULL;
}
if (!IsZeroState()) {
delete picker_;
picker_ = NULL;
delete availability_;
// ~ContentTransfer calls CloseChannels which calls Channel::Close which tries to unregister
// the availability of that peer from availability_, which has been deallocated here already :-(
availability_ = NULL;
if (picker_ != NULL) {
delete picker_;
picker_ = NULL;
}
if (availability_ != NULL) {
delete availability_;
// ~ContentTransfer calls CloseChannels which calls Channel::Close which tries to unregister
// the availability of that peer from availability_, which has been deallocated here already :-(
availability_ = NULL;
}
}
}

0 comments on commit ae6006e

Please sign in to comment.