Malloc problem deleting string array

Hello,

I have a class that contains an array of TStrings. I implement the array as a pointer, as I need to set the size of the array dynamically. In the destructor for this class, I try to delete this pointer. However, I receive the following message whenever the destructor is called:

root.exe(6163) malloc: *** Deallocation of a pointer not malloced: 0x2bd0574; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug.

The following class reproduces the problem I am experiencing:

class StrArray
{private:
  TString* TheArray;
  Int_t NumEntries;
 public:
  StrArray(Int_t);
  ~StrArray();
};

StrArray::StrArray(Int_t Entries)
{
  NumEntries = Entries;
  TheArray = new TString[NumEntries];
}

StrArray::~StrArray()
{if (TheArray != NULL) delete TheArray;}

with the following lines at the root prompt:

StrArray a(5)
.q

What is the error that I am making in trying to delete this pointer? Is there a simple way to implement a dynamically-sized array so as to avoid this problem with pointers?

Thanks,
Adrian

[quote=“adriand”]Hello,

I have a class that contains an array of TStrings. I implement the array as a pointer, as I need to set the size of the array dynamically. In the destructor for this class, I try to delete this pointer. However, I receive the following message whenever the destructor is called:

root.exe(6163) malloc: *** Deallocation of a pointer not malloced: 0x2bd0574; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug.

The following class reproduces the problem I am experiencing:

class StrArray
{private:
  TString* TheArray;
  Int_t NumEntries;
 public:
  StrArray(Int_t);
  ~StrArray();
};

StrArray::StrArray(Int_t Entries)
{
  NumEntries = Entries;
  TheArray = new TString[NumEntries];
}

StrArray::~StrArray()
{if (TheArray != NULL) delete TheArray;}

with the following lines at the root prompt:

StrArray a(5)
.q

What is the error that I am making in trying to delete this pointer? Is there a simple way to implement a dynamically-sized array so as to avoid this problem with pointers?

Thanks,
Adrian[/quote]
try: delete[] TheArray;

and
please use standard containers :wink: bicycle is already invented…

Thanks for the suggestion. I’ve changed all of my string arrays to TObjArrays of TObjStrings. However, I’m still getting malloc errors when I try to delete pointers, this time to histograms. The short class below demonstrates the behavior that I’m getting.

class TestClass
{private:
  TH1F* Hist;
 public:
  TestClass();
  ~TestClass();
  void SetHist(TString, TString);
};

TestClass::TestClass()
{Hist = NULL;}

TestClass::~TestClass()
{if (Hist != NULL) delete Hist;}

void TestClass::SetHist(TString FileName, TString HistName)
{
  TFile* f = new TFile(FileName);
  Hist = (TH1F*)f->Get(HistName);
}

The following at the root prompt produces the malloc messages:

.L TestClass.c
TestClass foo
foo.SetHist("Histogram_file.root", "h31112")
.q

The following is output:

What am I doing wrong in deleting this pointer?
Thanks,
Adrian

Hi,

I can not reproduce this problem with the current version of ROOT. So I assume this has been fixed since the version you are using.

You can also use valgrind (valgrind.kde.org) to find out more information on what is being double deleted.

Cheers,
Philippe