Member Avatar for Thew

Hello,
I need help to solve the problem in my app that uses structs.
I have for example this struct:

struct SimpleStruct
{
    SimpleStruct():
        hasVars(false){}
    ~SimpleStruct();
    AnotherStruct SimpleVariable;
    bool hasVars;
};

and in the code, initializing this struct goes ok, but when the class, that has this struct as a variable like:

class SimpleClass
{
public:
    SimpleClass();
    ~SimpleClass();
private:
    SimpleStruct mStruct;
};

set this variable with memcpy(&mStruct,mSource,sizeof(SimpleStruct)) (mSource is the source, created somewhere else) and this class is going to be destroyed, it raises exception like this:
Unhandled exception at 0x10c4ee64 in SampleApplication.exe: 0xC0000005: Access violation reading location 0xfeeeff22.
But when I don't set this mStruct variable with new one, nothing happens and the application exits successfuly. So my questions are:
1. How can I set the struct variable (I tried mStruct = mSource and memcpy(...))
2. Why is this happening

Recommended Answers

All 7 Replies

Do you mean this function?

void Weather::SetScheme(SceneArg *sceneArg)
{
	memcpy(&mScheme,sceneArg,sizeof(SceneArg));
	mIsLoaded = true;
	_update();
}

The only problem I see in that function is that sceneArg should be declared const. Also check that it is a valid pointer.

Member Avatar for Thew

The function looked like that before i made some changes:

void Weather::SetScheme(SceneArg sceneArg)
{
    mScheme = sceneArg;
    mIsLoaded = true;
    _update();
}

but everytime I run it, I get the exception which is raised when its trying to destroy the structs that you can see in the code. So when I don't made any changes to any struct, everything goes ok, the app is running, but when I try to set the structured variables with this function Weather::SetScheme, it will crash on destroy...
I also tried to make it as class, to remove the constructor and destructors, but...

I suspect the problem is actually somewhere else. Will you post the code that calls SetScheme() -- hopefully attach the rest of the problem so that I can compile and run it, assuming this is for MS-Windows operating system. Instead of attaching the files one at a time just use WinZip and zip them all then attach the *.zip file (remove object files and other compiler-generated files).

Member Avatar for Thew

I'm not sure if you will be able to compile it, but this is all the code for sample application. This project uses also Ogre3D, OgreAL, OgreNewt, OpenAL, vorbis and ogg, so...
The class that runs the SetScheme is Main.cpp in CreateScene

Member Avatar for Thew

I tried to delete all the struct definitions and start over. I found when I use SceneParticulation, the error will be raised. Otherwise everything goes ok. But what's wrong in this definition:

struct SceneParticulation
{
public:
    SceneParticulation(){};
    ~SceneParticulation(){}; // here the program stops and fails
    ParticleSystem SPParticle;
};

I tried to delete all the struct definitions and start over. I found when I use SceneParticulation, the error will be raised. Otherwise everything goes ok. But what's wrong in this definition:

struct SceneParticulation
{
public:
    SceneParticulation(){};
    ~SceneParticulation(){}; // here the program stops and fails
    ParticleSystem SPParticle;
};

Nothing is wrong with the definition as such, however, you also need to consider the member variable ParticleSystem SPParticle - what does that object hold and what will its destructor do. I think you should drop the habbit of memcpy'ing objects which are more than simple structs - simple meaning structs that only contain primitive types (int, char etc) and nothing else. I.e. instead of memcpy, write copy constructors/assignment operators where needed.

Member Avatar for Thew

Yes, I found that everytime the ParticleSystem SPParticle is going to be destroyed, it will crash. Maybe I need to create some other struct that will hold the informations thah ParticleSystem cannot hold.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.