lost value of object in c++ sqlite callback function - c++

these are the functions, after building the correct object inside fillAlbum data gets lost in openAlbum.
/*
the function will fill the album with correct values (callback function)
*/
int fillAlbum(void* data, int argc, char** argv, char** azColName)
{
Album* album = new Album();
album->setName(argv[1]);
album->setCreationDate(argv[3]);
album->setOwner(std::stoi(argv[2]));
data = album;
return 0;
}
/*
the function return the asked album
*/
Album DatabaseAccess::openAlbum(const std::string& albumName)
{
Album album;
char** errMessage = nullptr;
std::string sqlStatement = "SELECT * FROM ALBUMS WHERE NAME LIKE '" + albumName + "';";
sqlite3_exec(db_access, sqlStatement.c_str(), fillAlbum, &album, errMessage);
return album;
}

It gets lost (in fact it is worse: you have a memory leak!) because you don't use the callback correctly. You pass &album and now you have to cast the void* pointer and fill it, not overwrite it (in fact, the data = album line has no effect at all outside the fillAlbum function, you just overwrite a local variable). Try this:
int fillAlbum(void* data, int argc, char** argv, char** azColName)
{
Album* album = static_cast<Album*>(data); // <-- this line is crucial
album->setName(argv[1]);
album->setCreationDate(argv[3]);
album->setOwner(std::stoi(argv[2]));
return 0;
}

Related

Need help storing a value I get from a sqlite3 DB in a C++ variable

I'm having a 'Data.db' with 3 tables in it that store some kind of 'football data'.
One table is for 'teams' and another one is for 'players'. What I want to do now is to get the 'highest' ID from 'Teams' so I can assign newly generated players to that team.
(Every player has a _teamId)
I am able to print the ID I want by using this query and a callback_id that prints just the value I need.
Query:
string query = "SELECT COUNT(ID) FROM TEAMS;";
highest_id = (sqlite3_exec(db, query.c_str(), callback_id, 0, &messageError));
Callback:
int Team::callback_id(void* data, int count, char** values, char** columns) {
int id = atoi(values[1]);
std::cout << "callback_id says: " << id << endl;
return 0;
}
What do I have to do to store this id from my callback function in a variable that I can use later in my program? It seems like I can't just return it and I can't just assign the id value to my Team::_id as it gives me an error:
'invalid use of member ‘Team::_id’ in static member function'
Help would be much appreciated. I'm still trying to learn more about sqlite3 and the callback function in particular but I've wasted so much time on this problem that I don't know what else I could try.
You have a void* argument in sqlite3_exec which you currently set to 0. Instead, pass a pointer to an object there. This makes it possible to store what you like in that object in the callback.
One often passes a pointer to an object and call a non-static member function in that object to deal with the callback.
Example:
class Team {
public:
void func() {
std::string query = "SELECT COUNT(ID) FROM TEAMS;";
sqlite3_exec(db, query.c_str(), &Team::callback_id_proxy, this, &messageError);
// ^^^^
}
// a static callback proxy, casting `data` to a `Team*`
static int callback_id_proxy(void* data, int count, char** values, char** columns) {
return static_cast<Team*>(data)->callback_id(count, values, columns);
}
int callback_id(int count, char** values, char** columns) {
// here you can store what you like in your `Team` object
if(count > 0) highest_id = atoi(values[0]);
else highest_id = 0;
return 0;
}
int highest_id;
};
Note: You may want to use SELECT MAX(ID) FROM TEAMS; instead. Otherwise, you may get duplicates if you delete a team and then add a new team.
Another option is to make func and highest_id static too. I've renamed highest_id into number_of_teams here, because that's what it really is.
class Team {
public:
static int get_number_of_teams() {
std::string query = "SELECT COUNT(ID) FROM TEAMS;";
if(sqlite3_exec(db, query.c_str(),
&Team::callback_count,
nullptr, // static function, no "this" available
&messageError) == SQLITE_ABORT)
throw std::runtime_error("callback_id returned non-zero");
return number_of_teams;
}
static int callback_count(void* data, int count, char** values, char** columns) {
if(count > 0) {
number_of_teams = atoi(values[0]);
return 0;
}
return 1; // indicate error
}
private:
inline static int number_of_teams = -1;
};

Passing a member(which is a class) of an array(of class') to a function

I am trying to pass 5th element of an array(Products[]) of class product to another function. The goal is to update the information of the element Product[5]. Everything seems to work fine except information of Product[5] variable is not updating.
Update: Problem solved by removing while(!EOF), thanks to Remy Lebeau.
The relevant part of Class:
class product
{
private:
float Wholesale_Price = 0;
float Retail_Price = 0;
string Supplier = "N/A";
string Note = "N/A";
int Stock_Amount = 0;
public:
string Name="N/A";
void UpdateRetailPrice(float New_Retail_Price)
{
Retail_Price = New_Retail_Price;
}
void UpdateProductAmount(int New_Stock_Amount)
{
Stock_Amount = New_Stock_Amount;
}
void UpdateWholesale_price(float New_Wholesale_Price)
{
Wholesale_Price = New_Wholesale_Price;
}
};
The relevant part of function:
void ReadProductFromFile(product* Products)
{
string Name, Supplier, Note, Wholesale_Price, Retail_Price, Stock_Amount;//wholesale price, stock amount,price are in string so that
//it becomes easy to use getline(). Use stoi() later for turning string to int.
ifstream ReadProductFromFile;
ReadProductFromFile.open("product.txt");
if (!ReadProductFromFile)
{
perror("Product File failed to open");
return;
}
while(!EOF)
{
/*read product info txt file------>*/
getline(ReadProductFromFile, Name);
getline(ReadProductFromFile, Wholesale_Price);
getline(ReadProductFromFile, Retail_Price);
getline(ReadProductFromFile, Stock_Amount);
/*update product info---------->*/
Products->Name = Name;
Products->UpdateWholesale_price(stoi(Wholesale_Price));
Products->UpdateProductAmount(stoi(Stock_Amount));
Products->UpdateRetailPrice(stoi(Retail_Price));
}
}
Relevant part of Main function:
int main(int argc, char const *argv[])
{
product Products[10];
ReadProductFromFile(Products+5);//is this the right way to send Products[5]? tried &(product[5]) but error
return 0;
}
Input:
Bananas
210
270
310

Run on list that passed as void* data

I sent a list of type "Albums" to a callback function of SQL database.
I tried to run on the list with for loop but I got an error that says "This range-based 'for' statement requires a suitable "begin" function and none was found"
Here is my code:
//my code in short
int callbackPictures(void* data, int argc, char** argv, char** azColName)
{
list<Album>* albums = static_cast<list<Album>*>(data);
for (auto& album : albums)//this is where I got the error
{
//do stuff
}
}

unpredictable runtime behavior while converting QString to const char*

I have these following codes set up-
class ID3{
const char* fileName;
TagLib::FileRef *file;
public:
ID3(const char *);
QImage* artwork();
}
ID3::ID3(const char* fileNameStr){
this->fileName = fileNameStr;
this->file = new TagLib::FileRef(fileNameStr);
qDebug()<<fileNameStr; //OUTPUT 2
}
QImage* ID3::artwork(){
QString str = QString::fromLocal8Bit(this->fileName);
qDebug()<<str; //OUTPUT 3
//MORE CODES-------
}
const char * QstrTocChar(QString str){
QByteArray ba = str.toLocal8Bit();
qDebug()<<ba.constData(); //OUTPUT 1
return ba.constData();
}
int main(int argc, char *argv[]){
.
.
.
QString fileName = "C:/Qt/Qt5.0.2/Projects/taglib_test/music files/Muse_-_Madness.mp3";
file = new ID3(QstrTocChar(fileName));
QImage *image = file->artwork();
}
Now when I run the program, I get these strange outputs
OUTPUT 1
C:/Qt/Qt5.0.2/Projects/taglib_test/music files/Muse_-_Madness.mp3
OUTPUT 2
????p???e'2/
OUTPUT 3
"°í³àpµ˜Æe'2/"
Not sure about OUTPUT 2 but I expect OUTPUT 3 to be same as OUTPUT 1. I am a Qt newbie. Would really appreciate advice/help in understanding, these strange character encoding issues and how to get OUTPUT 3 fixed.
Thanks!
ba.constantData() is returning a pointer to data which will be invalid when QstrToChar finishes executing (the 8-bit converted QByteArray), when QstrToChar completes, all you have left is free'd junk.
What if you just did:
file = new ID3(fileName.toLocal8Bit().constData());
in your main routine?
Actually, you still probably need to keep your own copy of this data in your private ID3 char *, since it can go away with the destruction of these temporaries.
Your code should be this, instead:
class ID3{
std::string fileName;
std::smart_ptr<TagLib::FileRef> file;
public:
ID3(std::string);
QImage* artwork();
}
ID3::ID3(std::string fileNameStr) {
this->fileName = fileNameStr;
this->file.reset(new TagLib::FileRef(fileNameStr));
qDebug()<<fileNameStr; //OUTPUT 2
}
QImage* ID3::artwork(){
QString str = QString::fromLocal8Bit(this->fileName);
qDebug()<<str; //OUTPUT 3
//MORE CODES-------
}
std::string QstrToCppString(QString str){
QByteArray ba = str.toLocal8Bit();
qDebug()<<ba.constData(); //OUTPUT 1
return std::string(ba.constData());
}
int main(int argc, char *argv[]){
.
.
.
QString fileName = "C:/Qt/Qt5.0.2/Projects/taglib_test/music files/Muse_-_Madness.mp3";
file = new ID3(QstrToCppString(fileName));
QImage *image = file->artwork();
}
Notice that I've wrapped your TagLib::FileRef in a smart_ptr as well, since you are new-ing it, you'll need to manage the memory. An alternative would be to write a proper destructor for your ID3 class. You're definitely leaking these currently (unless you just didn't share your destructor code).

