Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why new Image failed?

Why new Image failed?

Scheduled Pinned Locked Moved C / C++ / MFC
c++winformsgraphicsdebuggingquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yu jian
    wrote on last edited by
    #1

    <pre lang="c++">
    Image* m_pImage;
    CString strTempIMGPath = m_strCurrentImagePath;
    wchar_t szFile[256] = {0};

    char \*pStr = strTempIMGPath.GetBuffer(0);
    int size = MultiByteToWideChar(GetACP(), 0, pStr, -1, NULL, 0);
    MultiByteToWideChar(GetACP(), 0, pStr, -1, szFile, size);
    strTempIMGPath.ReleaseBuffer();
    if (m\_pImage != NULL)
    {
        delete m\_pImage;
        m\_pImage = NULL;
    }
    

    <b> m_pImage = new Image(szFile, FALSE);</b>
    if(m_pImage->GetLastStatus() == Gdiplus::Status::Ok)
    {
    ...
    }

    After this statement,m_pImage = new Image(szFile, FALSE) the m_pImage is NULL, I do not know why create Image failed, after debug that I find that the szFile is a right PNG image file path. The last code used in a CDialog form, I use the CDialog::DoModel() to show this dialog in the CMyApp::InitInstance() I do not know whether I use this class Image too early, or I need use some code to initialize the GDI+ library?

    C S 2 Replies Last reply
    0
    • Y yu jian

      <pre lang="c++">
      Image* m_pImage;
      CString strTempIMGPath = m_strCurrentImagePath;
      wchar_t szFile[256] = {0};

      char \*pStr = strTempIMGPath.GetBuffer(0);
      int size = MultiByteToWideChar(GetACP(), 0, pStr, -1, NULL, 0);
      MultiByteToWideChar(GetACP(), 0, pStr, -1, szFile, size);
      strTempIMGPath.ReleaseBuffer();
      if (m\_pImage != NULL)
      {
          delete m\_pImage;
          m\_pImage = NULL;
      }
      

      <b> m_pImage = new Image(szFile, FALSE);</b>
      if(m_pImage->GetLastStatus() == Gdiplus::Status::Ok)
      {
      ...
      }

      After this statement,m_pImage = new Image(szFile, FALSE) the m_pImage is NULL, I do not know why create Image failed, after debug that I find that the szFile is a right PNG image file path. The last code used in a CDialog form, I use the CDialog::DoModel() to show this dialog in the CMyApp::InitInstance() I do not know whether I use this class Image too early, or I need use some code to initialize the GDI+ library?

      C Offline
      C Offline
      chaau
      wrote on last edited by
      #2

      replace the first line with this:

      Image* m_pImage = NULL;

      Otherwise, m_pImage is not NULL, and you are trying to delete uninitialised pointer, leadding to memory corruption

      1 Reply Last reply
      0
      • Y yu jian

        <pre lang="c++">
        Image* m_pImage;
        CString strTempIMGPath = m_strCurrentImagePath;
        wchar_t szFile[256] = {0};

        char \*pStr = strTempIMGPath.GetBuffer(0);
        int size = MultiByteToWideChar(GetACP(), 0, pStr, -1, NULL, 0);
        MultiByteToWideChar(GetACP(), 0, pStr, -1, szFile, size);
        strTempIMGPath.ReleaseBuffer();
        if (m\_pImage != NULL)
        {
            delete m\_pImage;
            m\_pImage = NULL;
        }
        

        <b> m_pImage = new Image(szFile, FALSE);</b>
        if(m_pImage->GetLastStatus() == Gdiplus::Status::Ok)
        {
        ...
        }

        After this statement,m_pImage = new Image(szFile, FALSE) the m_pImage is NULL, I do not know why create Image failed, after debug that I find that the szFile is a right PNG image file path. The last code used in a CDialog form, I use the CDialog::DoModel() to show this dialog in the CMyApp::InitInstance() I do not know whether I use this class Image too early, or I need use some code to initialize the GDI+ library?

        S Offline
        S Offline
        Santhosh G_
        wrote on last edited by
        #3

        // Used for GDI+ initialisation.
        ULONG_PTR m_gdiplusToken;

        // Initialization of GDI+ library.
        Gdiplus::GdiplusStartupInput gdiplusStartupInput;
        Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);

        The GdiplusStartup function initializes GDI+. Call GdiplusStartup before making any other GDI+ calls, and call GdiplusShutdown when you have finished using GDI+.

        Y 1 Reply Last reply
        0
        • S Santhosh G_

          // Used for GDI+ initialisation.
          ULONG_PTR m_gdiplusToken;

          // Initialization of GDI+ library.
          Gdiplus::GdiplusStartupInput gdiplusStartupInput;
          Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);

          The GdiplusStartup function initializes GDI+. Call GdiplusStartup before making any other GDI+ calls, and call GdiplusShutdown when you have finished using GDI+.

          Y Offline
          Y Offline
          yu jian
          wrote on last edited by
          #4

          Very Good answer, thank you very very much.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups