David PHAM-VAN

Fix Windows memory leaks

@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 ## 3.7.1 3 ## 3.7.1
4 4
5 - Fix Pdf Raster on WEB 5 - Fix Pdf Raster on WEB
  6 +- Fix Windows memory leaks
6 7
7 ## 3.7.0 8 ## 3.7.0
8 9
@@ -37,7 +37,7 @@ void PrintJob::directPrintPdf(std::string name, @@ -37,7 +37,7 @@ void PrintJob::directPrintPdf(std::string name,
37 std::vector<uint8_t> data, 37 std::vector<uint8_t> data,
38 std::string withPrinter) {} 38 std::string withPrinter) {}
39 39
40 -void PrintJob::printPdf(std::string name) { 40 +bool PrintJob::printPdf(std::string name) {
41 PRINTDLG pd; 41 PRINTDLG pd;
42 // HWND hwnd; 42 // HWND hwnd;
43 43
@@ -60,7 +60,7 @@ void PrintJob::printPdf(std::string name) { @@ -60,7 +60,7 @@ void PrintJob::printPdf(std::string name) {
60 auto r = PrintDlg(&pd); 60 auto r = PrintDlg(&pd);
61 61
62 if (r == 1) { 62 if (r == 1) {
63 - printf("ncopies: %d\n", pd.nCopies); 63 + // printf("ncopies: %d\n", pd.nCopies);
64 // printf("hDevMode: %d\n", (int)pd.hDevMode); 64 // printf("hDevMode: %d\n", (int)pd.hDevMode);
65 65
66 // DEVMODE* b = static_cast<DEVMODE*>(GlobalLock(pd.hDevMode)); 66 // DEVMODE* b = static_cast<DEVMODE*>(GlobalLock(pd.hDevMode));
@@ -89,24 +89,27 @@ void PrintJob::printPdf(std::string name) { @@ -89,24 +89,27 @@ void PrintJob::printPdf(std::string name) {
89 auto marginRight = pageWidth - printableWidth - marginLeft; 89 auto marginRight = pageWidth - printableWidth - marginLeft;
90 auto marginBottom = pageHeight - printableHeight - marginTop; 90 auto marginBottom = pageHeight - printableHeight - marginTop;
91 91
92 - printf("dpiX: %f\n", dpiX);  
93 - printf("HORZRES: %d\n", GetDeviceCaps(pd.hDC, HORZRES));  
94 - printf("PHYSICALOFFSETX: %d\n", GetDeviceCaps(pd.hDC, PHYSICALOFFSETX));  
95 - printf("pageWidth: %f\n", pageWidth); 92 + // printf("dpiX: %f\n", dpiX);
  93 + // printf("HORZRES: %d\n", GetDeviceCaps(pd.hDC, HORZRES));
  94 + // printf("PHYSICALOFFSETX: %d\n", GetDeviceCaps(pd.hDC, PHYSICALOFFSETX));
  95 + // printf("pageWidth: %f\n", pageWidth);
96 96
97 hDC = pd.hDC; 97 hDC = pd.hDC;
98 hDevMode = pd.hDevMode; 98 hDevMode = pd.hDevMode;
99 hDevNames = pd.hDevNames; 99 hDevNames = pd.hDevNames;
100 100
101 - printf("HDC: %llu job: %d\n", (size_t)pd.hDC, index); 101 + // printf("HDC: %llu job: %d\n", (size_t)pd.hDC, index);
102 102
103 printing->onLayout(this, pageWidth, pageHeight, marginLeft, marginTop, 103 printing->onLayout(this, pageWidth, pageHeight, marginLeft, marginTop,
104 marginRight, marginBottom); 104 marginRight, marginBottom);
  105 + return true;
105 } 106 }
  107 +
  108 + return false;
106 } 109 }
107 110
108 void PrintJob::writeJob(std::vector<uint8_t> data) { 111 void PrintJob::writeJob(std::vector<uint8_t> data) {
109 - printf("hDC: %llu job: %d\n", (size_t)hDC, index); 112 + // printf("hDC: %llu job: %d\n", (size_t)hDC, index);
110 auto dpiX = static_cast<double>(GetDeviceCaps(hDC, LOGPIXELSX)) / 72; 113 auto dpiX = static_cast<double>(GetDeviceCaps(hDC, LOGPIXELSX)) / 72;
111 auto dpiY = static_cast<double>(GetDeviceCaps(hDC, LOGPIXELSY)) / 72; 114 auto dpiY = static_cast<double>(GetDeviceCaps(hDC, LOGPIXELSY)) / 72;
112 115
@@ -141,16 +144,16 @@ void PrintJob::writeJob(std::vector<uint8_t> data) { @@ -141,16 +144,16 @@ void PrintJob::writeJob(std::vector<uint8_t> data) {
141 144
142 auto doc = FPDF_LoadMemDocument64(data.data(), data.size(), nullptr); 145 auto doc = FPDF_LoadMemDocument64(data.data(), data.size(), nullptr);
143 if (!doc) { 146 if (!doc) {
144 - printf("Error loading the document: %d\n", FPDF_GetLastError()); 147 + // printf("Error loading the document: %d\n", FPDF_GetLastError());
145 return; 148 return;
146 } 149 }
147 150
148 auto pages = FPDF_GetPageCount(doc); 151 auto pages = FPDF_GetPageCount(doc);
149 - printf("Page count: %d\n", pages); 152 + // printf("Page count: %d\n", pages);
150 153
151 for (auto pageNum = 0; pageNum < pages; pageNum++) { 154 for (auto pageNum = 0; pageNum < pages; pageNum++) {
152 r = StartPage(hDC); 155 r = StartPage(hDC);
153 - printf("StartPage = %d\n", r); 156 + // printf("StartPage = %d\n", r);
154 157
155 auto page = FPDF_LoadPage(doc, pageNum); 158 auto page = FPDF_LoadPage(doc, pageNum);
156 // print(FPDF_GetLastError()); 159 // print(FPDF_GetLastError());
@@ -160,23 +163,23 @@ void PrintJob::writeJob(std::vector<uint8_t> data) { @@ -160,23 +163,23 @@ void PrintJob::writeJob(std::vector<uint8_t> data) {
160 163
161 // print('$width x $height'); 164 // print('$width x $height');
162 165
163 - printf("pdfWidth: %f dpiX: %f \n", pdfWidth, dpiX); 166 + // printf("pdfWidth: %f dpiX: %f \n", pdfWidth, dpiX);
164 int bWidth = static_cast<int>(pdfWidth * dpiX); 167 int bWidth = static_cast<int>(pdfWidth * dpiX);
165 int bHeight = static_cast<int>(pdfHeight * dpiY); 168 int bHeight = static_cast<int>(pdfHeight * dpiY);
166 169
167 - printf("bwidth/bheight: %d x %d\n", bWidth, bHeight); 170 + // printf("bwidth/bheight: %d x %d\n", bWidth, bHeight);
168 171
169 FPDF_RenderPage(hDC, page, 0, 0, bWidth, bHeight, 0, FPDF_ANNOT); 172 FPDF_RenderPage(hDC, page, 0, 0, bWidth, bHeight, 0, FPDF_ANNOT);
170 173
171 r = EndPage(hDC); 174 r = EndPage(hDC);
172 - printf("EndPage = %d\n", r); 175 + // printf("EndPage = %d\n", r);
173 } 176 }
174 177
175 FPDF_CloseDocument(doc); 178 FPDF_CloseDocument(doc);
176 FPDF_DestroyLibrary(); 179 FPDF_DestroyLibrary();
177 180
178 r = EndDoc(hDC); 181 r = EndDoc(hDC);
179 - printf("EndDoc = %d\n", r); 182 + // printf("EndDoc = %d\n", r);
180 // DeleteDC(printerDC); 183 // DeleteDC(printerDC);
181 DeleteDC(hDC); 184 DeleteDC(hDC);
182 GlobalFree(hDevNames); 185 GlobalFree(hDevNames);
@@ -241,13 +244,13 @@ void PrintJob::rasterPdf(std::vector<uint8_t> data, @@ -241,13 +244,13 @@ void PrintJob::rasterPdf(std::vector<uint8_t> data,
241 244
242 auto doc = FPDF_LoadMemDocument64(data.data(), data.size(), nullptr); 245 auto doc = FPDF_LoadMemDocument64(data.data(), data.size(), nullptr);
243 if (!doc) { 246 if (!doc) {
244 - fprintf(stderr, "Error: %d\n", FPDF_GetLastError()); 247 + // printf(stderr, "Error: %d\n", FPDF_GetLastError());
245 printing->onPageRasterEnd(this); 248 printing->onPageRasterEnd(this);
246 return; 249 return;
247 } 250 }
248 251
249 auto pageCount = FPDF_GetPageCount(doc); 252 auto pageCount = FPDF_GetPageCount(doc);
250 - printf("pdf: pages:%d\n", pageCount); 253 + // printf("pdf: pages:%d\n", pageCount);
251 254
252 if (pages.size() == 0) { 255 if (pages.size() == 0) {
253 // Use all pages 256 // Use all pages
@@ -262,14 +265,14 @@ void PrintJob::rasterPdf(std::vector<uint8_t> data, @@ -262,14 +265,14 @@ void PrintJob::rasterPdf(std::vector<uint8_t> data,
262 265
263 auto page = FPDF_LoadPage(doc, n); 266 auto page = FPDF_LoadPage(doc, n);
264 if (!page) { 267 if (!page) {
265 - printf("Page Error: %d\n", FPDF_GetLastError()); 268 + // printf("Page Error: %d\n", FPDF_GetLastError());
266 continue; 269 continue;
267 } 270 }
268 271
269 auto width = FPDF_GetPageWidth(page); 272 auto width = FPDF_GetPageWidth(page);
270 auto height = FPDF_GetPageHeight(page); 273 auto height = FPDF_GetPageHeight(page);
271 274
272 - printf("pdf: page:%d w:%f h:%f\n", n, width, height); 275 + // printf("pdf: page:%d w:%f h:%f\n", n, width, height);
273 276
274 auto bWidth = static_cast<int>(width * scale); 277 auto bWidth = static_cast<int>(width * scale);
275 auto bHeight = static_cast<int>(height * scale); 278 auto bHeight = static_cast<int>(height * scale);
@@ -41,7 +41,7 @@ class PrintJob { @@ -41,7 +41,7 @@ class PrintJob {
41 public: 41 public:
42 PrintJob(Printing* printing, int index); 42 PrintJob(Printing* printing, int index);
43 43
44 - ~PrintJob() { printf("Delete PrintJob #%d\n", index); } 44 + // ~PrintJob() { printf("Delete PrintJob #%d\n", index); }
45 45
46 int id() { return index; } 46 int id() { return index; }
47 47
@@ -49,7 +49,7 @@ class PrintJob { @@ -49,7 +49,7 @@ class PrintJob {
49 std::vector<uint8_t> data, 49 std::vector<uint8_t> data,
50 std::string withPrinter); 50 std::string withPrinter);
51 51
52 - void printPdf(std::string name); 52 + bool printPdf(std::string name);
53 53
54 void writeJob(std::vector<uint8_t> data); 54 void writeJob(std::vector<uint8_t> data);
55 55
@@ -26,14 +26,6 @@ Printing::Printing() {} @@ -26,14 +26,6 @@ Printing::Printing() {}
26 26
27 Printing::~Printing() {} 27 Printing::~Printing() {}
28 28
29 -PrintJob* Printing::createJob(int num) {  
30 - return new PrintJob{this, num};  
31 -}  
32 -  
33 -void Printing::remove(PrintJob* job) {  
34 - delete job;  
35 -}  
36 -  
37 void Printing::onPageRasterized(std::vector<uint8_t> data, 29 void Printing::onPageRasterized(std::vector<uint8_t> data,
38 int width, 30 int width,
39 int height, 31 int height,
@@ -63,44 +55,32 @@ void Printing::onPageRasterEnd(PrintJob* job) { @@ -63,44 +55,32 @@ void Printing::onPageRasterEnd(PrintJob* job) {
63 55
64 class OnLayoutResult : public flutter::MethodResult<flutter::EncodableValue> { 56 class OnLayoutResult : public flutter::MethodResult<flutter::EncodableValue> {
65 public: 57 public:
66 - OnLayoutResult(PrintJob* job_) : job(job_) {  
67 - n = 90;  
68 - printf("OnLayoutResult (%d) %p\n", job->id(), this);  
69 - }  
70 -  
71 - OnLayoutResult(const OnLayoutResult& other) {  
72 - job = other.job;  
73 - printf("OnLayoutResult copy (%d) %p\n", job->id(), this);  
74 - }  
75 -  
76 - OnLayoutResult(const OnLayoutResult&& other) {  
77 - job = other.job;  
78 - printf("OnLayoutResult move (%d) %p\n", job->id(), this);  
79 - }  
80 -  
81 - ~OnLayoutResult() {  
82 - printf("OnLayoutResult delete (%d) %p\n", job->id(), this); 58 + OnLayoutResult(PrintJob* job) : job(job) {
  59 + // printf("OnLayoutResult (%d) %p\n", job->id(), this);
83 } 60 }
84 61
85 private: 62 private:
86 PrintJob* job; 63 PrintJob* job;
87 - int n = 0;  
88 64
89 protected: 65 protected:
90 void SuccessInternal(const flutter::EncodableValue* result) { 66 void SuccessInternal(const flutter::EncodableValue* result) {
91 auto doc = std::get<std::vector<uint8_t>>(*result); 67 auto doc = std::get<std::vector<uint8_t>>(*result);
92 - printf("Success! n:%d (%d) %llu bytes %p\n", n, job->id(), doc.size(),  
93 - this); 68 + // printf("Success! (%d) %llu bytes %p\n", job->id(), doc.size(), this);
94 job->writeJob(doc); 69 job->writeJob(doc);
  70 + delete job;
95 } 71 }
96 72
97 void ErrorInternal(const std::string& error_code, 73 void ErrorInternal(const std::string& error_code,
98 const std::string& error_message, 74 const std::string& error_message,
99 const flutter::EncodableValue* error_details) { 75 const flutter::EncodableValue* error_details) {
100 printf("Error!\n"); 76 printf("Error!\n");
  77 + delete job;
101 } 78 }
102 79
103 - void NotImplementedInternal() { printf("NotImplemented!\n"); } 80 + void NotImplementedInternal() {
  81 + printf("NotImplemented!\n");
  82 + delete job;
  83 + }
104 }; 84 };
105 85
106 void Printing::onLayout(PrintJob* job, 86 void Printing::onLayout(PrintJob* job,
@@ -110,8 +90,9 @@ void Printing::onLayout(PrintJob* job, @@ -110,8 +90,9 @@ void Printing::onLayout(PrintJob* job,
110 double marginTop, 90 double marginTop,
111 double marginRight, 91 double marginRight,
112 double marginBottom) { 92 double marginBottom) {
113 - printf("onLayout (%d) %fx%f %f %f %f %f\n", job->id(), pageWidth, pageHeight,  
114 - marginLeft, marginTop, marginRight, marginBottom); 93 + // printf("onLayout (%d) %fx%f %f %f %f %f\n", job->id(), pageWidth,
  94 + // pageHeight,
  95 + // marginLeft, marginTop, marginRight, marginBottom);
115 96
116 // auto result = std::make_unique<OnLayoutResult>(job); 97 // auto result = std::make_unique<OnLayoutResult>(job);
117 98
@@ -37,10 +37,6 @@ class Printing { @@ -37,10 +37,6 @@ class Printing {
37 37
38 virtual ~Printing(); 38 virtual ~Printing();
39 39
40 - PrintJob* createJob(int num);  
41 -  
42 - void remove(PrintJob* job);  
43 -  
44 void onPageRasterized(std::vector<uint8_t> data, 40 void onPageRasterized(std::vector<uint8_t> data,
45 int width, 41 int width,
46 int height, 42 int height,
@@ -71,14 +71,15 @@ class PrintingPlugin : public flutter::Plugin { @@ -71,14 +71,15 @@ class PrintingPlugin : public flutter::Plugin {
71 : std::string{}; 71 : std::string{};
72 auto vJob = arguments->find(flutter::EncodableValue("job")); 72 auto vJob = arguments->find(flutter::EncodableValue("job"));
73 auto jobNum = vJob != arguments->end() ? std::get<int>(vJob->second) : -1; 73 auto jobNum = vJob != arguments->end() ? std::get<int>(vJob->second) : -1;
74 - auto job = printing.createJob(jobNum);  
75 - job->printPdf(name);  
76 - // printing.remove(job);  
77 - result->Success(flutter::EncodableValue(1)); 74 + auto job = new PrintJob{&printing, jobNum};
  75 + auto res = job->printPdf(name);
  76 + if (!res) {
  77 + delete job;
  78 + }
  79 + result->Success(flutter::EncodableValue(res ? 1 : 0));
78 } else if (method_call.method_name().compare("directPrintPdf") == 0) { 80 } else if (method_call.method_name().compare("directPrintPdf") == 0) {
79 - auto job = printing.createJob(-1); 81 + auto job = std::make_unique<PrintJob>(&printing, -1);
80 job->directPrintPdf("name", std::vector<uint8_t>{}, "withPrinter"); 82 job->directPrintPdf("name", std::vector<uint8_t>{}, "withPrinter");
81 - printing.remove(job);  
82 result->Success(nullptr); 83 result->Success(nullptr);
83 } else if (method_call.method_name().compare("sharePdf") == 0) { 84 } else if (method_call.method_name().compare("sharePdf") == 0) {
84 const auto* arguments = 85 const auto* arguments =
@@ -91,14 +92,12 @@ class PrintingPlugin : public flutter::Plugin { @@ -91,14 +92,12 @@ class PrintingPlugin : public flutter::Plugin {
91 auto doc = vDoc != arguments->end() 92 auto doc = vDoc != arguments->end()
92 ? std::get<std::vector<uint8_t>>(vDoc->second) 93 ? std::get<std::vector<uint8_t>>(vDoc->second)
93 : std::vector<uint8_t>{}; 94 : std::vector<uint8_t>{};
94 - auto job = printing.createJob(-1); 95 + auto job = std::make_unique<PrintJob>(&printing, -1);
95 auto res = job->sharePdf(doc, name); 96 auto res = job->sharePdf(doc, name);
96 - printing.remove(job);  
97 result->Success(flutter::EncodableValue(res ? 1 : 0)); 97 result->Success(flutter::EncodableValue(res ? 1 : 0));
98 } else if (method_call.method_name().compare("pickPrinter") == 0) { 98 } else if (method_call.method_name().compare("pickPrinter") == 0) {
99 - auto job = printing.createJob(-1); 99 + auto job = std::make_unique<PrintJob>(&printing, -1);
100 job->pickPrinter(nullptr); 100 job->pickPrinter(nullptr);
101 - printing.remove(job);  
102 result->Success(nullptr); 101 result->Success(nullptr);
103 } else if (method_call.method_name().compare("rasterPdf") == 0) { 102 } else if (method_call.method_name().compare("rasterPdf") == 0) {
104 const auto* arguments = 103 const auto* arguments =
@@ -117,19 +116,17 @@ class PrintingPlugin : public flutter::Plugin { @@ -117,19 +116,17 @@ class PrintingPlugin : public flutter::Plugin {
117 vScale != arguments->end() ? std::get<double>(vScale->second) : 1; 116 vScale != arguments->end() ? std::get<double>(vScale->second) : 1;
118 auto vJob = arguments->find(flutter::EncodableValue("job")); 117 auto vJob = arguments->find(flutter::EncodableValue("job"));
119 auto jobNum = vJob != arguments->end() ? std::get<int>(vJob->second) : -1; 118 auto jobNum = vJob != arguments->end() ? std::get<int>(vJob->second) : -1;
120 - auto job = printing.createJob(jobNum); 119 + auto job = std::make_unique<PrintJob>(&printing, jobNum);
121 job->rasterPdf(doc, std::vector<int>{}, scale); 120 job->rasterPdf(doc, std::vector<int>{}, scale);
122 result->Success(nullptr); 121 result->Success(nullptr);
123 - printing.remove(job);  
124 } else if (method_call.method_name().compare("printingInfo") == 0) { 122 } else if (method_call.method_name().compare("printingInfo") == 0) {
125 - auto job = printing.createJob(-1); 123 + auto job = std::make_unique<PrintJob>(&printing, -1);
126 auto map = flutter::EncodableMap{}; 124 auto map = flutter::EncodableMap{};
127 for (auto item : job->printingInfo()) { 125 for (auto item : job->printingInfo()) {
128 map[flutter::EncodableValue(item.first)] = 126 map[flutter::EncodableValue(item.first)] =
129 flutter::EncodableValue(item.second); 127 flutter::EncodableValue(item.second);
130 } 128 }
131 result->Success(map); 129 result->Success(map);
132 - printing.remove(job);  
133 } else { 130 } else {
134 result->NotImplemented(); 131 result->NotImplemented();
135 } 132 }