Thread: Enum (Access violation) Crash

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    7

    Enum (Access violation) Crash

    Hello All,

    Small console code crashing when it reach to enum (T12::a4) variable. Same thing works fine on GUI.

    error message:

    (First-chance exception at 0x0F3FCAB4 (msvcr100d.dll) in ConsoleApplication2.exe: 0xC0000005: Access violation reading location 0x00000000.
    If there is a handler for this exception, the program may be safely continued.)

    Code:
    funA()
    {
    char one[100] = "7a-11-2";
    char two[100] = "19.32";
    char three[25] = "hello";
    char four[25] = "hello";
    
    m_pVar = new T12::CClassA(two, one, three, four, T12::a4);
    }
    enum.h
    Code:
    namespace T12
    {
    enum em { a1 = 0, a2 = 1, a3 = 2, a4 = 3};
    }
    .h
    Code:
    namespace T12
    {
    CClassA
    {
    public:
    CClassA(const std::string& two, const std::string& one, const  std::string& three, const std::string& four, em eVar);
    };
    
    }
    How to solve this.
    Answers Appreciated.
    Last edited by marvic; 02-11-2017 at 01:11 AM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Way to provide an abundance of information. No one is going to be able to reproduce your error like this. Post something small that we can compile and look at ourselves. Look here for help in doing that.

    For the shot in the dark answer you deserve, perhaps m_pvar was leaked by funA() - since it looks like you just create the object and fall off the end of the function. When you actually try to use m_pvar, boom!

    Perhaps a debugger pointed you at the line of your mistake.

    If you mean to write that funA returns a handle to an object then write that, and call it accordingly:

    Code:
    T12::CClassA *funA(void); // function prototype - void kept in for unambiguous sake
    
    int main()
    {
        T12::CClassA *handle = funA();
       // use handle
       delete handle; 
    }
    Compile with warnings on in the future too! I thought C++ was supposed to get rid of implicit return values....
    Last edited by whiteflags; 02-11-2017 at 01:46 AM.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    7
    Thanks for replay,
    This is code should be clear

    Code:
    T12::CTemp* FunA();
    
    T12::CClassA* m_pVar;
    
    int main()
    {
    FunA(); 
    
    return 0;
    }
    
    T12::CTemp* FunA()
    {
    char one[100] = "7a-11-2";
    char two[100] = "19.32";
    char three[25] = "hello";
    char four[25] = "hello";
    m_pVar = NULL; -------> Correct Way?
    
    m_pVar = new T12::CClassA(two, one, three, four, T12::a4);
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > This is code should be clear
    That still isn't a Short, Self Contained, Correct Example

    Few will spend 10's of minutes guessing what all the missing code is in your snippet, only to fail to reproduce the issue you're facing.

    Here's my 5 minutes.
    Code:
    #include <iostream>
    #include <string>
    
    namespace T12
    {
      enum em { a1 = 0, a2 = 1, a3 = 2, a4 = 3};
    }
    
    namespace T12
    {
      class CClassA
      {
        public:
          CClassA(const std::string& two, const std::string& one, const  std::string& three, const std::string& four, em eVar) {
          }
          CClassA* FunA();
      };
    }
    
    T12::CClassA* m_pVar;
    
    int main()
    {
      FunA(); //!!?? what
      return 0;
    }
    
    T12::CClassA* FunA()
    {
      char one[100] = "7a-11-2";
      char two[100] = "19.32";
      char three[25] = "hello";
      char four[25] = "hello";
      m_pVar = new T12::CClassA(two, one, three, four, T12::a4);
    }
    Edit it so it
    a) reproduces your problem
    b) is something we can just copy/paste/compile and test in 30 seconds without wasting time guessing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2016
    Posts
    7
    FunA()is not member of CClassA,
    it is member of T12::CTemp* FunA();

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Whatever - post something we can copy/paste.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2016
    Posts
    7
    This should be ok
    Code:
    #include <iostream>
    #include <string>
     
    namespace T12
    {
      enum em { a1 = 0, a2 = 1, a3 = 2, a4 = 3};
    }
     
    namespace T12
    {
      class CClassA
      {
        public:
          CClassA(const std::string& two, const std::string& one, const  std::string& three, const std::string& four, em eVar) {
          }
      };
    }
     
    namespace T12
    {
    class CTemp
    {
    public:
     FunA();
    };
    }
    
    T12::CClassA* m_pVar;
     
    int main()
    {
      FunA(); //!!?? what --> Call to FunA()
      return 0;
    }
     
    T12::CTemp* FunA()
    {
      char one[100] = "7a-11-2";
      char two[100] = "19.32";
      char three[25] = "hello";
      char four[25] = "hello";
      m_pVar = NULL;
      m_pVar = new T12::CClassA(two, one, three, four, T12::a4);
    }

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, I had to remove a bunch of stray '\240' characters (not sure where they came from though) and I had to move the definition of FunA() directly above main(). Unfortunately, it works for me. Didn't produce the problem.

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by whiteflags View Post
    Well, I had to remove a bunch of stray '\240' characters (not sure where they came from though)
    \240 (0xa0) is the code for a non-breaking space in the Widows-1252 character set.

    OP, this works:
    Code:
    #include <iostream>
    #include <string>
      
    namespace T12
    {
      // If the values are just 0,1,2,3,... then they don't need to be specified.
      enum em { a1, a2, a3, a4 };
    }
      
    namespace T12
    {
      class CClassA
      {
        public:
          CClassA(const std::string& two, const std::string& one,
                  const std::string& three, const std::string& four, em eVar)
          {
          }
      };
    }
      
    namespace T12
    {
      class CTemp
      {
      public:
        // FunA needs a return type.
        void FunA();
      };
    }
     
    T12::CClassA* m_pVar;
      
    int main()
    {
      // You need an object to call a non-static class member.
      T12::CTemp ct;
      ct.FunA();
      return 0;
    }
    
    // You need to specify the class that FunA belongs to.
    void T12::CTemp::FunA()
    {
      char one[100] = "7a-11-2";
      char two[100] = "19.32";
      char three[25] = "hello";
      char four[25] = "hello";
      m_pVar = new T12::CClassA(two, one, three, four, T12::a4);
    }
    Last edited by algorism; 02-11-2017 at 08:40 AM.

  10. #10
    Registered User
    Join Date
    Nov 2016
    Posts
    7
    Thank you, it is working. But when i create thw FunA() to DLL it is crashing at enum. With above violation message.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > But when i create thw FunA() to DLL it is crashing at enum.
    More drip-fed information, so now it's only a problem if a DLL is involved.

    DLLs in Visual C++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Nov 2016
    Posts
    7
    After creating Dll, in main console, here it is crashing here.

    Code:
    m_pVar = new T12::CClassA(two, one, three, four, T12::a4);
    Last edited by marvic; 02-12-2017 at 05:58 AM.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Before posting, copy & paste the code you're going to post into a source file and compile.
    Does it fail to compile? Don't post it.
    Does it work as it should? Don't post it.

    Does it compile and still crash? Then copy & paste the code in your source file and post it here.
    Then copy & paste the exact same code you compiled here. You will receive no help until you do so.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-19-2010, 09:25 PM
  2. Access Violation
    By beene in forum Game Programming
    Replies: 8
    Last Post: 03-27-2007, 01:39 PM
  3. Access Violation under VC++ 6.0
    By xephyr in forum C Programming
    Replies: 4
    Last Post: 07-30-2004, 12:06 AM
  4. Access violation...
    By major_small in forum C++ Programming
    Replies: 5
    Last Post: 12-18-2003, 12:53 AM
  5. Access Violation!!
    By Yoshi in forum C++ Programming
    Replies: 4
    Last Post: 09-11-2002, 01:22 PM

Tags for this Thread