Access violation in a dialog based application that shows jpg 
Author Message
 Access violation in a dialog based application that shows jpg

Hi,

I found a function that load jpg files into an IStream, to show it into a
window.
I want then make a test with an app wizard project dialog based with Visual
C++ 6..
My dialog box is very simple and manage only:
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
Into the OnInitDialog() ,  I call:
LoadPictureFile("10.jpg");
the function that load the jpg file into an IStream.

***************************************************************************
The problem come out randomly,
or when the program start or when I am moving the dialog box with my mouse
or when I cover it with other applications and then I come back to the
dialog.
Windows give me an access violation message and the debug shows me that the
program stopped in
CWinThread::PumpMessage
where there is this test:
if (!::GetMessage(&m_msgCur, NULL, NULL, NULL))
***************************************************************************

Into the OnPaint() of the dialog I use gpPicture of type LPPICTURE.
gpPicture has been initialised into the LoadPictureFile
and then I draw the jpg on the dialog like that:
gpPicture->Render(dc.m_hDC, 50, 50, nWidth, nHeight, 0, hmHeight,
hmWidth, -hmHeight, &rect);
with all parameters already initialised.

Now I show you the function to load the jpg file that is using
OleLoadPicure():

// This function loads a file into an IStream.
void LoadPictureFile(LPCTSTR szFile)
{
   // open file
 HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0,
NULL);
 _ASSERTE(INVALID_HANDLE_VALUE != hFile);

 // get file size
 DWORD dwFileSize = GetFileSize(hFile, NULL);
 _ASSERTE(-1 != dwFileSize);

 LPVOID pvData = NULL;
 // alloc memory based on file size
 HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
 _ASSERTE(NULL != hGlobal);

 pvData = GlobalLock(hGlobal);
 _ASSERTE(NULL != pvData);

 DWORD dwBytesRead = 0;
 // read file and store in global memory
 BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
 _ASSERTE(FALSE != bRead);
 GlobalUnlock(hGlobal);
 CloseHandle(hFile);

 LPSTREAM pstm = NULL;
 // create IStream* from global memory
 HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
 _ASSERTE(SUCCEEDED(hr) && pstm);

 // Create IPicture from image file
 if (gpPicture)
  gpPicture->Release();
 hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID
*)&gpPicture);
 _ASSERTE(SUCCEEDED(hr) && gpPicture);
 pstm->Release();

// InvalidateRect(ghWnd, NULL, TRUE);

Quote:
}

Thank you for the attention.
I am investigating in this problem and I will appreciate any suggestion from
you!

Maurus



Mon, 04 Jul 2005 12:59:11 GMT  
 Access violation in a dialog based application that shows jpg

Hi,
 Could you post your whole OnPaint handler, so we can get more clues maybe.

--
Regards,
  Kobi Ben Tzvi


Quote:
> Hi,

> I found a function that load jpg files into an IStream, to show it into a
> window.
> I want then make a test with an app wizard project dialog based with
Visual
> C++ 6..
> My dialog box is very simple and manage only:
> ON_WM_PAINT()
> ON_WM_QUERYDRAGICON()
> Into the OnInitDialog() ,  I call:
> LoadPictureFile("10.jpg");
> the function that load the jpg file into an IStream.

***************************************************************************
Quote:
> The problem come out randomly,
> or when the program start or when I am moving the dialog box with my mouse
> or when I cover it with other applications and then I come back to the
> dialog.
> Windows give me an access violation message and the debug shows me that
the
> program stopped in
> CWinThread::PumpMessage
> where there is this test:
> if (!::GetMessage(&m_msgCur, NULL, NULL, NULL))

***************************************************************************
Quote:

> Into the OnPaint() of the dialog I use gpPicture of type LPPICTURE.
> gpPicture has been initialised into the LoadPictureFile
> and then I draw the jpg on the dialog like that:
> gpPicture->Render(dc.m_hDC, 50, 50, nWidth, nHeight, 0, hmHeight,
> hmWidth, -hmHeight, &rect);
> with all parameters already initialised.

> Now I show you the function to load the jpg file that is using
> OleLoadPicure():

> // This function loads a file into an IStream.
> void LoadPictureFile(LPCTSTR szFile)
> {
>    // open file
>  HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING,
0,
> NULL);
>  _ASSERTE(INVALID_HANDLE_VALUE != hFile);

>  // get file size
>  DWORD dwFileSize = GetFileSize(hFile, NULL);
>  _ASSERTE(-1 != dwFileSize);

>  LPVOID pvData = NULL;
>  // alloc memory based on file size
>  HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
>  _ASSERTE(NULL != hGlobal);

>  pvData = GlobalLock(hGlobal);
>  _ASSERTE(NULL != pvData);

>  DWORD dwBytesRead = 0;
>  // read file and store in global memory
>  BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
>  _ASSERTE(FALSE != bRead);
>  GlobalUnlock(hGlobal);
>  CloseHandle(hFile);

>  LPSTREAM pstm = NULL;
>  // create IStream* from global memory
>  HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
>  _ASSERTE(SUCCEEDED(hr) && pstm);

>  // Create IPicture from image file
>  if (gpPicture)
>   gpPicture->Release();
>  hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID
> *)&gpPicture);
>  _ASSERTE(SUCCEEDED(hr) && gpPicture);
>  pstm->Release();

> // InvalidateRect(ghWnd, NULL, TRUE);
> }

> Thank you for the attention.
> I am investigating in this problem and I will appreciate any suggestion
from
> you!

> Maurus



Mon, 04 Jul 2005 13:10:27 GMT  
 Access violation in a dialog based application that shows jpg



Quote:

> Hi,
>  Could you post your whole OnPaint handler, so we can get more clues
maybe.

> --
> Regards,
>   Kobi Ben Tzvi

Hi Kobi!
Thanx for your reply,
I show you my OnPaint handler.
If you need some other chunk of my code to help me, I am here:

void CCardGameDlg::OnPaint()
{
 if (IsIconic())
 {
  CPaintDC dc(this); // device context for painting

  SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

  // Center icon in client rectangle
  int cxIcon = GetSystemMetrics(SM_CXICON);
  int cyIcon = GetSystemMetrics(SM_CYICON);
  CRect rect;
  GetClientRect(&rect);
  int x = (rect.Width() - cxIcon + 1) / 2;
  int y = (rect.Height() - cyIcon + 1) / 2;

  // Draw the icon
  dc.DrawIcon(x, y, m_hIcon);
 }
 else
 {
  if (gpPicture)
  {
     CPaintDC dc(this); // device context for painting
   // get width and height of picture
   long hmWidth;
   long hmHeight;
   gpPicture->get_Width(&hmWidth);
   gpPicture->get_Height(&hmHeight);
   // convert himetric to pixels
   int nWidth = MulDiv(hmWidth, GetDeviceCaps(dc.m_hDC, LOGPIXELSX),
HIMETRIC_INCH);
   int nHeight = MulDiv(hmHeight, GetDeviceCaps(dc.m_hDC, LOGPIXELSY),
HIMETRIC_INCH);
     CRect rect;
   //RECT rc;
   GetClientRect(&rect);
   // display picture using IPicture::Render
   gpPicture->Render(dc.m_hDC, 50, 50, nWidth, nHeight, 0, hmHeight,
hmWidth, -hmHeight, &rect);
  }

  CDialog::OnPaint();
 }

Quote:
}

Maurus


Mon, 04 Jul 2005 13:32:15 GMT  
 Access violation in a dialog based application that shows jpg
Hi Maurus,
I just wrote sample MFC dialog application with code you posted and it works
without any problems. The only thing I didn't find definition for
HIMETRIC_INCH and used some constant value, but its shouldn't have affected
that.  What is the size of jpg that you are loading ? did you try loading
other jpgs ?

Do you have any other code that can gpPicture ?

--
Regards,
  Kobi Ben Tzvi


Quote:



> > Hi,
> >  Could you post your whole OnPaint handler, so we can get more clues
> maybe.

> > --
> > Regards,
> >   Kobi Ben Tzvi

> Hi Kobi!
> Thanx for your reply,
> I show you my OnPaint handler.
> If you need some other chunk of my code to help me, I am here:

> void CCardGameDlg::OnPaint()
> {
>  if (IsIconic())
>  {
>   CPaintDC dc(this); // device context for painting

>   SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

>   // Center icon in client rectangle
>   int cxIcon = GetSystemMetrics(SM_CXICON);
>   int cyIcon = GetSystemMetrics(SM_CYICON);
>   CRect rect;
>   GetClientRect(&rect);
>   int x = (rect.Width() - cxIcon + 1) / 2;
>   int y = (rect.Height() - cyIcon + 1) / 2;

>   // Draw the icon
>   dc.DrawIcon(x, y, m_hIcon);
>  }
>  else
>  {
>   if (gpPicture)
>   {
>      CPaintDC dc(this); // device context for painting
>    // get width and height of picture
>    long hmWidth;
>    long hmHeight;
>    gpPicture->get_Width(&hmWidth);
>    gpPicture->get_Height(&hmHeight);
>    // convert himetric to pixels
>    int nWidth = MulDiv(hmWidth, GetDeviceCaps(dc.m_hDC, LOGPIXELSX),
> HIMETRIC_INCH);
>    int nHeight = MulDiv(hmHeight, GetDeviceCaps(dc.m_hDC, LOGPIXELSY),
> HIMETRIC_INCH);
>      CRect rect;
>    //RECT rc;
>    GetClientRect(&rect);
>    // display picture using IPicture::Render
>    gpPicture->Render(dc.m_hDC, 50, 50, nWidth, nHeight, 0, hmHeight,
> hmWidth, -hmHeight, &rect);
>   }

>   CDialog::OnPaint();
>  }
> }

> Maurus



Mon, 04 Jul 2005 13:58:38 GMT  
 Access violation in a dialog based application that shows jpg



Quote:
> Hi Maurus,
> I just wrote sample MFC dialog application with code you posted and it
works
> without any problems. The only thing I didn't find definition for
> HIMETRIC_INCH and used some constant value, but its shouldn't have
affected
> that.  What is the size of jpg that you are loading ? did you try loading
> other jpgs ?

> Do you have any other code that can gpPicture ?

Hi Kobi,

did you try to move around the dialog and to cover it with other
applications?
I am using Windows ME, so I don't know if this OS can give me the problem
that you don't have.
Anyway, here there are all the constansts that I am using:

#define MAX_LOADSTRING 100
#define HIMETRIC_INCH 2540
#define MAP_LOGHIM_TO_PIX(x,ppli)   ( ((ppli)*(x) + HIMETRIC_INCH/2) /
HIMETRIC_INCH )

// Global Variables:
HINSTANCE hInst;        // current instance
TCHAR szTitle[MAX_LOADSTRING];   // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
LPPICTURE gpPicture = NULL;

the jpg file that I am loading is very small (63 x 90) pixels.
I only tried to load these kind of jpg because they are for a card game.
The application did not have any problem if I leaved the project with a
Windows with a menu where I loaded the file.
The problems came when I built a new application dialog based by the app
wizard.

Quote:
> Do you have any other code that can gpPicture ?

Can you explain that? I did not understand you

Maurus



Mon, 04 Jul 2005 14:33:37 GMT  
 Access violation in a dialog based application that shows jpg


Quote:

> > Hi Maurus,
> > I just wrote sample MFC dialog application with code you posted and it
> works
> > without any problems. The only thing I didn't find definition for
> > HIMETRIC_INCH and used some constant value, but its shouldn't have
> affected
> > that.  What is the size of jpg that you are loading ? did you try
loading
> > other jpgs ?

> > Do you have any other code that can gpPicture ?

I just want to tell you another thing.

Yuo should try in debug mode because, it seems that in the release mode,
there is not the access violation problem, or maybe it is not visible.
In debug mode, you should move the dialog many times, and the problem seems
to appear more often when the jpg goes out of the screen (even when another
windows cover it) and then it is back to the screen again.

Maurus



Mon, 04 Jul 2005 15:42:06 GMT  
 Access violation in a dialog based application that shows jpg


Quote:



> > Hi Maurus,
> > I just wrote sample MFC dialog application with code you posted and it
> works
> > without any problems. The only thing I didn't find definition for
> > HIMETRIC_INCH and used some constant value, but its shouldn't have
> affected
> > that.  What is the size of jpg that you are loading ? did you try
loading
> > other jpgs ?

> > Do you have any other code that can gpPicture ?

> Hi Kobi,

> did you try to move around the dialog and to cover it with other
> applications?
> I am using Windows ME, so I don't know if this OS can give me the problem
> that you don't have.
> Anyway, here there are all the constansts that I am using:

> #define MAX_LOADSTRING 100
> #define HIMETRIC_INCH 2540
> #define MAP_LOGHIM_TO_PIX(x,ppli)   ( ((ppli)*(x) + HIMETRIC_INCH/2) /
> HIMETRIC_INCH )

> // Global Variables:
> HINSTANCE hInst;        // current instance
> TCHAR szTitle[MAX_LOADSTRING];   // The title bar text
> TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
> LPPICTURE gpPicture = NULL;

> the jpg file that I am loading is very small (63 x 90) pixels.
> I only tried to load these kind of jpg because they are for a card game.
> The application did not have any problem if I leaved the project with a
> Windows with a menu where I loaded the file.
> The problems came when I built a new application dialog based by the app
> wizard.

> Maurus

Yes I tried moving it, and all other possible things, works without hitch.

Quote:
> > Do you have any other code that can gpPicture ?
> Can you explain that? I did not understand you

I meant if you have any code that might overwrite for example the gpPicture
pointer?
--
Regards,
  Kobi Ben Tzvi


Mon, 04 Jul 2005 15:31:19 GMT  
 Access violation in a dialog based application that shows jpg
Hi Maurus,
I even checked it with Numega Boundschecker and no problems, reported. And
yes I moved it lots of times, covered it with other windows...

Maybe if the project isn't confidential you can post/email me and try it.
--
Regards,
  Kobi Ben Tzvi


Quote:



> > > Hi Maurus,
> > > I just wrote sample MFC dialog application with code you posted and it
> > works
> > > without any problems. The only thing I didn't find definition for
> > > HIMETRIC_INCH and used some constant value, but its shouldn't have
> > affected
> > > that.  What is the size of jpg that you are loading ? did you try
> loading
> > > other jpgs ?

> > > Do you have any other code that can gpPicture ?

> I just want to tell you another thing.

> Yuo should try in debug mode because, it seems that in the release mode,
> there is not the access violation problem, or maybe it is not visible.
> In debug mode, you should move the dialog many times, and the problem
seems
> to appear more often when the jpg goes out of the screen (even when
another
> windows cover it) and then it is back to the screen again.

> Maurus



Mon, 04 Jul 2005 15:54:40 GMT  
 Access violation in a dialog based application that shows jpg



Quote:
> Hi Maurus,
> I even checked it with Numega Boundschecker and no problems, reported. And
> yes I moved it lots of times, covered it with other windows...

> Maybe if the project isn't confidential you can post/email me and try it.
> --
> Regards,
>   Kobi Ben Tzvi

No!
It is absolutely no confidential!
I am just learning to develop an MFC application dialog based.
Thank you very much Kobi, I will send you a zip with the whole project and
the jpg file that I am loading.
I have Windows ME installed, so maybe it could be the difference between my
version and yours.

Maurus



Mon, 04 Jul 2005 16:13:19 GMT  
 Access violation in a dialog based application that shows jpg

Hi again Maurus,

Your project also runs without any problems, which makes me believe that
something is not right in Win Me or Windows XP that I am running, is more
"forgiving" and not doing any problems.
Anyway, I would suggest that you install latest service pack/updates and
try.

--
Regards,
  Kobi Ben Tzvi


Quote:



> > Hi Maurus,
> > I even checked it with Numega Boundschecker and no problems, reported.
And
> > yes I moved it lots of times, covered it with other windows...

> > Maybe if the project isn't confidential you can post/email me and try
it.
> > --
> > Regards,
> >   Kobi Ben Tzvi

> No!
> It is absolutely no confidential!
> I am just learning to develop an MFC application dialog based.
> Thank you very much Kobi, I will send you a zip with the whole project and
> the jpg file that I am loading.
> I have Windows ME installed, so maybe it could be the difference between
my
> version and yours.

> Maurus



Mon, 04 Jul 2005 16:58:13 GMT  
 Access violation in a dialog based application that shows jpg



Quote:

> Hi again Maurus,

> Your project also runs without any problems, which makes me believe that
> something is not right in Win Me or Windows XP that I am running, is more
> "forgiving" and not doing any problems.
> Anyway, I would suggest that you install latest service pack/updates and
> try.

> --
> Regards,
>   Kobi Ben Tzvi

Thank you again for your help Kobi!

I will try to install the last service pack for visual studio 6 and then I
will try the same project.
If the error is still there, it will means that there is something strange
in the Windows API calls that makes some conflicts on a Windows ME OS.

Maurus



Mon, 04 Jul 2005 18:07:49 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. How to not show task button with a base dialog application

2. How not show task button with a base dialog application

3. ActiveX Control not Showing on Dialog Based Application

4. Initially showing a dialog base application hidden - how?

5. Showing taskbar visible dialog from dialog based app

6. Cancel button doesn't show up on access violation

7. Showing a modal dialog in a modal dialog application

8. Adding Another MFC Dialog to the Dialog base application

9. Trapping Keystrokes in a Dialog Box or Dialog based application

10. Executing modal tabbed dialog from a dialog-based application

11. Positioning of dialogs in a dialog based application

12. Replacing the dialog box of a dialog-based application

 

 
Powered by phpBB® Forum Software