Fix PropItem leak by using nsAutoPtr (bug 407074). r+sr=peterv, a=damon

This commit is contained in:
jruderman@hmc.edu 2007-12-11 19:23:34 -08:00
parent 3a80055521
commit a71db7b656
4 changed files with 46 additions and 32 deletions

View File

@ -450,13 +450,23 @@ PRBool TypeInState::FindPropInList(nsIAtom *aProp,
* PropItem: helper struct for TypeInState
*******************************************************************/
PropItem::PropItem() :
tag(nsnull)
,attr()
,value()
{
MOZ_COUNT_CTOR(PropItem);
}
PropItem::PropItem(nsIAtom *aTag, const nsAString &aAttr, const nsAString &aValue) :
tag(aTag)
,attr(aAttr)
,value(aValue)
{
MOZ_COUNT_CTOR(PropItem);
}
PropItem::~PropItem()
{
MOZ_COUNT_DTOR(PropItem);
}

View File

@ -52,7 +52,7 @@ struct PropItem
nsString attr;
nsString value;
PropItem() : tag(nsnull), attr(), value() {}
PropItem();
PropItem(nsIAtom *aTag, const nsAString &aAttr, const nsAString &aValue);
~PropItem();
};
@ -81,12 +81,12 @@ public:
nsresult ClearProp(nsIAtom *aProp, const nsString &aAttr);
//**************************************************************************
// TakeClearProperty: hands back next poroperty item on the clear list.
// TakeClearProperty: hands back next property item on the clear list.
// caller assumes ownership of PropItem and must delete it.
nsresult TakeClearProperty(PropItem **outPropItem);
//**************************************************************************
// TakeSetProperty: hands back next poroperty item on the set list.
// TakeSetProperty: hands back next property item on the set list.
// caller assumes ownership of PropItem and must delete it.
nsresult TakeSetProperty(PropItem **outPropItem);

View File

