Do better logging of string leaks. Bug 336914, r+sr=darin

This commit is contained in:
bzbarsky%mit.edu 2006-05-12 03:36:37 +00:00
Родитель 31d81c4061
Коммит 5abe2229c9
3 изменённых файлов: 34 добавлений и 5 удалений

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

@ -119,6 +119,11 @@ ReleaseData( void* data, PRUint32 flags )
{
nsMemory::Free(data);
STRING_STAT_INCREMENT(AdoptFree);
#ifdef NS_BUILD_REFCNT_LOGGING
// Treat this as destruction of a "StringAdopt" object for leak
// tracking purposes.
NS_LogDtor(data, "StringAdopt", 1);
#endif // NS_BUILD_REFCNT_LOGGING
}
// otherwise, nothing to do.
}
@ -178,12 +183,15 @@ nsStringBuffer::AddRef()
{
PR_AtomicIncrement(&mRefCount);
STRING_STAT_INCREMENT(Share);
NS_LOG_ADDREF(this, mRefCount, "nsStringBuffer", sizeof(*this));
}
void
nsStringBuffer::Release()
{
if (PR_AtomicDecrement(&mRefCount) == 0)
PRInt32 count = PR_AtomicDecrement(&mRefCount);
NS_LOG_RELEASE(this, count, "nsStringBuffer");
if (count == 0)
{
STRING_STAT_INCREMENT(Free);
free(this); // we were allocated with |malloc|
@ -196,16 +204,17 @@ nsStringBuffer::Release()
nsStringBuffer*
nsStringBuffer::Alloc(size_t size)
{
STRING_STAT_INCREMENT(Alloc);
NS_ASSERTION(size != 0, "zero capacity allocation not allowed");
nsStringBuffer *hdr =
(nsStringBuffer *) malloc(sizeof(nsStringBuffer) + size);
if (hdr)
{
STRING_STAT_INCREMENT(Alloc);
hdr->mRefCount = 1;
hdr->mStorageSize = size;
NS_LOG_ADDREF(hdr, 1, "nsStringBuffer", sizeof(*hdr));
}
return hdr;
}
@ -220,9 +229,16 @@ nsStringBuffer::Realloc(nsStringBuffer* hdr, size_t size)
// no point in trying to save ourselves if we hit this assertion
NS_ASSERTION(!hdr->IsReadonly(), "|Realloc| attempted on readonly string");
// Treat this as a release and addref for refcounting purposes, since we
// just asserted that the refcound is 1. If we don't do that, refcount
// logging will claim we've leaked all sorts of stuff.
NS_LOG_RELEASE(hdr, 0, "nsStringBuffer");
hdr = (nsStringBuffer*) realloc(hdr, sizeof(nsStringBuffer) + size);
if (hdr)
if (hdr) {
NS_LOG_ADDREF(hdr, 1, "nsStringBuffer", sizeof(*hdr));
hdr->mStorageSize = size;
}
return hdr;
}

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

@ -46,7 +46,15 @@ nsTAdoptingString_CharT::operator=( const self_type& str )
if (str.mFlags & F_OWNED)
{
Adopt(str.mData, str.mLength);
// We want to do what Adopt() does, but without actually incrementing
// the Adopt count. Note that we can be a little more straightforward
// about this than Adopt() is, because we know that str.mData is
// non-null. Should we be able to assert that str is not void here?
NS_ASSERTION(str.mData, "String with null mData?");
Finalize();
mData = str.mData;
mLength = str.mLength;
SetDataFlags(F_TERMINATED | F_OWNED);
// Make str forget the buffer we just took ownership of.
new (mutable_str) self_type();

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

@ -425,6 +425,11 @@ nsTSubstring_CharT::Adopt( char_type* data, size_type length )
SetDataFlags(F_TERMINATED | F_OWNED);
STRING_STAT_INCREMENT(Adopt);
#ifdef NS_BUILD_REFCNT_LOGGING
// Treat this as construction of a "StringAdopt" object for leak
// tracking purposes.
NS_LogCtor(mData, "StringAdopt", 1);
#endif // NS_BUILD_REFCNT_LOGGING
}
else
{