Closed Bug 208704 Opened 21 years ago Closed 21 years ago

Memory leaks in DOM Inspector's PNG Encoder

Categories

(Other Applications :: DOM Inspector, defect)

defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: glennrp+bmo, Assigned: dwitte)

Details

(Keywords: memory-leak)

Attachments

(1 file, 3 obsolete files)

I think there's a memory leak in the DOM Inspector.  In inPNGEncoder.cpp, there
is no png_destroy_write_struct() to match the png_create_write_struct(). 
Immediately after the png_write_end() near line 101, the following line should
be added:

   png_destroy_write_struct(&pngStruct, &infoStruct);

See example.c that comes with the libpng distribution.

Glenn
Keywords: mlk
It looks like we also leak here:

 80   FILE *file = fopen(ToNewCString(str), "wb");
there's another unrelated string leak at
http://lxr.mozilla.org/seamonkey/source/extensions/inspector/base/src/inPNGEncoder.cpp#78:

 78   nsAutoString str;
 79   str.Assign(aURL);
 80   FILE *file = fopen(ToNewCString(str), "wb");

perhaps this should just be

      FILE *file = fopen(NS_LossyConvertUCS2toASCII(aURL).get(), "wb");

(presuming it really does want an ASCII string and not UTF8). caillon, wanna fix
that while you're in there?
ah caillon beat me to it ;)
There are some opportunities to improve the encoding speed of imPNGEncoder and
reduce its resource consumption.  Do you want those suggestions here or in a
separate bug?
dwitte: it seems doubtful to me that it really wants to open an url

that should really use OpenANSIFileDesc from nsILocalFile, to work with
non-ascii paths.
Glenn, please use a new bug for that.  Do you want to produce a patch for the leaks?
Summary: Memory leak in DOM Inspector's PNG Encoder → Memory leaks in DOM Inspector's PNG Encoder
Please patch both leaks.  and use diff -u
Sorry, the *file leak is a bit out of my territory.  I'd probably botch the fix.
Whoever does fix it can combine my one-liner with their fix.
Attached patch fix for both leaks (obsolete) — Splinter Review
switches over to nsILocalFile per biesi's request. includes the png writer leak
fix.
Attachment #125263 - Attachment is obsolete: true
Attached patch diff -w of same (obsolete) — Splinter Review
Comment on attachment 125276 [details] [diff] [review]
diff -w of same

is the localfile fu okay?
Attachment #125276 - Flags: review?(cbiesinger)
Comment on attachment 125276 [details] [diff] [review]
diff -w of same

o) please rename aURL to aPath, in the .idl and the .cpp, because it is no URL

o) please declare |rv| on the line where it is used

o) maybe consider changing aURL to be an |AString|

o) Is there a special reason you don't want to follow symlinks? (PR_FALSE for
second argument to NewLocalFile)

r=biesi on the patch with the above changes (except possibly point 3)

However, please get caillon to review this too, as he is the module owner.
Attachment #125276 - Flags: review?(cbiesinger) → review?(caillon)
> Is there a special reason you don't want to follow symlinks? (PR_FALSE for
> second argument to NewLocalFile)

thanks for catching that... I saw it was unused in the unix localfile impl, so I
didn't think it mattered - but the other impls do use it ;)

> However, please get caillon to review this too, as he is the module owner.

yeah, I know caillon's the mo, I just wanted you to look at the patch first
since you're the one who wanted it. fair's fair. ;)

thx for the review!
Comment on attachment 125275 [details] [diff] [review]
fix for both leaks

>+  nsresult rv;
>+  nsCOMPtr<nsILocalFile> localFile;
>+  FILE *file;
>+  rv = NS_NewLocalFile(nsDependentString(aURL), PR_FALSE, getter_AddRefs(localFile));

You know the drill.  Declare rv while assigning instead of way up above.

>+  if (NS_SUCCEEDED(rv))
>+    rv = localFile->OpenANSIFileDesc("wb", &file);

Please fix this to use braces (opening brace on the same line as the if), even
for one-liners.  Ad infinitum.


>+
>+  ReverseRGB(width, height, bits);
>+  PRUint8* rowPtr = bits;

Earlier, you use |Type *ident| and here it is |Type* ident|.  Consistency would
totally rock here.  I prefer putting the * on the identifier to avoid things
such as someone expecting |Type* foo, bar|; to be two pointers.

>+  for (PRUint32 row = 0; row < height; ++row) {
>+    png_write_row(pngStruct, rowPtr);
>+    rowPtr += width*3;

Nit: space out the multiplication's operands?


Biesi said r=biesi which is fine, go ahead and have at that, with the
additional nits I pointed out fixed.
Attachment #125275 - Flags: review+
Attached patch fix v2Splinter Review
ugh, so I looked at the js that calls this method, and it has an nsILocalFile
all along. so I changed the interface to take a localfile directly... does that
sound ok?

fixed nits in the rest of the file and cleaned up some #includes as well.
Attachment #125275 - Attachment is obsolete: true
Attachment #125276 - Attachment is obsolete: true
Attachment #125276 - Flags: review?(caillon)
Attachment #125427 - Flags: review?(caillon)
Comment on attachment 125427 [details] [diff] [review]
fix v2

caillon said r=biesi is fine.

(this is untested, btw... you're on windows right? any chance you could have a
quick poke?)
Attachment #125427 - Flags: review?(caillon) → review?(cbiesinger)
Comment on attachment 125427 [details] [diff] [review]
fix v2

ah, this sounds like an even better solution

I'm usually on linux... while I could test on windows, I don't currently have
an up-to-date and working build there, and have no time to make one currently,
so I'm sorry but I can't test the patch on windows
Attachment #125427 - Flags: review?(cbiesinger) → review+
Comment on attachment 125427 [details] [diff] [review]
fix v2

thanks biesi (np about the testing, i'll figure something out)

jst - any chance you can do the honors here? fixes a couple of leaks and cleans
up some stuff.
Attachment #125427 - Flags: superreview?(jst)
.
Assignee: caillon → dwitte
Comment on attachment 125427 [details] [diff] [review]
fix v2

sr=jst
Attachment #125427 - Flags: superreview?(jst) → superreview+
checked in.
Status: NEW → RESOLVED
Closed: 21 years ago
Resolution: --- → FIXED
Product: Core → Other Applications
QA Contact: timeless → dom-inspector
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: