David PHAM-VAN

Fix Windows memory leaks

... ... @@ -3,6 +3,7 @@
## 3.7.1
- Fix Pdf Raster on WEB
- Fix Windows memory leaks
## 3.7.0
... ...
... ... @@ -37,7 +37,7 @@ void PrintJob::directPrintPdf(std::string name,
std::vector<uint8_t> data,
std::string withPrinter) {}
void PrintJob::printPdf(std::string name) {
bool PrintJob::printPdf(std::string name) {
PRINTDLG pd;
// HWND hwnd;
... ... @@ -60,7 +60,7 @@ void PrintJob::printPdf(std::string name) {
auto r = PrintDlg(&pd);
if (r == 1) {
printf("ncopies: %d\n", pd.nCopies);
// printf("ncopies: %d\n", pd.nCopies);
// printf("hDevMode: %d\n", (int)pd.hDevMode);
// DEVMODE* b = static_cast<DEVMODE*>(GlobalLock(pd.hDevMode));
... ... @@ -89,24 +89,27 @@ void PrintJob::printPdf(std::string name) {
auto marginRight = pageWidth - printableWidth - marginLeft;
auto marginBottom = pageHeight - printableHeight - marginTop;
printf("dpiX: %f\n", dpiX);
printf("HORZRES: %d\n", GetDeviceCaps(pd.hDC, HORZRES));
printf("PHYSICALOFFSETX: %d\n", GetDeviceCaps(pd.hDC, PHYSICALOFFSETX));
printf("pageWidth: %f\n", pageWidth);
// printf("dpiX: %f\n", dpiX);
// printf("HORZRES: %d\n", GetDeviceCaps(pd.hDC, HORZRES));
// printf("PHYSICALOFFSETX: %d\n", GetDeviceCaps(pd.hDC, PHYSICALOFFSETX));
// printf("pageWidth: %f\n", pageWidth);
hDC = pd.hDC;
hDevMode = pd.hDevMode;
hDevNames = pd.hDevNames;
printf("HDC: %llu job: %d\n", (size_t)pd.hDC, index);
// printf("HDC: %llu job: %d\n", (size_t)pd.hDC, index);
printing->onLayout(this, pageWidth, pageHeight, marginLeft, marginTop,
marginRight, marginBottom);
return true;
}
return false;
}
void PrintJob::writeJob(std::vector<uint8_t> data) {
printf("hDC: %llu job: %d\n", (size_t)hDC, index);
// printf("hDC: %llu job: %d\n", (size_t)hDC, index);
auto dpiX = static_cast<double>(GetDeviceCaps(hDC, LOGPIXELSX)) / 72;
auto dpiY = static_cast<double>(GetDeviceCaps(hDC, LOGPIXELSY)) / 72;
... ... @@ -141,16 +144,16 @@ void PrintJob::writeJob(std::vector<uint8_t> data) {
auto doc = FPDF_LoadMemDocument64(data.data(), data.size(), nullptr);
if (!doc) {
printf("Error loading the document: %d\n", FPDF_GetLastError());
// printf("Error loading the document: %d\n", FPDF_GetLastError());
return;
}
auto pages = FPDF_GetPageCount(doc);
printf("Page count: %d\n", pages);
// printf("Page count: %d\n", pages);
for (auto pageNum = 0; pageNum < pages; pageNum++) {
r = StartPage(hDC);
printf("StartPage = %d\n", r);
// printf("StartPage = %d\n", r);
auto page = FPDF_LoadPage(doc, pageNum);
// print(FPDF_GetLastError());
... ... @@ -160,23 +163,23 @@ void PrintJob::writeJob(std::vector<uint8_t> data) {
// print('$width x $height');
printf("pdfWidth: %f dpiX: %f \n", pdfWidth, dpiX);
// printf("pdfWidth: %f dpiX: %f \n", pdfWidth, dpiX);
int bWidth = static_cast<int>(pdfWidth * dpiX);
int bHeight = static_cast<int>(pdfHeight * dpiY);
printf("bwidth/bheight: %d x %d\n", bWidth, bHeight);
// printf("bwidth/bheight: %d x %d\n", bWidth, bHeight);
FPDF_RenderPage(hDC, page, 0, 0, bWidth, bHeight, 0, FPDF_ANNOT);
r = EndPage(hDC);
printf("EndPage = %d\n", r);
// printf("EndPage = %d\n", r);
}
FPDF_CloseDocument(doc);
FPDF_DestroyLibrary();
r = EndDoc(hDC);
printf("EndDoc = %d\n", r);
// printf("EndDoc = %d\n", r);
// DeleteDC(printerDC);
DeleteDC(hDC);
GlobalFree(hDevNames);
... ... @@ -241,13 +244,13 @@ void PrintJob::rasterPdf(std::vector<uint8_t> data,
auto doc = FPDF_LoadMemDocument64(data.data(), data.size(), nullptr);
if (!doc) {
fprintf(stderr, "Error: %d\n", FPDF_GetLastError());
// printf(stderr, "Error: %d\n", FPDF_GetLastError());
printing->onPageRasterEnd(this);
return;
}
auto pageCount = FPDF_GetPageCount(doc);
printf("pdf: pages:%d\n", pageCount);
// printf("pdf: pages:%d\n", pageCount);
if (pages.size() == 0) {
// Use all pages
... ... @@ -262,14 +265,14 @@ void PrintJob::rasterPdf(std::vector<uint8_t> data,
auto page = FPDF_LoadPage(doc, n);
if (!page) {
printf("Page Error: %d\n", FPDF_GetLastError());
// printf("Page Error: %d\n", FPDF_GetLastError());
continue;
}
auto width = FPDF_GetPageWidth(page);
auto height = FPDF_GetPageHeight(page);
printf("pdf: page:%d w:%f h:%f\n", n, width, height);
// printf("pdf: page:%d w:%f h:%f\n", n, width, height);
auto bWidth = static_cast<int>(width * scale);
auto bHeight = static_cast<int>(height * scale);
... ...
... ... @@ -41,7 +41,7 @@ class PrintJob {
public:
PrintJob(Printing* printing, int index);
~PrintJob() { printf("Delete PrintJob #%d\n", index); }
// ~PrintJob() { printf("Delete PrintJob #%d\n", index); }
int id() { return index; }
... ... @@ -49,7 +49,7 @@ class PrintJob {
std::vector<uint8_t> data,
std::string withPrinter);
void printPdf(std::string name);
bool printPdf(std::string name);
void writeJob(std::vector<uint8_t> data);
... ...
... ... @@ -26,14 +26,6 @@ Printing::Printing() {}
Printing::~Printing() {}
PrintJob* Printing::createJob(int num) {
return new PrintJob{this, num};
}
void Printing::remove(PrintJob* job) {
delete job;
}
void Printing::onPageRasterized(std::vector<uint8_t> data,
int width,
int height,
... ... @@ -63,44 +55,32 @@ void Printing::onPageRasterEnd(PrintJob* job) {
class OnLayoutResult : public flutter::MethodResult<flutter::EncodableValue> {
public:
OnLayoutResult(PrintJob* job_) : job(job_) {
n = 90;
printf("OnLayoutResult (%d) %p\n", job->id(), this);
}
OnLayoutResult(const OnLayoutResult& other) {
job = other.job;
printf("OnLayoutResult copy (%d) %p\n", job->id(), this);
}
OnLayoutResult(const OnLayoutResult&& other) {
job = other.job;
printf("OnLayoutResult move (%d) %p\n", job->id(), this);
}
~OnLayoutResult() {
printf("OnLayoutResult delete (%d) %p\n", job->id(), this);
OnLayoutResult(PrintJob* job) : job(job) {
// printf("OnLayoutResult (%d) %p\n", job->id(), this);
}
private:
PrintJob* job;
int n = 0;
protected:
void SuccessInternal(const flutter::EncodableValue* result) {
auto doc = std::get<std::vector<uint8_t>>(*result);
printf("Success! n:%d (%d) %llu bytes %p\n", n, job->id(), doc.size(),
this);
// printf("Success! (%d) %llu bytes %p\n", job->id(), doc.size(), this);
job->writeJob(doc);
delete job;
}
void ErrorInternal(const std::string& error_code,
const std::string& error_message,
const flutter::EncodableValue* error_details) {
printf("Error!\n");
delete job;
}
void NotImplementedInternal() { printf("NotImplemented!\n"); }
void NotImplementedInternal() {
printf("NotImplemented!\n");
delete job;
}
};
void Printing::onLayout(PrintJob* job,
... ... @@ -110,8 +90,9 @@ void Printing::onLayout(PrintJob* job,
double marginTop,
double marginRight,
double marginBottom) {
printf("onLayout (%d) %fx%f %f %f %f %f\n", job->id(), pageWidth, pageHeight,
marginLeft, marginTop, marginRight, marginBottom);
// printf("onLayout (%d) %fx%f %f %f %f %f\n", job->id(), pageWidth,
// pageHeight,
// marginLeft, marginTop, marginRight, marginBottom);
// auto result = std::make_unique<OnLayoutResult>(job);
... ...
... ... @@ -37,10 +37,6 @@ class Printing {
virtual ~Printing();
PrintJob* createJob(int num);
void remove(PrintJob* job);
void onPageRasterized(std::vector<uint8_t> data,
int width,
int height,
... ...
... ... @@ -71,14 +71,15 @@ class PrintingPlugin : public flutter::Plugin {
: std::string{};
auto vJob = arguments->find(flutter::EncodableValue("job"));
auto jobNum = vJob != arguments->end() ? std::get<int>(vJob->second) : -1;
auto job = printing.createJob(jobNum);
job->printPdf(name);
// printing.remove(job);
result->Success(flutter::EncodableValue(1));
auto job = new PrintJob{&printing, jobNum};
auto res = job->printPdf(name);
if (!res) {
delete job;
}
result->Success(flutter::EncodableValue(res ? 1 : 0));
} else if (method_call.method_name().compare("directPrintPdf") == 0) {
auto job = printing.createJob(-1);
auto job = std::make_unique<PrintJob>(&printing, -1);
job->directPrintPdf("name", std::vector<uint8_t>{}, "withPrinter");
printing.remove(job);
result->Success(nullptr);
} else if (method_call.method_name().compare("sharePdf") == 0) {
const auto* arguments =
... ... @@ -91,14 +92,12 @@ class PrintingPlugin : public flutter::Plugin {
auto doc = vDoc != arguments->end()
? std::get<std::vector<uint8_t>>(vDoc->second)
: std::vector<uint8_t>{};
auto job = printing.createJob(-1);
auto job = std::make_unique<PrintJob>(&printing, -1);
auto res = job->sharePdf(doc, name);
printing.remove(job);
result->Success(flutter::EncodableValue(res ? 1 : 0));
} else if (method_call.method_name().compare("pickPrinter") == 0) {
auto job = printing.createJob(-1);
auto job = std::make_unique<PrintJob>(&printing, -1);
job->pickPrinter(nullptr);
printing.remove(job);
result->Success(nullptr);
} else if (method_call.method_name().compare("rasterPdf") == 0) {
const auto* arguments =
... ... @@ -117,19 +116,17 @@ class PrintingPlugin : public flutter::Plugin {
vScale != arguments->end() ? std::get<double>(vScale->second) : 1;
auto vJob = arguments->find(flutter::EncodableValue("job"));
auto jobNum = vJob != arguments->end() ? std::get<int>(vJob->second) : -1;
auto job = printing.createJob(jobNum);
auto job = std::make_unique<PrintJob>(&printing, jobNum);
job->rasterPdf(doc, std::vector<int>{}, scale);
result->Success(nullptr);
printing.remove(job);
} else if (method_call.method_name().compare("printingInfo") == 0) {
auto job = printing.createJob(-1);
auto job = std::make_unique<PrintJob>(&printing, -1);
auto map = flutter::EncodableMap{};
for (auto item : job->printingInfo()) {
map[flutter::EncodableValue(item.first)] =
flutter::EncodableValue(item.second);
}
result->Success(map);
printing.remove(job);
} else {
result->NotImplemented();
}
... ...