Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Unified Diff: ui/gfx/codec/png_codec.cc

Issue 9959061: Use RAII for write libpng structs. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/codec/png_codec.cc
===================================================================
--- ui/gfx/codec/png_codec.cc (revision 130345)
+++ ui/gfx/codec/png_codec.cc (working copy)
@@ -359,8 +359,27 @@
private:
png_struct** ps_;
png_info** pi_;
+ DISALLOW_COPY_AND_ASSIGN(PngReadStructDestroyer);
};
+// Automatically destroys the given write structs on destruction to make
+// cleanup and error handling code cleaner.
+class PngWriteStructDestroyer {
+ public:
+ explicit PngWriteStructDestroyer(png_struct** ps) : ps_(ps), pi_(0) {
+ }
+ ~PngWriteStructDestroyer() {
+ png_destroy_write_struct(ps_, pi_);
+ }
+ void SetInfoStruct(png_info** pi) {
+ pi_ = pi;
+ }
+ private:
+ png_struct** ps_;
+ png_info** pi_;
+ DISALLOW_COPY_AND_ASSIGN(PngWriteStructDestroyer);
+};
+
bool BuildPNGStruct(const unsigned char* input, size_t input_size,
png_struct** png_ptr, png_info** info_ptr) {
if (input_size < 8)
@@ -746,18 +765,17 @@
NULL, NULL, NULL);
if (!png_ptr)
return false;
+ PngWriteStructDestroyer destroyer(&png_ptr);
png_info* info_ptr = png_create_info_struct(png_ptr);
- if (!info_ptr) {
- png_destroy_write_struct(&png_ptr, NULL);
+ if (!info_ptr)
return false;
- }
+ destroyer.SetInfoStruct(&info_ptr);
PngEncoderState state(output);
bool success = DoLibpngWrite(png_ptr, info_ptr, &state,
size.width(), size.height(), row_byte_width,
input, compression_level, png_output_color_type,
output_color_components, converter, comments);
- png_destroy_write_struct(&png_ptr, &info_ptr);
return success;
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698