Bug 993591: Eagerly free nsStreamLoader data. r=mcmanus

This commit is contained in:
Ben Kelly 2014-04-17 10:59:54 -04:00
Родитель b720c442dc
Коммит ed231bac61
3 изменённых файлов: 21 добавлений и 10 удалений

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

@ -27,9 +27,9 @@ interface nsIStreamLoaderObserver : nsISupports
* If the observer wants to take over responsibility for the
* data buffer (result), it returns NS_SUCCESS_ADOPTED_DATA
* in place of NS_OK as its success code. The loader will then
* "forget" about the data, and not NS_Free() it in its own
* destructor; observer must call NS_Free() when the data is
* no longer required.
* "forget" about the data and not NS_Free() it after
* onStreamComplete() returns; observer must call NS_Free()
* when the data is no longer required.
*/
void onStreamComplete(in nsIStreamLoader loader,
in nsISupports ctxt,

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

@ -18,9 +18,7 @@ nsStreamLoader::nsStreamLoader()
nsStreamLoader::~nsStreamLoader()
{
if (mData) {
NS_Free(mData);
}
ReleaseData();
}
NS_IMETHODIMP
@ -103,10 +101,9 @@ nsStreamLoader::OnStopRequest(nsIRequest* request, nsISupports *ctxt,
// the observer now owns the data buffer, and the loader must
// not deallocate it
mData = nullptr;
mLength = 0;
mAllocated = 0;
}
// done.. cleanup
ReleaseData();
mRequest = 0;
mObserver = 0;
mContext = 0;
@ -132,8 +129,7 @@ nsStreamLoader::WriteSegmentFun(nsIInputStream *inStr,
self->mData = static_cast<uint8_t*>(NS_Realloc(self->mData,
self->mLength + count));
if (!self->mData) {
self->mLength = 0;
self->mAllocated = 0;
self->ReleaseData();
return NS_ERROR_OUT_OF_MEMORY;
}
self->mAllocated = self->mLength + count;
@ -155,3 +151,14 @@ nsStreamLoader::OnDataAvailable(nsIRequest* request, nsISupports *ctxt,
uint32_t countRead;
return inStr->ReadSegments(WriteSegmentFun, this, count, &countRead);
}
void
nsStreamLoader::ReleaseData()
{
if (mData) {
NS_Free(mData);
mData = nullptr;
}
mLength = 0;
mAllocated = 0;
}

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

@ -30,6 +30,10 @@ protected:
static NS_METHOD WriteSegmentFun(nsIInputStream *, void *, const char *,
uint32_t, uint32_t, uint32_t *);
// Utility method to free mData, if present, and update other state to
// reflect that no data has been allocated.
void ReleaseData();
nsCOMPtr<nsIStreamLoaderObserver> mObserver;
nsCOMPtr<nsISupports> mContext; // the observer's context
nsCOMPtr<nsIRequest> mRequest;