fixed memory leaks in nsAutoString

This commit is contained in:
peterl 1998-04-16 21:47:17 +00:00
Родитель 75c0994f83
Коммит 355fcae2bd
8 изменённых файлов: 172 добавлений и 24 удалений

Просмотреть файл

@ -87,6 +87,26 @@ nsString::nsString(const PRUnichar* aUnicodeStr){
SelfTest();
}
/*-------------------------------------------------------
* special subclas constructor
* this will optionally not allocate a buffer
* but the subclass must
* @update psl 4/16/98
* @param
* @return
*------------------------------------------------------*/
nsString::nsString(PRBool aSubclassBuffer)
{
mLength=mCapacity=0;
mStr=0;
if (PR_FALSE == aSubclassBuffer) {
EnsureCapacityFor(1);
this->SetString("");
}
if(++mInstanceCount==1)
SelfTest();
}
/*-------------------------------------------------------
* standard destructor
* @update gess 3/27/98
@ -1829,7 +1849,9 @@ void nsString::SelfTest(void) {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString() : nsString() {
nsAutoString::nsAutoString()
: nsString(PR_TRUE)
{
mStr = mBuf;
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
@ -1841,7 +1863,8 @@ nsAutoString::nsAutoString() : nsString() {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(const char* isolatin1) : nsString()
nsAutoString::nsAutoString(const char* isolatin1)
: nsString(PR_TRUE)
{
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
@ -1855,7 +1878,9 @@ nsAutoString::nsAutoString(const char* isolatin1) : nsString()
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(const nsString& other) {
nsAutoString::nsAutoString(const nsString& other)
: nsString(PR_TRUE)
{
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
mStr=mBuf;
@ -1868,7 +1893,9 @@ nsAutoString::nsAutoString(const nsString& other) {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(PRUnichar aChar) {
nsAutoString::nsAutoString(PRUnichar aChar)
: nsString(PR_TRUE)
{
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
mStr=mBuf;
@ -1881,7 +1908,9 @@ nsAutoString::nsAutoString(PRUnichar aChar) {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(const nsAutoString& other) {
nsAutoString::nsAutoString(const nsAutoString& other)
: nsString(PR_TRUE)
{
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
mStr=mBuf;
@ -1906,6 +1935,9 @@ void nsAutoString::EnsureCapacityFor(PRInt32 aNewLength) {
if (mLength > 0) {
nsCRT::memcpy(temp, mStr, mLength * sizeof(chartype));
}
if ((mStr != mBuf) && (0 != mStr)) {
delete [] mStr;
}
mStr = temp;
}
}
@ -1916,7 +1948,9 @@ void nsAutoString::EnsureCapacityFor(PRInt32 aNewLength) {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(const PRUnichar* unicode, PRInt32 uslen) {
nsAutoString::nsAutoString(const PRUnichar* unicode, PRInt32 uslen)
: nsString(PR_TRUE)
{
mStr = mBuf;
mCapacity = sizeof(mBuf) / sizeof(chartype);
if (0 == uslen) {

Просмотреть файл

@ -43,6 +43,9 @@ class NS_BASE nsString {
nsString(const char* anISOLatin1="");
nsString(const nsString&);
nsString(const PRUnichar* aUnicode);
protected:
nsString(PRBool aSubclassBuffer); // special subclas constructor
public:
virtual ~nsString();
PRInt32 Length() const { return mLength; }

Просмотреть файл

@ -87,6 +87,26 @@ nsString::nsString(const PRUnichar* aUnicodeStr){
SelfTest();
}
/*-------------------------------------------------------
* special subclas constructor
* this will optionally not allocate a buffer
* but the subclass must
* @update psl 4/16/98
* @param
* @return
*------------------------------------------------------*/
nsString::nsString(PRBool aSubclassBuffer)
{
mLength=mCapacity=0;
mStr=0;
if (PR_FALSE == aSubclassBuffer) {
EnsureCapacityFor(1);
this->SetString("");
}
if(++mInstanceCount==1)
SelfTest();
}
/*-------------------------------------------------------
* standard destructor
* @update gess 3/27/98
@ -1829,7 +1849,9 @@ void nsString::SelfTest(void) {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString() : nsString() {
nsAutoString::nsAutoString()
: nsString(PR_TRUE)
{
mStr = mBuf;
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
@ -1841,7 +1863,8 @@ nsAutoString::nsAutoString() : nsString() {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(const char* isolatin1) : nsString()
nsAutoString::nsAutoString(const char* isolatin1)
: nsString(PR_TRUE)
{
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
@ -1855,7 +1878,9 @@ nsAutoString::nsAutoString(const char* isolatin1) : nsString()
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(const nsString& other) {
nsAutoString::nsAutoString(const nsString& other)
: nsString(PR_TRUE)
{
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
mStr=mBuf;
@ -1868,7 +1893,9 @@ nsAutoString::nsAutoString(const nsString& other) {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(PRUnichar aChar) {
nsAutoString::nsAutoString(PRUnichar aChar)
: nsString(PR_TRUE)
{
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
mStr=mBuf;
@ -1881,7 +1908,9 @@ nsAutoString::nsAutoString(PRUnichar aChar) {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(const nsAutoString& other) {
nsAutoString::nsAutoString(const nsAutoString& other)
: nsString(PR_TRUE)
{
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
mStr=mBuf;
@ -1906,6 +1935,9 @@ void nsAutoString::EnsureCapacityFor(PRInt32 aNewLength) {
if (mLength > 0) {
nsCRT::memcpy(temp, mStr, mLength * sizeof(chartype));
}
if ((mStr != mBuf) && (0 != mStr)) {
delete [] mStr;
}
mStr = temp;
}
}
@ -1916,7 +1948,9 @@ void nsAutoString::EnsureCapacityFor(PRInt32 aNewLength) {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(const PRUnichar* unicode, PRInt32 uslen) {
nsAutoString::nsAutoString(const PRUnichar* unicode, PRInt32 uslen)
: nsString(PR_TRUE)
{
mStr = mBuf;
mCapacity = sizeof(mBuf) / sizeof(chartype);
if (0 == uslen) {

Просмотреть файл

@ -43,6 +43,9 @@ class NS_BASE nsString {
nsString(const char* anISOLatin1="");
nsString(const nsString&);
nsString(const PRUnichar* aUnicode);
protected:
nsString(PRBool aSubclassBuffer); // special subclas constructor
public:
virtual ~nsString();
PRInt32 Length() const { return mLength; }

Просмотреть файл

@ -87,6 +87,26 @@ nsString::nsString(const PRUnichar* aUnicodeStr){
SelfTest();
}
/*-------------------------------------------------------
* special subclas constructor
* this will optionally not allocate a buffer
* but the subclass must
* @update psl 4/16/98
* @param
* @return
*------------------------------------------------------*/
nsString::nsString(PRBool aSubclassBuffer)
{
mLength=mCapacity=0;
mStr=0;
if (PR_FALSE == aSubclassBuffer) {
EnsureCapacityFor(1);
this->SetString("");
}
if(++mInstanceCount==1)
SelfTest();
}
/*-------------------------------------------------------
* standard destructor
* @update gess 3/27/98
@ -1829,7 +1849,9 @@ void nsString::SelfTest(void) {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString() : nsString() {
nsAutoString::nsAutoString()
: nsString(PR_TRUE)
{
mStr = mBuf;
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
@ -1841,7 +1863,8 @@ nsAutoString::nsAutoString() : nsString() {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(const char* isolatin1) : nsString()
nsAutoString::nsAutoString(const char* isolatin1)
: nsString(PR_TRUE)
{
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
@ -1855,7 +1878,9 @@ nsAutoString::nsAutoString(const char* isolatin1) : nsString()
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(const nsString& other) {
nsAutoString::nsAutoString(const nsString& other)
: nsString(PR_TRUE)
{
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
mStr=mBuf;
@ -1868,7 +1893,9 @@ nsAutoString::nsAutoString(const nsString& other) {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(PRUnichar aChar) {
nsAutoString::nsAutoString(PRUnichar aChar)
: nsString(PR_TRUE)
{
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
mStr=mBuf;
@ -1881,7 +1908,9 @@ nsAutoString::nsAutoString(PRUnichar aChar) {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(const nsAutoString& other) {
nsAutoString::nsAutoString(const nsAutoString& other)
: nsString(PR_TRUE)
{
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
mStr=mBuf;
@ -1906,6 +1935,9 @@ void nsAutoString::EnsureCapacityFor(PRInt32 aNewLength) {
if (mLength > 0) {
nsCRT::memcpy(temp, mStr, mLength * sizeof(chartype));
}
if ((mStr != mBuf) && (0 != mStr)) {
delete [] mStr;
}
mStr = temp;
}
}
@ -1916,7 +1948,9 @@ void nsAutoString::EnsureCapacityFor(PRInt32 aNewLength) {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(const PRUnichar* unicode, PRInt32 uslen) {
nsAutoString::nsAutoString(const PRUnichar* unicode, PRInt32 uslen)
: nsString(PR_TRUE)
{
mStr = mBuf;
mCapacity = sizeof(mBuf) / sizeof(chartype);
if (0 == uslen) {

Просмотреть файл

@ -43,6 +43,9 @@ class NS_BASE nsString {
nsString(const char* anISOLatin1="");
nsString(const nsString&);
nsString(const PRUnichar* aUnicode);
protected:
nsString(PRBool aSubclassBuffer); // special subclas constructor
public:
virtual ~nsString();
PRInt32 Length() const { return mLength; }

Просмотреть файл

@ -87,6 +87,26 @@ nsString::nsString(const PRUnichar* aUnicodeStr){
SelfTest();
}
/*-------------------------------------------------------
* special subclas constructor
* this will optionally not allocate a buffer
* but the subclass must
* @update psl 4/16/98
* @param
* @return
*------------------------------------------------------*/
nsString::nsString(PRBool aSubclassBuffer)
{
mLength=mCapacity=0;
mStr=0;
if (PR_FALSE == aSubclassBuffer) {
EnsureCapacityFor(1);
this->SetString("");
}
if(++mInstanceCount==1)
SelfTest();
}
/*-------------------------------------------------------
* standard destructor
* @update gess 3/27/98
@ -1829,7 +1849,9 @@ void nsString::SelfTest(void) {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString() : nsString() {
nsAutoString::nsAutoString()
: nsString(PR_TRUE)
{
mStr = mBuf;
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
@ -1841,7 +1863,8 @@ nsAutoString::nsAutoString() : nsString() {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(const char* isolatin1) : nsString()
nsAutoString::nsAutoString(const char* isolatin1)
: nsString(PR_TRUE)
{
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
@ -1855,7 +1878,9 @@ nsAutoString::nsAutoString(const char* isolatin1) : nsString()
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(const nsString& other) {
nsAutoString::nsAutoString(const nsString& other)
: nsString(PR_TRUE)
{
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
mStr=mBuf;
@ -1868,7 +1893,9 @@ nsAutoString::nsAutoString(const nsString& other) {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(PRUnichar aChar) {
nsAutoString::nsAutoString(PRUnichar aChar)
: nsString(PR_TRUE)
{
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
mStr=mBuf;
@ -1881,7 +1908,9 @@ nsAutoString::nsAutoString(PRUnichar aChar) {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(const nsAutoString& other) {
nsAutoString::nsAutoString(const nsAutoString& other)
: nsString(PR_TRUE)
{
mLength=0;
mCapacity = (sizeof(mBuf) / sizeof(chartype))-sizeof(chartype);
mStr=mBuf;
@ -1906,6 +1935,9 @@ void nsAutoString::EnsureCapacityFor(PRInt32 aNewLength) {
if (mLength > 0) {
nsCRT::memcpy(temp, mStr, mLength * sizeof(chartype));
}
if ((mStr != mBuf) && (0 != mStr)) {
delete [] mStr;
}
mStr = temp;
}
}
@ -1916,7 +1948,9 @@ void nsAutoString::EnsureCapacityFor(PRInt32 aNewLength) {
* @param
* @return
*------------------------------------------------------*/
nsAutoString::nsAutoString(const PRUnichar* unicode, PRInt32 uslen) {
nsAutoString::nsAutoString(const PRUnichar* unicode, PRInt32 uslen)
: nsString(PR_TRUE)
{
mStr = mBuf;
mCapacity = sizeof(mBuf) / sizeof(chartype);
if (0 == uslen) {

Просмотреть файл

@ -43,6 +43,9 @@ class NS_BASE nsString {
nsString(const char* anISOLatin1="");
nsString(const nsString&);
nsString(const PRUnichar* aUnicode);
protected:
nsString(PRBool aSubclassBuffer); // special subclas constructor
public:
virtual ~nsString();
PRInt32 Length() const { return mLength; }