Exe crashes after coming out of the method in c++ - c++

I have a method FormatOutPut() that internally calls a "callBack" method MyFunct() in C++. There is a crash, when the control comes out the FormatOutPut(). The callback method MyFunct() just prints the output on the screen. When I debugged the code using windbg I got the following traces,
(1250.1270): Stack buffer overflow - code c0000409 (!!! second chance !!!)
What I am guessing is that,the return address on the stack for the FormatOutPut(), is corrupted by the callback method. So when control is shifted back to the calling method it crashes.
When I comment out the callback method everything works fine. Any inputs on this will be great help.
Callback method prototype is as below,
typedef void(__stdcall *MyCallBack)(char*,char*,char*,char*,char*,char*,char*,char*,char*,char*,int, int );
Body:-
void MyCallbackRoutine(char* GetFeature,char* GetVersion,char* GetStartDate, char* GetExpireDate, char* GetUsers,char* GetKey,char* GetVendorString, char* GetHostID,char* GetErrorMsg,char* GetLicense,int GetCheckOutStatus, int nCount)
{
if ( nCount == 0 )
{
_strtime_s( timeCallbackstart, 10 );
time(&startCallbackstart);
bOnlyOnce = true;
}
cout << endl;
cout << "-------------------------------------------------------" << endl;
cout << "GetCheckOutStatus: " << GetCheckOutStatus << endl;
cout << "GetErrorMsg: " << GetErrorMsg << endl;
cout << endl;
cout << "GetFeature: " << GetFeature << endl;
cout << "GetVersion: " << GetVersion << endl;
cout << "GetStartDate: " << GetStartDate << endl;
cout << "GetExpireDate: " << GetExpireDate << endl;
cout << "GetUsers: " << GetUsers << endl;
cout << "GetKey: " << GetKey << endl;
cout << "GetVendorString: " << GetVendorString << endl;
cout << "GetHostID: " << GetHostID << endl;
cout << "GetLicense: " << GetLicense << endl;
cout << endl;
cout << "Licenscounter: " << nCount << endl;
cout << "------------------------------------------------------" << endl;
cout << endl;
}
Thanks and Regards,
AKJ

The issue that I see is that your calling convention of __stdcall for the typedef of the function prototype and the callback function itself does not match. If the calling convention does not match up, stack issues can arise when returning from the called function.
More on calling conventions here.
The typedef for the function is as follows:
typedef void(__stdcall *MyCallBack)(char*,char*,char*,char*,char*,char*,char*,
char*,char*,char*,int, int );
but the function that is assigned as the callback has this:
void MyCallbackRoutine(char* GetFeature,char* GetVersion,char* GetStartDate,
char* GetExpireDate, char* GetUsers,char* GetKey,
char* GetVendorString, char* GetHostID,
char* GetErrorMsg,char* GetLicense,
int GetCheckOutStatus, int nCount)
The number and type of parameters match up, the return type void matches, but the key element that doesn't match is that __stdcall is missing. By default, The calling convention is __cdecl if not specified. The correction should be:
void __stdcall MyCallbackRoutine(char* GetFeature,char* GetVersion,char* GetStartDate,
char* GetExpireDate, char* GetUsers,char* GetKey,
char* GetVendorString, char* GetHostID,
char* GetErrorMsg,char* GetLicense,
int GetCheckOutStatus, int nCount)
Note that the compiler should have picked up the issue when assigning a function who's definition doesn't match the prototype as this small example demonstrates (If this compiler error occurs, do not try to fix it by applying a C-style cast to shut the compiler up -- that is not a fix).

Related

C++ Passing Pointer To A Function

I have a problem passing a pointer to a function. Here is the code.
#include <iostream>
using namespace std;
int age = 14;
int weight = 66;
int SetAge(int &rAge);
int SetWeight(int *pWeight);
int main()
{
int &rAge = age;
int *pWeight = &weight;
cout << "I am " << rAge << " years old." << endl;
cout << "And I am " << *pWeight << " kg." << endl;
cout << "Next year I will be " << SetAge(rAge) << " years old." << endl;
cout << "And after a big meal I will be " << SetWeight(*pWeight);
cout << " kg." << endl;
return 0;
}
int SetAge(int &rAge)
{
rAge++;
return rAge;
}
int SetWeight(int *pWeight)
{
*pWeight++;
return *pWeight;
}
My compiler outputs this:
|| C:\Users\Ivan\Desktop\Exercise01.cpp: In function 'int main()':
Exercise01.cpp|20 col 65 error| invalid conversion from 'int' to 'int*' [-fpermissive]
|| cout << "And after a big meal I will be " << SetWeight(*pWeight);
|| ^
Exercise01.cpp|9 col 5 error| initializing argument 1 of 'int SetWeight(int*)' [-fpermissive]
|| int SetWeight(int *pWeight);
|| ^
PS: In real life I wouldnt use this but I got into it and I wanna get it working this way.
You shouldn't dereference the pointer. It should be:
cout << "And after a big meal I will be " << SetWeight(pWeight);
Also, in SetWeight(), you are incrementing the pointer instead of incrementing the value, it should be:
int SetWeight(int *pWeight)
{
(*pWeight)++;
return *pWeight;
}
int *pWeight = &weight;
This declares pWeight as a pointer to an int. SetWeight actually takes a pointer to an int, so you can just pass pWeight straight in without any other qualifiers:
cout << "And after a big meal I will be " << SetWeight(pWeight);
First I took your feedback and changed:
cout << "And after a big meal I will be " << SetWeight(*pWeight);
// to
cout << "And after a big meal I will be " << SetWeight(pWeight);
// But after that I changed also:
*pWeight++;
// to
*pWeight += 1;
The * symbol can have two different meanings in C++. When used in a function header, they indicate that the variable being passed is a pointer. When used elsewhere in front of a pointer it indicates that to which the pointer is pointing. It seems you may have confused these.

c++11 capture-by-value lambda producing wrong value

I'm trying to store a lambda in an object system involving several layers of indirection. I'm using g++ 4.7.1.
Depending on how exactly I construct the (equivalent) objects, the lambda may or may not have the correct value.
Code:
#include <iostream>
#include <functional> // used for std::function
using namespace std; // TODO nope
typedef function<int()> intf;
struct SaveLambda {
const intf func;
SaveLambda(const intf& _func) : func(_func) {}
};
struct StoreSaved {
const SaveLambda* child;
StoreSaved(const SaveLambda& _child) : child(&_child) {
cout << "Before returning parent: " << child->func() << endl;
}
};
int main() {
const int ten = 10;
auto S = SaveLambda([ten](){return ten;});
cout << "No indirection: " << S.func() << endl << endl;
auto saved = StoreSaved(S);
cout << "Indirection, saved: " << saved.child->func() << endl << endl;
auto temps = StoreSaved ( SaveLambda([ten](){cout << "&ten: "<< &ten << endl; return ten;}) );
cout << "***** what. *****" << endl;
cout << "Indirection, unsaved: " << temps.child->func() << endl;
cout << "***** what. *****" << endl << endl;
cout << "ten still lives: " << ten << endl;
}
Compile as g++ -std=c++11 -Wall -o itest itest.cpp and run: notice the one line of output with a different value.
What am I doing wrong? I assumed that capture-by-value would, well, capture by value. (Observe most disconcertingly that the print in StoreSaved (line 15) produces the correct value, unlike line 34, despite these both referring to the same object. The only difference is adding another layer of indirection.)
This is wrong:
auto temps = StoreSaved(
/* This temporary value dies at the last semicolon! */
SaveLambda([ten](){cout << "&ten: "<< &ten << endl; return ten;})
);
StoreSaved then has a pointer to a nonexistent object. Using it is UB.
As already pointed out by others, the problem is that in temps you end with a pointer to a nonexistent SaveLambda struct, as it is a temporary.
You can keep a copy using a SaveLambda struct in StoreSaved, instead of a pointer:
struct StoreSaved {
const SaveLambda child;
StoreSaved(const SaveLambda& _child) : child(_child) {
cout << "Before returning parent: " << child.func() << endl;
}
};
You also have to change all the child->func() to child.func(), as you are not dealing with a pointer anymore.

Python Callback from SWIG PyObject_Call Segfault

I have a wx.py.Shell.shell widget which lets the user execute python code that interacts with my program. I want to be able to pass a function that the user defines in this space to my C++ code (Through the wxswig generated wrapper around my custom widget)and execute it.
In my C++ code I'm using a std::function <> class to invoke bound functions (C++ or Python)
So I created a simple class to wrap the PyObject with the function call operator. However I get a segfault when I try to call the PyObject *.
class PyMenuCallback
{
PyObject *Func;
public:
PyMenuCallback(const PyMenuCallback &op2);
PyMenuCallback(PyObject *func);
~PyMenuCallback ();
void operator() (int id);
};
/////////////////////////////////////////////////////////
PyMenuCallback::PyMenuCallback(PyObject *func)
: Func(func)
{
Py_XINCREF (Func);
if(!PyCallable_Check(Func))
cout << "Not a Callable Callback." << endl; //Throw an exception or something
}
PyMenuCallback::PyMenuCallback(const PyMenuCallback &op2)
: Func (op2.Func)
{
Py_XINCREF (Func);
if(!PyCallable_Check(Func))
cout << "Not a Callable Callback." << endl;
}
PyMenuCallback::~PyMenuCallback()
{
Py_XDECREF (Func);
}
void PyMenuCallback::operator() (int id)
{
cout << "Calling Callback" << endl;
if (Func == 0 || Func == Py_None || !PyCallable_Check(Func))
return;
cout << "Building Args" << endl;
PyObject *arglist = Py_BuildValue ("(i)",id);
cout << "Func: " << Func->ob_type->tp_name << " " << Func->ob_refcnt << endl;
PyObject *result = PyObject_Call(Func,arglist,0); //<<<<<---SEGFAULTS HERE
cout << "Executed" << endl;
Py_DECREF(arglist);
Py_XDECREF(result);
}
In my attempts to find what was going on, I put a bunch of print statements.
One of which prints the type name and reference count the line before the segfault. This results in "function 3" so I have to assume the function has not been destroyed yet.
I'm passing the following to swig:
void AddOption (std::string name, PyObject *pycallback);
In which I construct a PyMenuCallback
I'm at a loss for what's causing the segfault, any ideas?
Since the C++ calling the python callback is within a wxWidget, and the swig wrapper is generated by the special wxPython swig (wxswig?) There is some thread protection required around the function call...
The fixed operator should look like this
void PyMenuCallback::operator() (int id)
{
cout << "Calling Callback" << endl;
if (Func == 0 || Func == Py_None || !PyCallable_Check(Func))
return;
cout << "Building Args" << endl;
PyObject *arglist = Py_BuildValue ("(i)",id);
cout << "Built: " << arglist << endl;
cout << "Func: " << Func->ob_type->tp_name << " " << Func->ob_refcnt << endl;
wxPyBlock_t blocked = wxPyBeginBlockThreads(); //Anti-WxSwig
PyObject *result = PyObject_Call(Func,arglist,0);
wxPyEndBlockThreads(blocked);
cout << "Executed" << endl;
Py_XDECREF(arglist);
Py_XDECREF(result);
}
Make sure to include
#include "wx/wxPython/wxPython.h"
#include "wx/wxPython/wxPython_int.h"

Where am I wrong? SetDataBuffer for DATE