@ -4316,8 +4316,8 @@ nsHTMLEditRules::CreateStyleForInsertText(nsISelection *aSelection, nsIDOMDocume
nsCOMPtr<nsIDOMNode> node, tmp;
PRInt32 offset;
nsresult res = mHTMLEditor->GetStartNodeAndOffset(aSelection, address_of(node), &offset);
if (NS_FAILED(res)) return res;
PropItem *item = nsnull;
NS_ENSURE_SUCCESS(res, res);
nsAutoPtr<PropItem> item;
// if we deleted selection then also for cached styles
if (mDidDeleteSelection &&
@ -4327,11 +4327,11 @@ nsHTMLEditRules::CreateStyleForInsertText(nsISelection *aSelection, nsIDOMDocume
(mTheAction == nsEditor::kOpDeleteSelection)))
{
res = ReapplyCachedStyles();
if (NS_FAILED(res)) return res;
NS_ENSURE_SUCCESS(res, res);
}
// either way we clear the cached styles array
res = ClearCachedStyles();
if (NS_FAILED(res)) return res;
NS_ENSURE_SUCCESS(res, res);
// next examine our present style and make sure default styles are either present or
// explicitly overridden. If neither, add the default style to the TypeInState
@ -4353,7 +4353,7 @@ nsHTMLEditRules::CreateStyleForInsertText(nsISelection *aSelection, nsIDOMDocume
nsAutoString curValue;
res = mHTMLEditor->GetInlinePropertyBase(propItem->tag, &(propItem->attr), nsnull,
&bFirst, &bAny, &bAll, &curValue, PR_FALSE);
if (NS_FAILED(res)) return res;
NS_ENSURE_SUCCESS(res, res);
if (!bAny) // no style set for this prop/attr
{
@ -4362,12 +4362,12 @@ nsHTMLEditRules::CreateStyleForInsertText(nsISelection *aSelection, nsIDOMDocume
}
// process clearing any styles first
mHTMLEditor->mTypeInState->TakeClearProperty(&item);
mHTMLEditor->mTypeInState->TakeClearProperty(getter_Transfers(item));
while (item)
{
nsCOMPtr<nsIDOMNode> leftNode, rightNode, secondSplitParent, newSelParent, savedBR;
res = mHTMLEditor->SplitStyleAbovePoint(address_of(node), &offset, item->tag, &item->attr, address_of(leftNode), address_of(rightNode));
if (NS_FAILED(res)) return res;
NS_ENSURE_SUCCESS(res, res);
PRBool bIsEmptyNode;
if (leftNode)
{
@ -4376,7 +4376,7 @@ nsHTMLEditRules::CreateStyleForInsertText(nsISelection *aSelection, nsIDOMDocume
{
// delete leftNode if it became empty
res = mEditor->DeleteNode(leftNode);
if (NS_FAILED(res)) return res;
NS_ENSURE_SUCCESS(res, res);
}
}
if (rightNode)
@ -4394,7 +4394,7 @@ nsHTMLEditRules::CreateStyleForInsertText(nsISelection *aSelection, nsIDOMDocume
}
offset = 0;
res = mHTMLEditor->SplitStyleAbovePoint(address_of(secondSplitParent), &offset, item->tag, &(item->attr), address_of(leftNode), address_of(rightNode));
if (NS_FAILED(res)) return res;
NS_ENSURE_SUCCESS(res, res);
// should be impossible to not get a new leftnode here
if (!leftNode) return NS_ERROR_FAILURE;
newSelParent = mHTMLEditor->GetLeftmostChild(leftNode);
@ -4404,14 +4404,14 @@ nsHTMLEditRules::CreateStyleForInsertText(nsISelection *aSelection, nsIDOMDocume
if (savedBR)
{
res = mEditor->MoveNode(savedBR, newSelParent, 0);
if (NS_FAILED(res)) return res;
NS_ENSURE_SUCCESS(res, res);
}
mHTMLEditor->IsEmptyNode(rightNode, &bIsEmptyNode, PR_FALSE, PR_TRUE);
if (bIsEmptyNode)
{
// delete rightNode if it became empty
res = mEditor->DeleteNode(rightNode);
if (NS_FAILED(res)) return res;
NS_ENSURE_SUCCESS(res, res);
}
// remove the style on this new heirarchy
PRInt32 newSelOffset = 0;
@ -4422,15 +4422,13 @@ nsHTMLEditRules::CreateStyleForInsertText(nsISelection *aSelection, nsIDOMDocume
// so I have to use the range tracking system to find the right spot to put selection.
nsAutoTrackDOMPoint tracker(mHTMLEditor->mRangeUpdater, address_of(newSelParent), &newSelOffset);
res = mHTMLEditor->RemoveStyleInside(leftNode, item->tag, &(item->attr));
if (NS_FAILED(res)) return res;
NS_ENSURE_SUCCESS(res, res);
}
// reset our node offset values to the resulting new sel point
node = newSelParent;
offset = newSelOffset;
}
// we own item now (TakeClearProperty hands ownership to us)
delete item;
mHTMLEditor->mTypeInState->TakeClearProperty(&item);
mHTMLEditor->mTypeInState->TakeClearProperty(getter_Transfers(item));
weDidSometing = PR_TRUE;
}
@ -4438,9 +4436,9 @@ nsHTMLEditRules::CreateStyleForInsertText(nsISelection *aSelection, nsIDOMDocume
PRInt32 relFontSize;
res = mHTMLEditor->mTypeInState->TakeRelativeFontSize(&relFontSize);
if (NS_FAILED(res)) return res;
res = mHTMLEditor->mTypeInState->TakeSetProperty(&item);
if (NS_FAILED(res)) return res;
NS_ENSURE_SUCCESS(res, res);
res = mHTMLEditor->mTypeInState->TakeSetProperty(getter_Transfers(item));
NS_ENSURE_SUCCESS(res, res);
if (item || relFontSize) // we have at least one style to add; make a
{ // new text node to insert style nodes above.
@ -4448,18 +4446,18 @@ nsHTMLEditRules::CreateStyleForInsertText(nsISelection *aSelection, nsIDOMDocume
{
// if we are in a text node, split it
res = mHTMLEditor->SplitNodeDeep(node, node, offset, &offset);
if (NS_FAILED(res)) return res;
NS_ENSURE_SUCCESS(res, res);
node->GetParentNode(getter_AddRefs(tmp));
node = tmp;
}
nsCOMPtr<nsIDOMNode> newNode;
nsCOMPtr<nsIDOMText> nodeAsText;
res = aDoc->CreateTextNode(EmptyString(), getter_AddRefs(nodeAsText));
if (NS_FAILED(res)) return res;
NS_ENSURE_SUCCESS(res, res);
if (!nodeAsText) return NS_ERROR_NULL_POINTER;
newNode = do_QueryInterface(nodeAsText);
res = mHTMLEditor->InsertNode(newNode, node, offset);
if (NS_FAILED(res)) return res;
NS_ENSURE_SUCCESS(res, res);
node = newNode;
offset = 0;
weDidSometing = PR_TRUE;
@ -4473,17 +4471,15 @@ nsHTMLEditRules::CreateStyleForInsertText(nsISelection *aSelection, nsIDOMDocume
for (j=0; j<abs(relFontSize); j++)
{
res = mHTMLEditor->RelativeFontChangeOnTextNode(dir, nodeAsText, 0, -1);
if (NS_FAILED(res)) return res;
NS_ENSURE_SUCCESS(res, res);
}
}
while (item)
{
res = mHTMLEditor->SetInlinePropertyOnNode(node, item->tag, &item->attr, &item->value);
if (NS_FAILED(res)) return res;
// we own item now (TakeSetProperty hands ownership to us)
delete item;
mHTMLEditor->mTypeInState->TakeSetProperty(&item);
NS_ENSURE_SUCCESS(res, res);
mHTMLEditor->mTypeInState->TakeSetProperty(getter_Transfers(item));
}
}
if (weDidSometing)

View File

@ -58,10 +58,18 @@ struct StyleCache : public PropItem
{
PRBool mPresent;
StyleCache() : PropItem(nsnull, EmptyString(), EmptyString()), mPresent(PR_FALSE){}
StyleCache() : PropItem(), mPresent(PR_FALSE) {
MOZ_COUNT_CTOR(StyleCache);
}
StyleCache(nsIAtom *aTag, const nsAString &aAttr, const nsAString &aValue) :
PropItem(aTag, aAttr, aValue), mPresent(PR_FALSE) {}
~StyleCache() {}
PropItem(aTag, aAttr, aValue), mPresent(PR_FALSE) {
MOZ_COUNT_CTOR(StyleCache);
}
~StyleCache() {
MOZ_COUNT_DTOR(StyleCache);
}
};