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

Attachments
#include "Weather.h"

using namespace Ogre;
using namespace MTP;

Weather::Weather(Ogre::SceneManager *scm, OgreAL::SoundManager *sm):
mSceneMgr(scm),mSoundMgr(sm),mIsLoaded(false)
{
}

Weather::~Weather()
{
}

void Weather::SaveScheme(char *fName)
{
	std::fstream fso;
	try
	{
		fso.open(fName,std::fstream::out | std::fstream::binary);
		if (fso.is_open())
		{
			fso.write((char*)&FSIGN,sizeof(MTPFILESIGN));
			fso.write((char*)&mScheme,sizeof(SceneArg));
			fso.close();
		}
	}catch(Exception& e)
	{
		Ogre::LogManager::getSingleton().logMessage("An exception has been thrown: "+e.getFullDescription());
	}
}

void Weather::LoadScheme(char *fName)
{
	std::fstream fso;
	MTPFILESIGN fTmp;
	try
	{
		fso.open(fName,std::fstream::in | std::fstream::binary);
		if (fso.is_open())
		{
			fso.read((char*)&fTmp,sizeof(MTPFILESIGN));
			if (fTmp == FSIGN)
			{
				fso.read((char*)&mScheme,sizeof(SceneArg));
				mIsLoaded = true;
				_update();
			}
			else
				Ogre::LogManager::getSingleton().logMessage("ERROR: file type is incorrect. Required WSC MTPFILESIGN !");
			fso.close();
		}
	}catch(Exception& e)
	{
		Ogre::LogManager::getSingleton().logMessage("An exception has been thrown: "+e.getFullDescription());
	}
}

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

SceneArg Weather::GetScheme(void)
{
	return mScheme;
}

void Weather::SetListenerPosition(WeatherListenerPosition wlp)
{
	mPlayerPosition = wlp;
}

WeatherListenerPosition Weather::GetListenerPosition()
{
	return mPlayerPosition;
}

void Weather::_update()
{
	if (mIsLoaded && mSceneMgr && mSoundMgr)
	{
		SetSceneView();
	}
}

void Weather::SetSceneView()
{
	if (mIsLoaded && mSceneMgr)
	{
		mSceneMgr->setSkyBox(true,mScheme.SAView.SVSkyBoxMat);
		mSceneMgr->setAmbientLight(mScheme.SAView.SVAmbientColor);
		if (mScheme.SAView.hasSVSL)
		{
			if (mSceneMgr->hasLight("__Weather__:Sun_Light"))
				mSceneMgr->destroyLight("__Weather__:Sun_Light");
			Light *_sunLight = mSceneMgr->createLight("__Weather__:Sun_Light");
			*_sunLight = mScheme.SAView.SVSunLight;
		}
	}
}

void Weather::SetSceneFog()
{
	if (mIsLoaded && mSceneMgr)
	{
		if (mScheme.hasSAFog)
		{
			mSceneMgr->setFog(mScheme.SAFog.SFMode, mScheme.SAFog.SFFogColor, mScheme.SAFog.SFExpDensity, mScheme.SAFog.SFLinearStart, mScheme.SAFog.SFLinearEnd);
		}
	}
}

MTPFILESIGN Weather::FSIGN = {{'W','S','C'},1,1};
#ifndef __Weather_H__
#define __Weather_H__

#include <Ogre.h>
#include <OgreStringConverter.h>
#include <OgreException.h>
#include <OgreAL.h>
#include <fstream>
#include "MTPStuff.h"

using namespace Ogre;
using namespace OgreAL;

namespace MTP
{
	enum WeatherListenerPosition {WLPEX,WLPIN,WLPIT};
	struct SceneParticulation
	{
	public:
		SceneParticulation(){};
		~SceneParticulation(){};
		ParticleSystem SPParticle;
	};
	struct SceneFog
	{
	public:
		SceneFog():
			SFMode(FOG_NONE),SFExpDensity(0),SFLinearStart(0),SFLinearEnd(0){}
		~SceneFog(){};
		FogMode SFMode;
		ColourValue SFFogColor;
		Real SFExpDensity;
		Real SFLinearStart;
		Real SFLinearEnd;
	};
	struct SceneSoundAmbient
	{
	public:
		SceneSoundAmbient():
			isSSABEbuffered(false),isSSABIbuffered(false),isSSABTbuffered(false){}
		~SceneSoundAmbient(){};
		char SSABackgroundEx[256];
		bool isSSABEbuffered;
		char SSABackgroundIn[256];
		bool isSSABIbuffered;
		char SSABackgroundIt[256];
		bool isSSABTbuffered;
	};
	struct SceneSound
	{
	public:
		SceneSound():
			SSRand(0),SSCount(0),SSStep(0){}
		~SceneSound(){};
		SceneSoundAmbient SSAmbient;
		char SSSoundPrefix[256];
		char SSSoundPostfix[32];
		int SSCount;
		int SSStep;
		float SSRand;
	};
	struct SceneView
	{
	public:
		SceneView():
			hasSVSL(false){}
		~SceneView(){};
		char SVSkyBoxMat[256];
		Light SVSunLight;
		bool hasSVSL;
		ColourValue SVAmbientColor;
	};
	struct SceneArg
	{
	public:
		SceneArg():
	hasSAFog(false),hasSAParticles(false),hasSASound(false){}
		~SceneArg(){};
		SceneView SAView;
		SceneFog SAFog;
		SceneSound SASound;
		SceneParticulation SAParticles;
		bool hasSAParticles;
		bool hasSAFog;
		bool hasSASound;
	};

	class Weather
	{
	public:
		// kontruktory a detruktory
		Weather(SceneManager*,SoundManager*);
		~Weather();

		// uloi schmu
		virtual void SaveScheme(char *);
		// nata schmu
		virtual void LoadScheme(char *);
		// nastav schmu
		virtual void SetScheme(SceneArg*);
		// vrti schmu
		virtual SceneArg GetScheme(void);
		// nastav pozciu poslucha
		virtual void SetListenerPosition(WeatherListenerPosition);
		// zska pozciu poslucha
		virtual WeatherListenerPosition GetListenerPosition(void);
	private:
		// skontroluje, prp. obnov / nata poasie
		virtual void _update(void);
		// nastavi atribty scny
		virtual void SetSceneView(void);
		// nastavi atribty hmly
		virtual void SetSceneFog(void);
	private:
		// schma poasia
		SceneArg mScheme;
		// prehrvan zvuk
		Sound *mSoundPlayer;
		// pozcia poslucha
		WeatherListenerPosition mPlayerPosition;
		///
		// sprvca scny
		SceneManager *mSceneMgr;
		// sprvca zvukov
		SoundManager *mSoundMgr;
		//
		bool mIsLoaded;
	public:
		///
		// MTP File Sign
		static MTPFILESIGN FSIGN;
	};
}

#endif

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.

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).

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

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.

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.

This question has already been answered. Start a new discussion instead.