如何在 visual studio 2017 中修复 'Unhandled exception at <memory location> in <program name>. Access violation writing location <memory location>.'
How to fix 'Unhandled exception at <memory location> in <program name>. Access violation writing location <memory location>.' in visual studio 2017
我正在编写代码以使用库 libxl 在 excel 中制作发票。我的代码创建一个文件,写入其中,然后将文件保存在项目文件夹中。
但是,在调试过程中,我看到 'Unhandled exception',在写入特定位置时存在访问冲突。异常实际上是这样说的 'Unhandled exception at 0x100437D7(libxl.dll) in BillingSoft.exe:0xC0000005: Access violation writing location 0x00C3DED4.'。
我已经阅读了许多文章和解决方案,但其中 none 似乎解决了我的问题。我尝试使用
为 "book" 分配 space
Book* book = (Book*)malloc(sizeof(book));
在 "book" 的声明之前,但它只是更改了位置值,并没有解决问题。
这是我的主要功能:
int main()
{
Book* book = xlCreateBook(); // xlCreateXMLBook() for xlsx
if (book)
{
Font* boldFont = book->addFont();
boldFont->setBold();
Font* titleFont = book->addFont();
titleFont->setName(L"Arial Black");
titleFont->setSize(16);
Format* titleFormat = book->addFormat();
if (titleFormat = nullptr)
{
titleFormat->setFont(titleFont);
}
Format* headerFormat = book->addFormat();
if (headerFormat = nullptr)
{
headerFormat->setAlignH(ALIGNH_CENTER);
headerFormat->setBorder(BORDERSTYLE_THIN);
headerFormat->setFont(boldFont);
headerFormat->setFillPattern(FILLPATTERN_SOLID);
headerFormat->setPatternForegroundColor(COLOR_TAN);
}
Format* descriptionFormat = book->addFormat();
if (descriptionFormat = nullptr)
{
descriptionFormat->setBorderLeft(BORDERSTYLE_THIN);
}
Format* amountFormat = book->addFormat();
if (amountFormat = nullptr)
{
amountFormat->setNumFormat(NUMFORMAT_CURRENCY_NEGBRA);
amountFormat->setBorderLeft(BORDERSTYLE_THIN);
amountFormat->setBorderRight(BORDERSTYLE_THIN);
}
Format* totalLabelFormat = book->addFormat();
if (totalLabelFormat = nullptr)
{
totalLabelFormat->setBorderTop(BORDERSTYLE_THIN);
totalLabelFormat->setAlignH(ALIGNH_RIGHT);
totalLabelFormat->setFont(boldFont);
}
Format* totalFormat = book->addFormat();
if (totalFormat = nullptr)
{
totalFormat->setNumFormat(NUMFORMAT_CURRENCY_NEGBRA);
totalFormat->setBorder(BORDERSTYLE_THIN);
totalFormat->setFont(boldFont);
totalFormat->setFillPattern(FILLPATTERN_SOLID);
totalFormat->setPatternForegroundColor(COLOR_YELLOW);
}
Format* signatureFormat = book->addFormat();
if (signatureFormat = nullptr)
{
signatureFormat->setAlignH(ALIGNH_CENTER);
signatureFormat->setBorderTop(BORDERSTYLE_THIN);
}
Sheet* sheet = book->addSheet(L"Sheet1");
if (sheet)
{
sheet->writeStr(2, 1, L"Invoice No. 3568", titleFormat);
sheet->writeStr(4, 1, L"Name: John Smith");
sheet->writeStr(5, 1, L"Address: San Ramon, CA 94583 USA");
sheet->writeStr(7, 1, L"Description", headerFormat);
sheet->writeStr(7, 2, L"Amount", headerFormat);
sheet->writeStr(8, 1, L"Ball-Point Pens", descriptionFormat);
sheet->writeNum(8, 2, 85, amountFormat);
sheet->writeStr(9, 1, L"T-Shirts", descriptionFormat);
sheet->writeNum(9, 2, 150, amountFormat);
sheet->writeStr(10, 1, L"Tea cups", descriptionFormat);
sheet->writeNum(10, 2, 45, amountFormat);
sheet->writeStr(11, 1, L"Total:", totalLabelFormat);
sheet->writeNum(11, 2, 280, totalFormat);
sheet->writeStr(14, 2, L"Signature", signatureFormat);
sheet->setCol(1, 1, 40);
sheet->setCol(2, 2, 15);
}
book->save(L"Invoice.xls"); // exception here!
book->release();
}
return 0;
}
我只想让程序正常运行,没有任何错误。请帮我解决我的问题。
任何帮助将不胜感激!
你总是在覆盖 Excel 给你的一切。所以当你这样做时:
sheet->writeStr(2, 1, L"Invoice No. 3568", titleFormat);
你传入 titleFormat
这是一个 nullptr
因为行:
if (titleFormat = nullptr)
打开警告并修复代码:
if (titleFormat != nullptr)
所有其他人也一样 if
。
我正在编写代码以使用库 libxl 在 excel 中制作发票。我的代码创建一个文件,写入其中,然后将文件保存在项目文件夹中。
但是,在调试过程中,我看到 'Unhandled exception',在写入特定位置时存在访问冲突。异常实际上是这样说的 'Unhandled exception at 0x100437D7(libxl.dll) in BillingSoft.exe:0xC0000005: Access violation writing location 0x00C3DED4.'。 我已经阅读了许多文章和解决方案,但其中 none 似乎解决了我的问题。我尝试使用
为 "book" 分配 space Book* book = (Book*)malloc(sizeof(book));
在 "book" 的声明之前,但它只是更改了位置值,并没有解决问题。
这是我的主要功能:
int main()
{
Book* book = xlCreateBook(); // xlCreateXMLBook() for xlsx
if (book)
{
Font* boldFont = book->addFont();
boldFont->setBold();
Font* titleFont = book->addFont();
titleFont->setName(L"Arial Black");
titleFont->setSize(16);
Format* titleFormat = book->addFormat();
if (titleFormat = nullptr)
{
titleFormat->setFont(titleFont);
}
Format* headerFormat = book->addFormat();
if (headerFormat = nullptr)
{
headerFormat->setAlignH(ALIGNH_CENTER);
headerFormat->setBorder(BORDERSTYLE_THIN);
headerFormat->setFont(boldFont);
headerFormat->setFillPattern(FILLPATTERN_SOLID);
headerFormat->setPatternForegroundColor(COLOR_TAN);
}
Format* descriptionFormat = book->addFormat();
if (descriptionFormat = nullptr)
{
descriptionFormat->setBorderLeft(BORDERSTYLE_THIN);
}
Format* amountFormat = book->addFormat();
if (amountFormat = nullptr)
{
amountFormat->setNumFormat(NUMFORMAT_CURRENCY_NEGBRA);
amountFormat->setBorderLeft(BORDERSTYLE_THIN);
amountFormat->setBorderRight(BORDERSTYLE_THIN);
}
Format* totalLabelFormat = book->addFormat();
if (totalLabelFormat = nullptr)
{
totalLabelFormat->setBorderTop(BORDERSTYLE_THIN);
totalLabelFormat->setAlignH(ALIGNH_RIGHT);
totalLabelFormat->setFont(boldFont);
}
Format* totalFormat = book->addFormat();
if (totalFormat = nullptr)
{
totalFormat->setNumFormat(NUMFORMAT_CURRENCY_NEGBRA);
totalFormat->setBorder(BORDERSTYLE_THIN);
totalFormat->setFont(boldFont);
totalFormat->setFillPattern(FILLPATTERN_SOLID);
totalFormat->setPatternForegroundColor(COLOR_YELLOW);
}
Format* signatureFormat = book->addFormat();
if (signatureFormat = nullptr)
{
signatureFormat->setAlignH(ALIGNH_CENTER);
signatureFormat->setBorderTop(BORDERSTYLE_THIN);
}
Sheet* sheet = book->addSheet(L"Sheet1");
if (sheet)
{
sheet->writeStr(2, 1, L"Invoice No. 3568", titleFormat);
sheet->writeStr(4, 1, L"Name: John Smith");
sheet->writeStr(5, 1, L"Address: San Ramon, CA 94583 USA");
sheet->writeStr(7, 1, L"Description", headerFormat);
sheet->writeStr(7, 2, L"Amount", headerFormat);
sheet->writeStr(8, 1, L"Ball-Point Pens", descriptionFormat);
sheet->writeNum(8, 2, 85, amountFormat);
sheet->writeStr(9, 1, L"T-Shirts", descriptionFormat);
sheet->writeNum(9, 2, 150, amountFormat);
sheet->writeStr(10, 1, L"Tea cups", descriptionFormat);
sheet->writeNum(10, 2, 45, amountFormat);
sheet->writeStr(11, 1, L"Total:", totalLabelFormat);
sheet->writeNum(11, 2, 280, totalFormat);
sheet->writeStr(14, 2, L"Signature", signatureFormat);
sheet->setCol(1, 1, 40);
sheet->setCol(2, 2, 15);
}
book->save(L"Invoice.xls"); // exception here!
book->release();
}
return 0;
}
我只想让程序正常运行,没有任何错误。请帮我解决我的问题。 任何帮助将不胜感激!
你总是在覆盖 Excel 给你的一切。所以当你这样做时:
sheet->writeStr(2, 1, L"Invoice No. 3568", titleFormat);
你传入 titleFormat
这是一个 nullptr
因为行:
if (titleFormat = nullptr)
打开警告并修复代码:
if (titleFormat != nullptr)
所有其他人也一样 if
。