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. virtual function crash

virtual function crash

Scheduled Pinned Locked Moved C / C++ / MFC
9 Posts 6 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.
  • S Offline
    S Offline
    Steve Severance
    wrote on last edited by
    #1

    I have an abstract class like:

    class CMyAbstract
    {
    public:
    virtual void Func(char *Param) = 0;
    };

    Then I have a class that uses it as its base class.

    class CMyCLass : public CMyAbstract
    {
    public:
    virtual void Func(char *Param);
    };

    In an implementation file

    CMyAbstract *pClass = new CMyClass;
    pClass->Func("yada");

    This crashes as long as its a virtual function. Why is this. Don't worry about omitted thing like constructors. Steve Not all who wander are lost...

    T N N L 4 Replies Last reply
    0
    • S Steve Severance

      I have an abstract class like:

      class CMyAbstract
      {
      public:
      virtual void Func(char *Param) = 0;
      };

      Then I have a class that uses it as its base class.

      class CMyCLass : public CMyAbstract
      {
      public:
      virtual void Func(char *Param);
      };

      In an implementation file

      CMyAbstract *pClass = new CMyClass;
      pClass->Func("yada");

      This crashes as long as its a virtual function. Why is this. Don't worry about omitted thing like constructors. Steve Not all who wander are lost...

      T Offline
      T Offline
      Tim Smith
      wrote on last edited by
      #2

      Does it crash in the constructor or destructor? Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

      S 1 Reply Last reply
      0
      • S Steve Severance

        I have an abstract class like:

        class CMyAbstract
        {
        public:
        virtual void Func(char *Param) = 0;
        };

        Then I have a class that uses it as its base class.

        class CMyCLass : public CMyAbstract
        {
        public:
        virtual void Func(char *Param);
        };

        In an implementation file

        CMyAbstract *pClass = new CMyClass;
        pClass->Func("yada");

        This crashes as long as its a virtual function. Why is this. Don't worry about omitted thing like constructors. Steve Not all who wander are lost...

        N Offline
        N Offline
        Nish Nishant
        wrote on last edited by
        #3

        Hope you have implemented the function? Else being a virtual function it'll try and call the base function which points to NULL. Nish


        Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.

        S 1 Reply Last reply
        0
        • T Tim Smith

          Does it crash in the constructor or destructor? Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

          S Offline
          S Offline
          Steve Severance
          wrote on last edited by
          #4

          No it does not. Steve Not all who wander are lost...

          1 Reply Last reply
          0
          • N Nish Nishant

            Hope you have implemented the function? Else being a virtual function it'll try and call the base function which points to NULL. Nish


            Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.

            S Offline
            S Offline
            Steve Severance
            wrote on last edited by
            #5

            yes the function is implemented. Steve Not all who wander are lost...

            1 Reply Last reply
            0
            • S Steve Severance

              I have an abstract class like:

              class CMyAbstract
              {
              public:
              virtual void Func(char *Param) = 0;
              };

              Then I have a class that uses it as its base class.

              class CMyCLass : public CMyAbstract
              {
              public:
              virtual void Func(char *Param);
              };

              In an implementation file

              CMyAbstract *pClass = new CMyClass;
              pClass->Func("yada");

              This crashes as long as its a virtual function. Why is this. Don't worry about omitted thing like constructors. Steve Not all who wander are lost...

              N Offline
              N Offline
              Neville Franks
              wrote on last edited by
              #6

              Steve Severance wrote: CMyAbstract *pClass = new CMyClass; Shouldn't it be: CMyClass* pClass = new CMyClass; Neville Franks, Author of ED for Windows. www.getsoft.com

              1 Reply Last reply
              0
              • S Steve Severance

                I have an abstract class like:

                class CMyAbstract
                {
                public:
                virtual void Func(char *Param) = 0;
                };

                Then I have a class that uses it as its base class.

                class CMyCLass : public CMyAbstract
                {
                public:
                virtual void Func(char *Param);
                };

                In an implementation file

                CMyAbstract *pClass = new CMyClass;
                pClass->Func("yada");

                This crashes as long as its a virtual function. Why is this. Don't worry about omitted thing like constructors. Steve Not all who wander are lost...

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Did you Copy/Paste the example code from your project? If so, check the class names: In the class definition CMyCLass is written with a uppercase L, the implementation fragment has CMyClass with a lowercase l.

                S 1 Reply Last reply
                0
                • L Lost User

                  Did you Copy/Paste the example code from your project? If so, check the class names: In the class definition CMyCLass is written with a uppercase L, the implementation fragment has CMyClass with a lowercase l.

                  S Offline
                  S Offline
                  Steve Severance
                  wrote on last edited by
                  #8

                  The error is runtime(Access Violation) not compile time. Here is actual source. Header

                  class CInputCommandMap
                  {
                  public:
                  CInputCommandMap();
                  ~CInputCommandMap();

                  void LoadCommandMap(char \*file);
                  virtual void ProcessInput(char \*keys, DIMOUSESTATE2 \*mouse) = 0;
                  
                  CInputCommand m\_keyA;
                  CInputCommand m\_keyB;
                  CInputCommand m\_keyC;
                      More Keys...
                  

                  };

                  class CDefaultKeyMap : public CInputCommandMap
                  {
                  public:
                  CDefaultKeyMap(){}
                  ~CDefaultKeyMap(){}

                  void ProcessInput(char \*keys, DIMOUSESTATE2 \*mouse);
                  

                  };

                  ProcessInput is overridden and contains the actual implentation. LoadCommandMap is

                  void LoadCommandMap(CInputCommandMap *map){m_pCommandMap = map;}

                  I use it like this.

                  pDefaultCommandMap = new CDefaultKeyMap;
                  pDefaultCommandMap->LoadCommandMap("default.key");
                  pInput->LoadCommandMap(pDefaultCommandMap);
                  
                      Code...In an update function that is in the main input class
                  
                  if(m\_pCommandMap)
                  	m\_pCommandMap->ProcessInput((char \*)m\_Keys,&m\_MouseState);
                  

                  Thanks once again for all you guys help. Steve Not all who wander are lost...

                  M 1 Reply Last reply
                  0
                  • S Steve Severance

                    The error is runtime(Access Violation) not compile time. Here is actual source. Header

                    class CInputCommandMap
                    {
                    public:
                    CInputCommandMap();
                    ~CInputCommandMap();

                    void LoadCommandMap(char \*file);
                    virtual void ProcessInput(char \*keys, DIMOUSESTATE2 \*mouse) = 0;
                    
                    CInputCommand m\_keyA;
                    CInputCommand m\_keyB;
                    CInputCommand m\_keyC;
                        More Keys...
                    

                    };

                    class CDefaultKeyMap : public CInputCommandMap
                    {
                    public:
                    CDefaultKeyMap(){}
                    ~CDefaultKeyMap(){}

                    void ProcessInput(char \*keys, DIMOUSESTATE2 \*mouse);
                    

                    };

                    ProcessInput is overridden and contains the actual implentation. LoadCommandMap is

                    void LoadCommandMap(CInputCommandMap *map){m_pCommandMap = map;}

                    I use it like this.

                    pDefaultCommandMap = new CDefaultKeyMap;
                    pDefaultCommandMap->LoadCommandMap("default.key");
                    pInput->LoadCommandMap(pDefaultCommandMap);
                    
                        Code...In an update function that is in the main input class
                    
                    if(m\_pCommandMap)
                    	m\_pCommandMap->ProcessInput((char \*)m\_Keys,&m\_MouseState);
                    

                    Thanks once again for all you guys help. Steve Not all who wander are lost...

                    M Offline
                    M Offline
                    Mike Nordell
                    wrote on last edited by
                    #9

                    You've got the right idea, but you've got bugs in both your interfaces in your imnplementations. The implementation bug is what crashes it. Sorry, can't help without _full_ definition. The interface bug is that a class that is intended to be inherited from, especially if it contains data (!), should (almost) always have a virtual destructor. To give you a hint: Your code is probably overwriting some memory it doesn't own.

                    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