Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getFilename() -Error #437

Open
DuanZhaobing opened this issue Aug 16, 2023 · 9 comments
Open

getFilename() -Error #437

DuanZhaobing opened this issue Aug 16, 2023 · 9 comments
Assignees
Labels

Comments

@DuanZhaobing
Copy link

SQLite::Database dbs("hello.db3", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
auto db_name = dbs.getFilename().c_str();
When I use the above code to create and get the database file name, the compiler prompts an error and cannot get the relevant value.

const std::string& getFilename() const noexcept

@DuanZhaobing
Copy link
Author

Error reading characters of string.

@UnixY2K
Copy link
Contributor

UnixY2K commented Aug 17, 2023

please provide the following information:

  • compiler name
  • compiler version
  • OS
  • build system: cmake, meson, etc.

@DuanZhaobing
Copy link
Author

DuanZhaobing commented Aug 17, 2023 via email

@SRombauts SRombauts self-assigned this Aug 17, 2023
@SRombauts
Copy link
Owner

SRombauts commented Aug 17, 2023

Hello,

I don't see anything wrong with how you use SQLiteCpp, only minor improvements that could be made to your code.
I can imagine that the issue is with the rest of the program; I would bet for garbage memory (eg if CenterWidget object is already destroyed when the function is called).

I copy pasted and slightly adapted your code to run in the example1/main.cpp, it's working as expected.

#include <vector>

void SaveToDatabase(
    const std::string database_name,
    const std::string table_name,
    const std::vector<uint8_t>& receive_data,
    std::uint32_t length)
{
    try
    {
        // Open a database file in create/write mode
        SQLite::Database database((database_name + ".db3").c_str(), SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);  // Error reading characters string
        std::cout << "SQLite database file '" << database.getFilename().c_str() << "' opened successfully\n";
    }
    catch (std::exception& e)
    {
        std::cout << "SQLite exception: " << e.what() << std::endl;
    }
}

void ReceiveCANData(const std::vector<uint8_t>& receive_data, uint32_t length)
{
    std::string database_name("C:\\UnitySrc\\SQLiteCpp\\examples\\example1\\example");
    std::string table_name = "table_name";
    SaveToDatabase(database_name, table_name, receive_data, length);
}

int main()
{
    const std::vector<uint8_t> receive_data = { 0x01, 0x02, 0x03, 0x04 };
    ReceiveCANData(receive_data, receive_data.size());

The output starts with the expected log:

SQLite database file 'C:\UnitySrc\SQLiteCpp\examples\example1\example.db3' opened successfully

@DuanZhaobing
Copy link
Author

Hello sir.

I used the same code in Visual Studio to create a new C++ Console App and a Qt Empty App, but with different results.
In the C++ Console App, everything works. But in the Qt Empty App, the exception unable to open database file is thrown.

I don't have enough experience to look further into the cause. And I don't know if it's a problem with my usage or something else.
Sincerely look forward to your reply.

//  In the Qt Empty App, no exception is thrown(when i use .data() or .c_str() function), but the database file name cannot be obtained(Unable to read memory).
SQLite::Database  new_db(s.data(), SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);  
#define SQLITECPP_COMPILE_DLL
#include <string>
#include <iostream>
#include <SQLiteCpp/SQLiteCpp.h>
#include <SQLiteCpp/VariadicBind.h>
int main()
{
	std::string s = "d.db3";
	try {
        // In the C++  Console App, everything works. But in the Qt Empty App, the exception unable to open database file is thrown.
	SQLite::Database  new_db(s, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);  
        
	// SQLite::Database  new_db(s.data(), SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);  //  In the Qt Empty App, no exception is thrown, but the database file name cannot be obtained(Unable to read memory).
	}
	catch (std::exception& e) {
		std::cout << "SQLite exception: " << e.what() << std::endl;
		return EXIT_FAILURE; // unexpected error : exit the example program
	}
	return 0;
}

getFilename

@UnixY2K
Copy link
Contributor

UnixY2K commented Aug 19, 2023

can you upload a sample repo with the code that throws the error?
as for the error seems that there is a use after free somewhere, probably related to the string used to initialize the database

@DuanZhaobing
Copy link
Author

DuanZhaobing commented Aug 19, 2023

Hello Sir.
The attachment is a simple sample code.Please check.
Here is the git repository address. https://github.com/DuanZhaobing/SQLiteCpp_getFilename-test
SQLiteCpp_getFilename-test.zip

@UnixY2K
Copy link
Contributor

UnixY2K commented Aug 21, 2023

seems that it could be caused for the following reasons:

  • somewhere in your code the string is modified and you try to use the c_str after it, if you have a std::string is preferred to use it directly
  • the string is not utf8 encoded, the most easy solution in c++17 would be to use std::filesystem the constructor should convert it to utf8 automatically for you

for the example repo is recommended that you also show the example of your QT app, or at least a minimal failing example that we can debug

@DuanZhaobing
Copy link
Author

Sorry to have kept you waiting, Sir.
The project files of the Qt app have been uploaded to the git repository.
https://github.com/DuanZhaobing/SQLiteCpp_getFilename-test/tree/main/SQLiteCppTest_Qt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants