memory access violation using std::map as local member [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a memory access violation error on the line m_Lights=tmp; during the call of the method v_init() in Algo::Init(). Shouldn't map m_Lights be created at the instantiation of m_LightsManager? Why do I have this error?
class LightManager
{
private:
std::map<sint32,Light> m_Lights;
public:
LightManager (void);
~LightManager (void);
void v_init();
};
LightManager ::LightManager (void)
{
}
LightManager ::~LightManager (void)
{
}
void LightManager ::v_init()
{
Light tL;
std::memset(&tL,0,sizeof(Light));
std::map<sint32,Light> tmp;
tmp.insert(std::pair<sint32,Light> (-1,tL));
m_Lights=tmp;
}
class Algo
{
private:
LightsManager m_LightsManager;
....
public:
Algo();
void Init();
};
Algo::Algo()
{
Init();
}
void Algo::Init()
{
m_LightsManager.v_init();
}

If Light is not trivially-copyable then
std::memset(&tL,0,sizeof(Light));
is undefined behavior. This is likely the cause of your error.

In addition to Vittorios answer: Do all the initialization in Lights constructor, instead of relying on an external memset call. And don't use std::memset in C++ to initialize all member variables of an object in one statement, do it explicitely for every variable (usually using a simple assignment; in C++11 you can do that even in the declaration/header, where it IMHO belongs).
Reason: Light may be derived, and the base class(es) define their own data, which you overwrite by recklessly nulling the whole object.

I solved the error, the problem came from a wrong declared pointer in other part of the soft that overlaped with the memory space where the object m_LightsManager where initiated ,

Related

My program is stopping [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This is my class,and my problem is when I call the constructor
Eveniment e1(1,m1);
with parameters in main method, my program is stopping and I don't know why. M1 is an object of IntrareCAlendar.
class Eveniment{
private:
const int id;
IntrareCalendar data;
char* detalii;
int static nrIntrari;
public:
Eveniment(int nr,IntrareCalendar ic) :id(nr){
this->data = ic;
nrIntrari++;
}
~Eveniment(){
if (this->detalii != NULL)
delete[]this->detalii;
}
};
What should I do? thanks a lot!
You never set detalii to anything valid. It remains uninitialised - it is not initialised to a particular value automatically. You could set it to nullptr in your constructor. (Don't use NULL in C++.)
Your destructor calls delete[] on that member, but no new[] has been called prior to that. As such the behaviour of your program is undefined.
Also, consider using static std::atomic<int> as the type for nrIntarari in case multiple threads instantiate an Eveniment.

C++ access violation when writing to typdef struct [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a struct defined in a header file. Then I have a singleton class where I am trying to use the struct. When I call ResetVars() from another class I get an access violation when it hits the line that says test.numResponses = "TEST". I am assuming this has something to do with initialization but I haven't been able to solve it. I am new to c++ and I have no idea how to get around this. Thanks for any help.
struct.h
typedef struct POLL_DATA
{
std::string numResponses;
std::string type;
std::string question;
} POLL_DATA;
ControlPolls.h
class ControlPolls
{
private:
static bool instanceFlag;
static ControlExitPolls *controlSingle;
ControlExitPolls();
POLL_DATA test;
public:
static ControlExitPolls* getInstance();
void ResetVars();
};
ControlPolls.cpp
#include "ControlPolls.h"
bool ControlPolls::instanceFlag = false;
ControlPolls* ControlPolls::controlSingle = NULL;
//Private Constructor
ControlExitPolls::ControlExitPolls()
{
};
//Get instance
ControlPolls* ControlPolls::getInstance()
{
if(!instanceFlag)
{
controlSingle = &ControlPolls();
instanceFlag = true;
return controlSingle;
}
else
{
return controlSingle;
}
}
void ControlExitPolls::ResetVars()
{
test.numResponses = "TEST";
}
callingClass.cpp
ControlPolls *controlSingleton;
controlSingleton = ControlPolls::getInstance();
controlSingleton->getInstance()->ResetVars();
You've been struck by C++'s Most Vexing Parse, a compiler rule that says anything that could be a function declaration is a function declaration. The culprit is this line:
POLL_DATA testPoll();
testPoll is treated as the declaration of a function with return type POLL_DATA. Try removing the brackets, or writing simply POLL_DATA testPoll; which implicitly calls the compiler-generated default constructor.
Another larger problem is that testPoll is a member of A, but you've hidden it and declared a local variable in your constructor, A::A(). I suggest you remove the constructor altogether because the implicit constructor will suffice.
Some more notes on your code:
You've declared your class a but refer to it later as A.
You've written an implementation of a constructor for A without declaring it like a proper forward declaration.
Also, typedef struct is not needed in C++. It is sufficient and encouraged to write:
struct POLLDATA {
...
};

C++: accessing private members of the class [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Still getting my head around C++ as I'm new to it, but I'm trying to extend some existing code I've got that is expecting me to make use of the std::vector.
The following is declared in the header (shortened for simplicity):
class WindowManager
{
private:
std::vector<Item*> m_itemlist;
}
My problem is how I'm meant to access this from the .cpp? I'd like to use it to have an array of Item type but I don't understand how to actually get to the point where I can add a newly instantiated Item, let's say button, to the array?
A bit of a rudimentary question but I've not had much luck with tutorials that cover std::vector.
If possible avoid using vector of pointers to Item. Use vector of Item directly.
class WindowManager
{
void addItem(Item const& item) { m_itemlist.push_back(item); }
private:
std::vector<Item> m_itemlist;
};
int main()
{
WindowManager wm;
Item i;
wm.addItem(i);
}
To add an item you could use a member function like this:
class WindowManager
{
private:
std::vector<Item *> m_itemlist;
public:
void addItem(Item *newItem);
}
in window_manager.cpp:
void WindowManager::addItem(Item *newItem)
{
m_itemlist.push_back(newItem);
}
see std::vector::push_back()

Returning an empty smart pointer [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a class that looks like this,
class A
{
std::shared_ptr<Type> ret;
public:
A()
{
ret=std::shared_ptr<Type>(new Type);
}
std::shared_ptr<Type> GetTypeA(){return ret;}
A (const A&a)
{
....
ret=a.ret;
}
};
class Type
{
A aa;
public:
Type(A*a):aa(*a){}
};
Somewhere in the client code, I call the method GetTypeA like this
void func(A*pA)
{
...
std::shared_ptr<Type> spT=pA->GetTypeA();
...
}
Debugging shows me that spT=empty after the call. But inside pA, ret value is NOT empty.
I notice some mistakes in your code :
A()
{
ret=std::shared_ptr<Type>(new Type);
}
"new Type" means you call default constructor for Type (Type::Type()), and you didn't write it in your sample. Try "new Type(*this)" to use your own constructor.
But to do this you need to change your Type class to:
class Type
{
A* aa; // Use a pointer
public:
Type::Type(A&a) :aa(&a){} // Use references
};
The problem is it's not resolving the "recursive aspect", depend your needs, I would use a static reference to A in the Type class...

C++ Reference Variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to use 'reference variables' in C++ classes?
I have the following code that I want to put into a class: (note KinectCV&)
KinectCV& kinect = freenect.createDevice(0);
kinect.some_init_functions();
while(condition) {
// getting frames from kinect and processing
kinect.some_processing_functions();
}
kinect.some_stopping_functions();
I'm trying to make a class and separate init, process and stop functions:
class MyKinect {
public:
KinectCV kinect;
void init(){
/* I cannot use the '& kinect = freenect.createDevice(0);' syntax, help me in this */
}
void process(){
kinect.some_processing_functions();
}
void stop(){
kinect.some_stopping_functions();
}
}
I cannot use the '& kinect = freenect.createDevice(0)
That is right, you cannot assign references; once initialized, they refer to the same object forever. What looks like an assignment in your code that works
KinectCV& kinect = freenect.createDevice(0);
is not an assignment, it's initialization. It can be rewritten using the initialization syntax instead of the assignment syntax, like this:
KinectCV& kinect(freenect.createDevice(0));
The reason behind it is that in C++ all initialization must happen in the constructor, not in a "designated initialization function". C++ has no idea that init is your initialization function; all it knows is that once the constructor is over, the object must be in a consistent state, included with all the references that it might hold.
Moving the initialization code into MyKinect's constructor will fix the problem:
class MyKinect {
public:
KinectCV kinect;
MyKinect() : kinect(freenect.createDevice(0)) {
}
}