Using string pointers in C++ and Objective-C

I have a sample project here on github where I created a c++ wrapper class for an external C++ library that I want to use in Objective-C.
I don't understand why my returned pointers are sometimes correct and sometimes wrong. Here's sample output:
Test Data = 43343008
In Compress 43343008
Returned Value = 43343008
Casted Value = 43343008
Test Data = 2239023
In Compress 2239023
Returned Value = 2239023
Casted Value = 2239023
Test Data = 29459973
In Compress 29459973
Returned Value = 29459973
Casted Value = l.remote
Test Data = 64019670
In Compress 64019670
Returned Value =
Casted Value = stem.syslog.master
In the above output you can see that the 1st and 2nd click of the button outputs the results I was expecting. In each of the other clicks either the returned value or casted value are invalid. I'm assuming this is because my pointer is pointing to an address I wasn't expecting. when running the app multiple times, any button click could be right or wrong.
I also tried with a single thread but experienced similar results.
The complete code is on github but here are the important bits.
ViewController.m
#import "ViewController.h"
extern const char * CompressCodeData(const char * strToCompress);
#implementation ViewController
...
// IBAction on the button
- (IBAction)testNow:(id)sender
{
[self performSelectorInBackground:#selector(analyze) withObject:nil];
}
- (void)analyze
{
#synchronized(self) {
const char *testData = [[NSString stringWithFormat:#"%d",
(int)(arc4random() % 100000000)] UTF8String];
NSLog(#"Test Data = %s", testData);
const char *compressed = CompressCodeData(testData);
NSLog(#"Returned Value = %s", compressed);
NSString *casted = [NSString stringWithCString:compressed
encoding:NSASCIIStringEncoding];
NSLog(#"Casted Value = %#\n\n", casted);
}
}
#end
SampleWrapper.cpp
#include <iostream>
#include <string.h>
#include <CoreFoundation/CoreFoundation.h>
using namespace std;
extern "C"
{
extern void NSLog(CFStringRef format, ...);
/**
* This function simply wraps a library function so that
* it can be used in objective-c.
*/
const char * CompressCodeData(const char * strToCompress)
{
const string s(strToCompress);
// Omitted call to static method in c++ library
// to simplify this test case.
//const char *result = SomeStaticLibraryFunction(s);
const char *result = s.c_str();
NSLog(CFSTR("In Compress %s"), result);
return result;
}
}
You are returning a pointer to at object that has been deallocated.
const string s(strToCompress);
…
const char *result = s.c_str();
NSLog(CFSTR("In Compress %s"), result);
return result;
s does not exist after CompressCodeData() function is over, so the pointer to it's internal memory is invalid.
You could allocate a chunk of memory to hold the response, but it would be up to the caller to release it.
char *compressed = CompressCodeData(testData);
NSLog(#"Returned Value = %s", compressed);
NSString *casted = [NSString stringWithCString:compressed
encoding:NSASCIIStringEncoding];
free(compressed);
NSLog(#"Casted Value = %#\n\n", casted);
…
const char * CompressCodeData(const char * strToCompress)
…
char *result = strdup(s.c_str());
Another solution is to pass in the memory to store the data into.
char compressed[2048]; // Or whatever!
CompressCodeData(testData, compressed, sizeof(compressed));
NSLog(#"Returned Value = %s", compressed);
NSString *casted = [NSString stringWithCString:compressed
encoding:NSASCIIStringEncoding];
NSLog(#"Casted Value = %#\n\n", casted);
…
void CompressCodeData(const char * strToCompress, char *result, size_t size)
…
s.copy(result, size - 1);
result[s.length() < size ? s.length() : size-1] = '\0';