   C++ static hell
  I have the following code:class ScreenVirtualityCalc {    public:void MakeScreenedCopy (TransImage &ImageTarget, double SizeX, double SizeY, ScreenInterface &SI;public:static DWORD PercentToPixel (const DWORD AxisLength, const double Percent);};class TransImage {    private:ScreenVirtualityCalc ScreenCalc;};voidTransImage::MakeScreenedCopy (TransImage &ImageTarget, double SizeX, double SizeY, ScreenInterface &SI){DWORD TargetX, TargetY, TargetRealX, TargetRealY;TargetX = this->ScreenCalc.PercentToPixel (SI.Width, SizeX); // runtime error line}I get a runtime error: Unhandled exception at v0p01.exe: 0xc00000005 access violation.What the hell cause that? 
  I'm sorry but I can't explain to you why this does not work, but I believe I can tell you how to correct it. You could try to call static functions using :voidTransImage::MakeScreenedCopy (TransImage &ImageTarget, double SizeX, double SizeY, ScreenInterface &SI){DWORD TargetX, TargetY, TargetRealX, TargetRealY;TargetX = ScreenVirtualityCalc:ercentToPixel( SI.Width, SizeX ) ;  }  EDIT : : + p =  !!!Victor Nicollet, INT13 game programmer  [edited by - ToohrVyk on February 14, 2004 11:03:19 AM];quote:Original post by ToohrVyk  I''m sorry but I can''t explain to you why this does not work, but I believe I can tell you how to correct it. You could try to call static functions using :voidTransImage::MakeScreenedCopy (TransImage &ImageTarget, double SizeX, double SizeY, ScreenInterface &SI){DWORD TargetX, TargetY, TargetRealX, TargetRealY;TargetX = ScreenVirtualityCalc:ercentToPixel( SI.Width, SizeX ) ;  }   EDIT : : + p =  !!!Victor Nicollet, INT13 game programmer  [edited by - ToohrVyk on February 14, 2004 11:03:19 AM]I did it and I still have the same problem. runtime error.;  Could you please post the code to the PercentToPixel function?Also, is TransImage::MakeScreenedCopy a static function?   Victor Nicollet, INT13 game programmer ;  The problem begun since I used a pointer to function, in the following code:class ScreenImage {public:void InitializeScreenImage (TransImage & SourceImage, TransImage & TargetImage, double SizeX, double SizeY, ScreenInterface & SI);void Update();void Update(double SizeX, double SizeY);private:double SizeX, SizeY;ScreenInterface * pSI;void * pData;void * pUpdateData;void (*pUpdate)(void * pData, void * pUpdateData, double SizeX, double SizeY, ScreenInterface & SI);};voidTransImageUpdate (void * pData, void * pUpdateData, double SizeX, double SizeY, ScreenInterface & SI){TransImage * pSource, * pTarget;pSource = (TransImage *)pUpdateData;pTarget = (TransImage *)pData;pSource->MakeScreenedCopy (*pTarget, SizeX, SizeY, SI);}voidScreenImage::InitializeScreenImage (TransImage & SourceImage, TransImage & TargetImage, double SizeX, double SizeY, ScreenInterface &SI){this->pData = &TargetImage;this->pUpdateData = &SourceImage;this->SizeX = SizeX;this->SizeY = SizeY;this->pUpdate = TransImageUpdate;}voidScreenImage::Update (){this->pUpdate (this->pData, this->pUpdateData, this->SizeX, this->SizeY, *this->pSI);}The code of percenttopixel is:inline DWORDScreenVirtualityCalc:ercentToPixel (const DWORD AxisLength, const double Percent){return floor((double)AxisLength*Percent);}I tried it with a not inline function and it still did the same runtime error. ;  Access violation means you''ve tried to look at, or write to, memory you''re not allowed to. In general, if you get something like that look for any pointers or references that aren''t initialized, then look for array overruns.In this particular case, you''ve got a ''this'' pointer dereferenced on the line you say is causing the problem - stick in a ''if (this == NULL) OutputDebugString("AARGH DIE!\n");'' just before that line and see if things die. Of course, if you''re not using MSVC (or at least some Windows IDE, I''m not sure how widespread OutputDebugString() is) then replace it with printf() or something.If that doesn''t help, make sure your references aren''t broken. Check whatever function is calling MakeScreenedCopy() and see what it''s passing in.Good luck - these sorts of challenges are what separate the C(++)programmers from the VB programmers. ;quote:Original post by fractoid   Access violation means you've tried to look at, or write to, memory you're not allowed to. In general, if you get something like that look for any pointers or references that aren't initialized, then look for array overruns.In this particular case, you've got a 'this' pointer dereferenced on the line you say is causing the problem - stick in a 'if (this == NULL) OutputDebugString("AARGH DIE!\n");' just before that line and see if things die. Of course, if you're not using MSVC (or at least some Windows IDE, I'm not sure how widespread OutputDebugString() is) then replace it with printf() or something.If that doesn't help, make sure your references aren't broken. Check whatever function is calling MakeScreenedCopy() and see what it's passing in.Good luck - these sorts of challenges are what separate the C(++)programmers from the VB programmers.  When I use this's members it works.The only problem I have is with the static function, and the problem happens even when I call it via the class and not via the instance of the class at this.Which is very strange because what sort of reference problem would cause a prohibited access to a static member function of a class? [edited by - The C modest god on February 14, 2004 11:46:27 AM];  Try calling the static function from somewhere else in the code. And try replacing the arugments with constants. Did you go step-by-step to pinpoint where the error occurs?These are basic debugging techniques...What compiler are you using?Cédric ;  I have solved the problem :D.Eventually, it was much more simpler then I thought and It nothing to do with the static functions.Thanks for all the help.   [edited by - The C modest god on February 14, 2004 11:57:50 AM];  Unless I missed it (it''s nearly 1am, I''m tired : P ) you haven''t initialized ScreenImage:: pSI - there''s no this->pSI = &SIin your ScreenImage::InitializeScreenImage() or other functions, but then I''m not sure if you use that either.