[GH-ISSUE #463] Bug: Read access violation is thrown when using "workbook_add_worksheet()" in a static function #361

Closed
opened 2026-05-05 12:12:06 -06:00 by gitea-mirror · 1 comment
Owner

Originally created by @HeyoItsMateo on GitHub (Dec 8, 2024).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/463

Hello,

I am trying to write a wrapper to make use of libxlsxwriter more streamlined and I am running into an access read violation during a call to workbook_add_worksheet() in a static function.

The XLSL object is defined as:

#include <sstream>
#include "xlsxwriter.h"

struct XLSX {
    XLSX(std::string fileName) {
        p_workbook = workbook_new(fileName.c_str());
    }
    ~XLSX() {
        close_workbook();
    }
public:
    static void add_sheet(const std::string& sheetName = "") {
	num_worksheets++;
	if (sheetName.empty()) {
	    std::ostringstream nameStream;
	    nameStream << "Sheet" << num_worksheets;
	    std::string _sheetName = nameStream.str();
	    worksheets[_sheetName] = workbook_add_worksheet(p_workbook, _sheetName.c_str());
	}
	else {
	    worksheets[sheetName] = workbook_add_worksheet(p_workbook, sheetName.c_str()); // Error line
	}
    }
    static void close_workbook();
private:
    inline static lxw_workbook* p_workbook = nullptr;
    inline static std::map<std::string, lxw_worksheet*> worksheets;
    inline static int num_worksheets = 0;
};

And the operations done in the main function are:

int main() {
    XLSX("myStats.xlsx");
    XLSX::add_sheet("SheetName");

    return 0;
}

Here is the sequence of function calls that led to the error (oldest call to most recent):
-> XLSX::add_sheet("SheetName")
-> workbook_add_worksheet(p_workbook, sheetName.c_str())
-> workbook_validate_sheet_name(self, init_data.name)
-> workbook_get_worksheet_by_name(self, sheetname)
-> RB_FIND(lxw_worksheet_names, self->worksheet_names, &worksheet_name)
Error: access violation reading location.

Looking at the variables reveals the issue in self->worksheet_names:
self->worksheet_names = 0x000002353f26c500 {rbh_root=0x0000000000000000 <NULL> }
With the members being unable to be read:
self->worksheet_names->rbh_root->name = <Unable to read memory>

However, if I remove the static keyword from the function add_sheet(), the error goes away.
Nothing else in the XLSX struct is changed, but the main function is adjusted to account for the removal of the static keyword.
The working main function is below:

int main() {
    XLSX xlsx("myStats.xlsx");
    xlsx.add_sheet("SheetName");

    return 0;
}
Originally created by @HeyoItsMateo on GitHub (Dec 8, 2024). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/463 Hello, I am trying to write a wrapper to make use of libxlsxwriter more streamlined and I am running into an access read violation during a call to `workbook_add_worksheet()` in a `static` function. The XLSL object is defined as: ```C++ #include <sstream> #include "xlsxwriter.h" struct XLSX { XLSX(std::string fileName) { p_workbook = workbook_new(fileName.c_str()); } ~XLSX() { close_workbook(); } public: static void add_sheet(const std::string& sheetName = "") { num_worksheets++; if (sheetName.empty()) { std::ostringstream nameStream; nameStream << "Sheet" << num_worksheets; std::string _sheetName = nameStream.str(); worksheets[_sheetName] = workbook_add_worksheet(p_workbook, _sheetName.c_str()); } else { worksheets[sheetName] = workbook_add_worksheet(p_workbook, sheetName.c_str()); // Error line } } static void close_workbook(); private: inline static lxw_workbook* p_workbook = nullptr; inline static std::map<std::string, lxw_worksheet*> worksheets; inline static int num_worksheets = 0; }; ``` And the operations done in the main function are: ```C++ int main() { XLSX("myStats.xlsx"); XLSX::add_sheet("SheetName"); return 0; } ``` Here is the sequence of function calls that led to the error (oldest call to most recent): -> `XLSX::add_sheet("SheetName")` -> `workbook_add_worksheet(p_workbook, sheetName.c_str())` -> `workbook_validate_sheet_name(self, init_data.name)` -> `workbook_get_worksheet_by_name(self, sheetname)` -> `RB_FIND(lxw_worksheet_names, self->worksheet_names, &worksheet_name)` Error: access violation reading location. Looking at the variables reveals the issue in `self->worksheet_names`: `self->worksheet_names = 0x000002353f26c500 {rbh_root=0x0000000000000000 <NULL> }` With the members being unable to be read: `self->worksheet_names->rbh_root->name = <Unable to read memory>` However, if I remove the `static` keyword from the function `add_sheet()`, the error goes away. Nothing else in the XLSX `struct` is changed, but the main function is adjusted to account for the removal of the `static` keyword. The working main function is below: ```C++ int main() { XLSX xlsx("myStats.xlsx"); xlsx.add_sheet("SheetName"); return 0; } ```
Author
Owner

@jmcnamara commented on GitHub (Dec 9, 2024):

I don't think I can help you here. As far as I can see it is a C++ issue and not a direct issue with libxlsxwriter. You will probably get a better answer on a C++ forum.

There is also an existing CPP wrapper for libxlsxwriter: https://github.com/Alexhuszagh/libxlsxwriterpp

It is a bit out of date but it may help you as a starting point.

<!-- gh-comment-id:2527429145 --> @jmcnamara commented on GitHub (Dec 9, 2024): I don't think I can help you here. As far as I can see it is a C++ issue and not a direct issue with libxlsxwriter. You will probably get a better answer on a C++ forum. There is also an existing CPP wrapper for libxlsxwriter: https://github.com/Alexhuszagh/libxlsxwriterpp It is a bit out of date but it may help you as a starting point.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/libxlsxwriter#361
No description provided.