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. Failed new operator

Failed new operator

Scheduled Pinned Locked Moved C / C++ / MFC
comhelpquestion
18 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.
  • J Offline
    J Offline
    Joe Smith IX
    wrote on last edited by
    #1

    Hi all, I am trying to use this class http://www.codeproject.com/cs/files/CABCompressExtract.asp?df=100&forumid=336576&fr=26[^] in my dialog class. But as soon as I declare the object, it failed to compiled with the following error:

    c:\testcompress\compresst.hpp(466) : error C2661: 'new' : no overloaded function takes 2 parameters
    c:\testcompress\compresst.hpp(465) : while compiling class-template member function 'void *__cdecl Cabinet::CCompressT::Alloc(unsigned int)'
    Error executing cl.exe.

    Here is my function call:

    void CTestCompressDlg::OnButton1()
    {
    // I copied these lines from the demo source code
    UINT u32_CabID = 12345;
    UINT u32_SplitSize = 0x7FFFFFFF;

    char s8_CompressFile[MAX_PATH];

    Cabinet::CCompress i_Compress;
    i_Compress.CreateFCIContext(s8_CompressFile, u32_SplitSize, u32_CabID);

    }

    And here is the snippet on the failing line:

    namespace Cabinet
    {

    template class CCompressT
    {
    public:
    ...

    protected:
    static void* Alloc(UINT size)
    {
    return operator new(size); // <--- failed here
    }

    ...
    }
    }

    Anyone has any idea why, please? Thanks a lot in advance.

    M N 2 Replies Last reply
    0
    • J Joe Smith IX

      Hi all, I am trying to use this class http://www.codeproject.com/cs/files/CABCompressExtract.asp?df=100&forumid=336576&fr=26[^] in my dialog class. But as soon as I declare the object, it failed to compiled with the following error:

      c:\testcompress\compresst.hpp(466) : error C2661: 'new' : no overloaded function takes 2 parameters
      c:\testcompress\compresst.hpp(465) : while compiling class-template member function 'void *__cdecl Cabinet::CCompressT::Alloc(unsigned int)'
      Error executing cl.exe.

      Here is my function call:

      void CTestCompressDlg::OnButton1()
      {
      // I copied these lines from the demo source code
      UINT u32_CabID = 12345;
      UINT u32_SplitSize = 0x7FFFFFFF;

      char s8_CompressFile[MAX_PATH];

      Cabinet::CCompress i_Compress;
      i_Compress.CreateFCIContext(s8_CompressFile, u32_SplitSize, u32_CabID);

      }

      And here is the snippet on the failing line:

      namespace Cabinet
      {

      template class CCompressT
      {
      public:
      ...

      protected:
      static void* Alloc(UINT size)
      {
      return operator new(size); // <--- failed here
      }

      ...
      }
      }

      Anyone has any idea why, please? Thanks a lot in advance.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Maybe try return new BYTE(size); or return new unsigned char(size); Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      J 1 Reply Last reply
      0
      • M Mark Salsbery

        Maybe try return new BYTE(size); or return new unsigned char(size); Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        J Offline
        J Offline
        Joe Smith IX
        wrote on last edited by
        #3

        They didn't work either. Btw, the demo compiles fine. How could it be?

        M 1 Reply Last reply
        0
        • J Joe Smith IX

          They didn't work either. Btw, the demo compiles fine. How could it be?

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Same compiler error?  It works for me on VS 2005. Do you need to include new.h?

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          J 1 Reply Last reply
          0
          • J Joe Smith IX

            Hi all, I am trying to use this class http://www.codeproject.com/cs/files/CABCompressExtract.asp?df=100&forumid=336576&fr=26[^] in my dialog class. But as soon as I declare the object, it failed to compiled with the following error:

            c:\testcompress\compresst.hpp(466) : error C2661: 'new' : no overloaded function takes 2 parameters
            c:\testcompress\compresst.hpp(465) : while compiling class-template member function 'void *__cdecl Cabinet::CCompressT::Alloc(unsigned int)'
            Error executing cl.exe.

            Here is my function call:

            void CTestCompressDlg::OnButton1()
            {
            // I copied these lines from the demo source code
            UINT u32_CabID = 12345;
            UINT u32_SplitSize = 0x7FFFFFFF;

            char s8_CompressFile[MAX_PATH];

            Cabinet::CCompress i_Compress;
            i_Compress.CreateFCIContext(s8_CompressFile, u32_SplitSize, u32_CabID);

            }

            And here is the snippet on the failing line:

            namespace Cabinet
            {

            template class CCompressT
            {
            public:
            ...

            protected:
            static void* Alloc(UINT size)
            {
            return operator new(size); // <--- failed here
            }

            ...
            }
            }

            Anyone has any idea why, please? Thanks a lot in advance.

            N Offline
            N Offline
            Nathan Holt at EMOM
            wrote on last edited by
            #5

            I recall that Visual C++ generates code with a debug feature in which new is #Defined as something else in debug builds. Does it compile in release mode? Nathan

            J M 2 Replies Last reply
            0
            • M Mark Salsbery

              Same compiler error?  It works for me on VS 2005. Do you need to include new.h?

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              J Offline
              J Offline
              Joe Smith IX
              wrote on last edited by
              #6

              Hi Mark, Just to be clear, this is what I did: 1. Create a dialog based app 2. #include "ExtractResourceT.hpp" #include "CompressT.hpp" 3. Create a button with OnButton1() function as above 4. Compile Are you telling me that those steps works in VS2005? (Mine is VC++ 6). PS. "include new.h" generated even more errors relating to new.h

              1 Reply Last reply
              0
              • N Nathan Holt at EMOM

                I recall that Visual C++ generates code with a debug feature in which new is #Defined as something else in debug builds. Does it compile in release mode? Nathan

                J Offline
                J Offline
                Joe Smith IX
                wrote on last edited by
                #7

                You are right. It compiles fine in release mode. This is new and weird to me... How am I supposed to debug my app then?

                N 1 Reply Last reply
                0
                • J Joe Smith IX

                  You are right. It compiles fine in release mode. This is new and weird to me... How am I supposed to debug my app then?

                  N Offline
                  N Offline
                  Nathan Holt at EMOM
                  wrote on last edited by
                  #8

                  Joe Smith IX wrote:

                  You are right. It compiles fine in release mode. This is new and weird to me... How am I supposed to debug my app then?

                  In the implementation file you are using, you could look for the #define new statement and remove it. If you want to keep the ability to detect memory leaks from that code, you can change it to #define TNEW, add a #else clause with #define TNEW new, and then replace all standard calls to new with TNEW. Nathan

                  J 1 Reply Last reply
                  0
                  • N Nathan Holt at EMOM

                    I recall that Visual C++ generates code with a debug feature in which new is #Defined as something else in debug builds. Does it compile in release mode? Nathan

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #9

                    :doh: I didn't think of that. Good eye Nathan!! :) Cheers!

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    1 Reply Last reply
                    0
                    • N Nathan Holt at EMOM

                      Joe Smith IX wrote:

                      You are right. It compiles fine in release mode. This is new and weird to me... How am I supposed to debug my app then?

                      In the implementation file you are using, you could look for the #define new statement and remove it. If you want to keep the ability to detect memory leaks from that code, you can change it to #define TNEW, add a #else clause with #define TNEW new, and then replace all standard calls to new with TNEW. Nathan

                      J Offline
                      J Offline
                      Joe Smith IX
                      wrote on last edited by
                      #10

                      But when I search for it, the only two instances of 'define new' are the standard one below in my dialog and app .cpp files. Could it be defined some other way? Or any other workaround to make it compilable in debug mode? Thanks.

                      #ifdef _DEBUG
                      #define new DEBUG_NEW
                      #undef THIS_FILE
                      static char THIS_FILE[] = __FILE__;
                      #endif

                      M 1 Reply Last reply
                      0
                      • J Joe Smith IX

                        But when I search for it, the only two instances of 'define new' are the standard one below in my dialog and app .cpp files. Could it be defined some other way? Or any other workaround to make it compilable in debug mode? Thanks.

                        #ifdef _DEBUG
                        #define new DEBUG_NEW
                        #undef THIS_FILE
                        static char THIS_FILE[] = __FILE__;
                        #endif

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #11

                        Do you really need to call the operator directly? Can you just use the vector form of new and allocate an array of bytes? return new BYTE[size]; Mark

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        J 1 Reply Last reply
                        0
                        • M Mark Salsbery

                          Do you really need to call the operator directly? Can you just use the vector form of new and allocate an array of bytes? return new BYTE[size]; Mark

                          Mark Salsbery Microsoft MVP - Visual C++ :java:

                          J Offline
                          J Offline
                          Joe Smith IX
                          wrote on last edited by
                          #12

                          Well, the thing is that the call is made from the class I downloaded from CP (see my first post above). If I change the object, then I have to modify the class as well, right? What makes me wonder is that how come I can run the demo code in debug mode and step thru line-by-line, while in my test project I can't even compile the debug mode? They both using the same classes! Any ideas? thanks for any suggestion.

                          M 1 Reply Last reply
                          0
                          • J Joe Smith IX

                            Well, the thing is that the call is made from the class I downloaded from CP (see my first post above). If I change the object, then I have to modify the class as well, right? What makes me wonder is that how come I can run the demo code in debug mode and step thru line-by-line, while in my test project I can't even compile the debug mode? They both using the same classes! Any ideas? thanks for any suggestion.

                            M Offline
                            M Offline
                            Mark Salsbery
                            wrote on last edited by
                            #13

                            If the problem is with using the DEBUG_NEW operator in debug builds, then you can wrap your calls to operator new with something like #pragma push_macro("new") #undef new ...   (use the new operator here) ... #pragma pop_macro("new") The problem is, where do you put this in a template class?  Do you put it in the class declaration or do you have to remember to do it every place you instantiate an object of the class? I haven't seen the rest of the class in question, but I assume if it calls operator new, then it must call operator delete somewhere.  Those should be the only calls you'd need to change. I have no idea why the author did that - is the operator new overloaded in the class? Mark

                            Mark Salsbery Microsoft MVP - Visual C++ :java:

                            N J 2 Replies Last reply
                            0
                            • M Mark Salsbery

                              If the problem is with using the DEBUG_NEW operator in debug builds, then you can wrap your calls to operator new with something like #pragma push_macro("new") #undef new ...   (use the new operator here) ... #pragma pop_macro("new") The problem is, where do you put this in a template class?  Do you put it in the class declaration or do you have to remember to do it every place you instantiate an object of the class? I haven't seen the rest of the class in question, but I assume if it calls operator new, then it must call operator delete somewhere.  Those should be the only calls you'd need to change. I have no idea why the author did that - is the operator new overloaded in the class? Mark

                              Mark Salsbery Microsoft MVP - Visual C++ :java:

                              N Offline
                              N Offline
                              Nathan Holt at EMOM
                              wrote on last edited by
                              #14

                              Mark Salsbery wrote:

                              #pragma push_macro("new") #undef new ... (use the new operator here) ... #pragma pop_macro("new")

                              That's cool. I didn't know about the #pragma push_macro. Thanks for mentioning it. Nathan

                              1 Reply Last reply
                              0
                              • M Mark Salsbery

                                If the problem is with using the DEBUG_NEW operator in debug builds, then you can wrap your calls to operator new with something like #pragma push_macro("new") #undef new ...   (use the new operator here) ... #pragma pop_macro("new") The problem is, where do you put this in a template class?  Do you put it in the class declaration or do you have to remember to do it every place you instantiate an object of the class? I haven't seen the rest of the class in question, but I assume if it calls operator new, then it must call operator delete somewhere.  Those should be the only calls you'd need to change. I have no idea why the author did that - is the operator new overloaded in the class? Mark

                                Mark Salsbery Microsoft MVP - Visual C++ :java:

                                J Offline
                                J Offline
                                Joe Smith IX
                                wrote on last edited by
                                #15

                                You are right about the conflict is with the definition of DEBUG_NEW operator in debug builds. As soon as I commented all instances, it compiled fine.

                                /*
                                #ifdef _DEBUG
                                #define new DEBUG_NEW
                                #undef THIS_FILE
                                static char THIS_FILE[] = __FILE__;
                                #endif
                                */

                                I can't find any code that overload new operator in the class, though... Using the push_macro and pop_macro resulted this error:

                                #pragma push_macro : 'new' is not currently defined as a macro

                                Anyway, what's the effect I will get by commenting out the definition of DEBUG_NEW? Thanks.

                                M 1 Reply Last reply
                                0
                                • J Joe Smith IX

                                  You are right about the conflict is with the definition of DEBUG_NEW operator in debug builds. As soon as I commented all instances, it compiled fine.

                                  /*
                                  #ifdef _DEBUG
                                  #define new DEBUG_NEW
                                  #undef THIS_FILE
                                  static char THIS_FILE[] = __FILE__;
                                  #endif
                                  */

                                  I can't find any code that overload new operator in the class, though... Using the push_macro and pop_macro resulted this error:

                                  #pragma push_macro : 'new' is not currently defined as a macro

                                  Anyway, what's the effect I will get by commenting out the definition of DEBUG_NEW? Thanks.

                                  M Offline
                                  M Offline
                                  Mark Salsbery
                                  wrote on last edited by
                                  #16

                                  Joe Smith IX wrote:

                                  what's the effect I will get by commenting out the definition of DEBUG_NEW?

                                  You just lose the file/linenumber info in debug builds that aid in finding memory leaks. I'm still not sure why the author used new like that... Mark

                                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                                  J 1 Reply Last reply
                                  0
                                  • M Mark Salsbery

                                    Joe Smith IX wrote:

                                    what's the effect I will get by commenting out the definition of DEBUG_NEW?

                                    You just lose the file/linenumber info in debug builds that aid in finding memory leaks. I'm still not sure why the author used new like that... Mark

                                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                                    J Offline
                                    J Offline
                                    Joe Smith IX
                                    wrote on last edited by
                                    #17

                                    In that case, is there any way to undef the DEBUG_NEW just before the conflicting code, and then re-define it right afterward? Something like below: (except this one generated one warning)

                                    static void* Alloc(UINT size)
                                    {
                                    #ifdef _DEBUG
                                    #undef new DEBUG_NEW // <-- compile warning C4067: unexpected tokens following preprocessor directive - expected a newline
                                    #endif

                                    return operator new(size);

                                    #ifdef _DEBUG
                                    #define new DEBUG_NEW
                                    #undef THIS_FILE
                                    static char THIS_FILE[] = __FILE__;
                                    #endif
                                    }

                                    Thanks.

                                    M 1 Reply Last reply
                                    0
                                    • J Joe Smith IX

                                      In that case, is there any way to undef the DEBUG_NEW just before the conflicting code, and then re-define it right afterward? Something like below: (except this one generated one warning)

                                      static void* Alloc(UINT size)
                                      {
                                      #ifdef _DEBUG
                                      #undef new DEBUG_NEW // <-- compile warning C4067: unexpected tokens following preprocessor directive - expected a newline
                                      #endif

                                      return operator new(size);

                                      #ifdef _DEBUG
                                      #define new DEBUG_NEW
                                      #undef THIS_FILE
                                      static char THIS_FILE[] = __FILE__;
                                      #endif
                                      }

                                      Thanks.

                                      M Offline
                                      M Offline
                                      Mark Salsbery
                                      wrote on last edited by
                                      #18

                                      Sorry for the late reply - did you get this working? The line with the warning should be just

                                      #undef new

                                      As I mentioned before, I'm just not sure where this should be in a template class. Since the actual class is built where an object is instantiated, I'm not sure if the #undef stuff needs to be there or if it worke by putting it in the template declaration. If it works in the template declaration (as you've shown) then that's cool :) It's easy enough to test. Mark

                                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                                      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