-3

I am writing code to make an invoice in excel by using the library libxl. My code creates a file, writes in it and then saves the file in the project folder.

However, as I progress to debug it, I see an 'Unhandled exception', with access violation in writing a particular location. The exception actually says this 'Unhandled exception at 0x100437D7(libxl.dll) in BillingSoft.exe:0xC0000005: Access violation writing location 0x00C3DED4.'. I have read many articles and solutions but none of them seem to fix my problem. I tried allocating space for "book" by using

    Book* book = (Book*)malloc(sizeof(book));

before declaration of "book", but it just changes the location values and doesn't solve the problem.

This is my main function:

    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;
    }

I just want the program to work fine without any error. Please help me fix my problem. Any help would be appreciated !

A_Singh7
  • 648
  • 7
  • 15
  • 1
    `sizeof(book) != sizeof(Book)`. The former allocates size of a pointer rather than size of a `Book` class. – Yksisarvinen Jan 19 '19 at 11:19
  • 1
    `Book* book = (Book*)malloc(sizeof(book));` that's not how you allocate memory in c++, use `new` or even better an appropriate smart pointer (if you're sure you need a pointer at all). – πάντα ῥεῖ Jan 19 '19 at 11:19
  • 1
    `Book* book = (Book*)malloc(sizeof(book)); book = xlCreateBook();` Such `malloc` call is useless, since the pointer to the memory, that was returned, was immediately overwritten, by the return value of `xlCreateBook`. – Algirdas Preidžius Jan 19 '19 at 11:23
  • Yeah, but that was just a try and not something fully serious. Please suggest solutions. Thanks ! – A_Singh7 Jan 19 '19 at 11:31
  • And also, changing sizeof(book) to sizeof(Book) doesn't eliminate the problem. Thanks ! – A_Singh7 Jan 19 '19 at 11:33
  • 1) "_Yeah, but that was just a try and not something fully serious._" When writing code, one must understand the code that one writes, instead of trying random snippets of code, while seeing what sticks. 2) "_changing sizeof(book) to sizeof(Book) doesn't eliminate the problem_" Of course, it doesn't help, because, as I already stated, such call does accomplish anything more, than to produce a memory leak.3)Your `if` statements accomplish nothing, since `if (titleFormat = nullptr)` is not a comparison, and I don't know if setting bunch of `nullptr` values via `writeStr` has anything to with it. – Algirdas Preidžius Jan 19 '19 at 11:37
  • What's that? `if (signatureFormat = nullptr)` – Matthieu Brucher Jan 19 '19 at 11:37
  • @A_Singh7 *I have read many articles and solutions* -- You wasted your time doing this. The issue is one where you have bug(s) in your program. This isn't a case where the program is supposed to work, and there is something wrong with the operating system or compiler toolset. It is a matter of improper C++ coding and you not handling memory management properly, nothing more nothing less. – PaulMcKenzie Jan 19 '19 at 12:07
  • @PaulMcKenzie. I understood your words. Thanks for the comments. I actually know all this, about coding and all things. The thing is I want help here from people like you about how to improve my code and solve this problem. Thanks ! – A_Singh7 Jan 19 '19 at 13:38

1 Answers1

1

You are always overwriting everything Excel gives you. So when you do:

sheet->writeStr(2, 1, L"Invoice No. 3568", titleFormat);

You pass in titleFormat which is a nullptr because of line:

if (titleFormat = nullptr)

Turn on your warnings and fix your code:

if (titleFormat != nullptr)

And same for all other if.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • Thank you so much for your work and solution. But, unfortunately the problem continues to remain the same. – A_Singh7 Jan 19 '19 at 11:59
  • Have you fixed everything properly? No more nullptr assignments? – Matthieu Brucher Jan 19 '19 at 12:00
  • Yeah, there are no more nullptr assignments. – A_Singh7 Jan 19 '19 at 12:00
  • A thought strikes my mind, well, can this error be because the memory location which has access violation while writing contains some garbage or junk ? – A_Singh7 Jan 20 '19 at 03:32
  • Yeah, and also, I tried debugging the code without those if’s and nullptr’s, it throws an exception ‘Unhandled exception. titlefomat is nullptr.’(because title format is the first to come in the line.) – A_Singh7 Jan 20 '19 at 03:34
  • OK, so you know what to fix. BTW, if `titlefornat` is `nullptr`, you cannot do anything in the final `if`, same for all the other tests that you have. If you get a bad return from the call, there is nothing we can do with the information you gave us and with the current state of the code. I consider the answer final. – Matthieu Brucher Jan 20 '19 at 10:10
  • As you like, I would accept it, if not me, it could help others. – A_Singh7 Jan 20 '19 at 11:35