I want to do array fetch for DATE in oracle with following code. It compiles alright and export all other data types correctly, except for the DATE type. The program says "ora-32146 cannot peform operation on a null date".
Any one can see where I am wrong?
Statement *stmt = conn->createStatement("SELECT AGE,CASH2, BIRTHDATE from myTable2"); /*, CASH1, CASH2, BIRTHDATE*/
//stmt->setMaxParamSize(1,sizeof(Number));
ResultSet *rs=stmt->executeQuery();
string myName[400];
int myAge[400];
double myCash1[400];
double myCash2[400];
oracle::occi::Date myBirthDate[400];
//rs->setDataBuffer(1,myName,OCCI_SQLT_STR,sizeof(string));
rs->setDataBuffer(1,myAge,OCCIINT,sizeof(int));
rs->setDataBuffer(2, myCash2, OCCIBDOUBLE, sizeof(double),NULL);
rs->setDataBuffer(3, myBirthDate,OCCI_SQLT_DATE, sizeof(oracle::occi::Date),NULL);
while (rs->next(200)==ResultSet::DATA_AVAILABLE)
{
//cout << "Exporting batch..." << endl;
for(size_t i=0;i<rs->getNumArrayRows();i++)
{
//cout << myName[i] << endl;
cout << myAge[i] << endl;
//cout << myCash1[i] << endl;
cout << myCash2[i] << endl;
int y;
unsigned int m,d,h,mm,s;
myBirthDate[i].getDate(y,m,d,h,mm,s);
cout << y <<"-"<<m<<"-"<<s << endl;
}
}
Figured it out.
using rs->setDataBuffer(3, myBirthDate,OCCI_SQLT_DAT, 7,NULL);
works out all right.
No good document on OCCI...

DM_simtoolTest.h:39: error: conversion from `int (*)(int)' to non-scalar type `std::string' requested

My code:
Functionality: It is a function expects a three arguments and create a file.
void performLog(string strStoredProcName, int nCount, double time)
{
int tme=(int) time;
int hour=tme/3600;
tme=tme%3600;
int min=tme/60;
tme=tme%60;
int sec=tme;
char *StrLen;
int len = 0;
int lenpass = 0;
StrLen = &strStoredProcName[0];
len = strlen(StrLen);
lenpass = 41 - len;
fstream outFile( "Perform.out", ios_base::out | ios_base::app );
if ( ! outFile )
{
cerr << "Cannot open 'Perform.out' for output" << "\n" << endl;
exit(-1);
}
if (paramLogCreation == false)
{
outFile << "**************Performance Log*********************" << "\n" << endl;
outFile << "DB Type: MYSQL" << "\n" << endl;
outFile << "-----------------------------------------------------------------------------------------" << "\n" << "\t" << "\t" << "\t" << "\t" << "Stored Procedure Statitics" << "\n" << endl;
outFile << "-----------------------------------------------------------------------------------------" << "\n" << endl;
outFile << "Store Procedure Name" << setw(30) << "Execution Count" << setw(30) << "Time Taken to Execute" << "\n" << endl;
paramLogCreation = true ;
}
outFile << strStoredProcName << setw(lenpass) << nCount << setw(20) <<hour<<"::"<<min<<"::"<<sec <<"\n" << endl;
outFile.close();
}
Here i am writing a unit test cases, for the code , which i have written, This function is one of the functions in that.
Please help me , how to resolve this issue.
Please i am very new one to the C++ and need to know the where i committed mistake.
I think this is the error:
void performLog(string strStoredProcName, int nCount, double time)
[...]
char *StrLen;
[...]
StrLen = &strStoredProcName[0];
To convert a string to char*, you should use c_str:
StrLen = new char [strStoredProcName.size()+1];
strcpy (StrLen, strStoredProcName.c_str());
Assuming by string you mean std:;string then when you say:
StrLen = &strStoredProcName[0];
len = strlen(StrLen);
there is no guarantee that the proc name contains a null-terminated string for strlen() to work on. If you want the length of the string, use the size() member function:
len = strStoredProcName.size();
However, this should not cause the error message you are getting, so please indicate in your post where the error is!!!
It looks like the error you're getting is in the header file (DM_simtoolTest.h), not the source file, which you appear to have pasted into the question.
The compiler is complaining that your code tries to use a function pointer (that's the strange int (*)(int) syntax) to get a string. Without seeing the code in question, my first (wild, speculative) guess would be that you forgot to quote the name of the function you're trying to test. I'd need to see the source of the header as well to properly identify the problem.