x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

operator delete already defined

Hello,

I am using my own library within Unreal 4 and I seem to have a conflict issue with the delete operator.

 20>libMyFramework.lib(MemBase.obj) : error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPEAX@Z) already defined in MyPlugin.cpp.obj
 20>libMyFramework.lib(MemBase.obj) : error LNK2005: "void __cdecl operator delete[](void *)" (??_V@YAXPEAX@Z) already defined in MyPlugin.cpp.obj

the linker error only appears if I use one of my own dynamic arrays from my library - which is a templates class (all inlined). That class internally uses new and delete which are defined with their own allocator.

I have properly guarded my #include of my framework like this

 #ifdef UE_GAME
 #include "AllowWindowsPlatformTypes.h"
 #endif
 
 #include "MyFramework.h"
 
 #ifdef UE_GAME
 #include "HideWindowsPlatformTypes.h"
 #endif

 

has anyone some other suggestion what I could do to prevent this redefinition from happening ?

Product Version: Not Selected
Tags:
more ▼

asked Jul 30 '14 at 08:04 PM in C++ Programming

avatar image

Chrys
301 21 27 44

avatar image Steve Robb STAFF Jul 31 '14 at 01:17 PM

Hi,

UE4 overrides the operator new/delete operators to its own implementation, and your library seems to be being built against the standard library implementation, hence the clash.

My suggestion would be to convert your library to a UE4 module which will then automatically start to use those operator overloads, which are overridden in UE4's IMPLEMENT_MODULE macro.

Hope this helps,

Steve

avatar image Chrys Jul 31 '14 at 01:40 PM

Hi Steve , thanks for your interest.

I cannot really convert my Library to a UE4 Module as it is used on my not unreal related applications on server side.

I am not at home right now so I can't check out the IMPLEMENT_MODULE macro. Is there a way to let my Library use its own allocators ? (as stated above the new and delete are overriden Inside my Library too - so its not the standard allocators).

is there maybe some magic #define I can use to wrap my headers around to "protect" them from clashing with UE4 ?

chrys

avatar image old xia Jun 21 '18 at 07:23 AM

I don't agree that library should be converted into ue4 module, since I found in default case, .lib does not have implement new and delete symbols, and they are 'external'.

I resovled my similar issue by making sure all obj linking into exe file, have only one "new and delete" implemention.(like static symbols in .obj)

avatar image Steve Robb STAFF Jul 31 '14 at 02:10 PM

UE4's operator new and delete are defined like this, but this won't really help you:

 #define REPLACEMENT_OPERATOR_NEW_AND_DELETE \
     OPERATOR_NEW_MSVC_PRAGMA void* operator new  ( size_t Size ) OPERATOR_NEW_THROW_SPEC    { return FMemory::Malloc( Size ); } \
     OPERATOR_NEW_MSVC_PRAGMA void* operator new[]( size_t Size ) OPERATOR_NEW_THROW_SPEC    { return FMemory::Malloc( Size ); } \
     void operator delete  ( void* Ptr )                          OPERATOR_DELETE_THROW_SPEC { FMemory::Free( Ptr ); } \
     void operator delete[]( void* Ptr )                          OPERATOR_DELETE_THROW_SPEC { FMemory::Free( Ptr ); }

Sorry, I didn't notice that you said your library overrides operator new and delete too. That makes sense then. You simply need to not override them and the linker should use the UE4 overrides when you statically link your library.

There is no way to have two different operator new/deletes statically linked into the same program at the same time, just as the same as it's not possible for any other function, so there's no 'magic' available. This is not just a UE4 thing - it's a C++ thing.

If you were adamant about keeping your own overrides then you could rebuild your library as a DLL and dynamically link it instead of statically linking it, as each DLL has its own operator new/delete.

However, this would tie your library to a Windows implementation - I don't know if this is a problem for you. You'd also have to be sure that all of your new/deletes happened within your module and not inside UE4, as mismatched new/deletes would cause disaster. This can happen easily with templates (like container classes), as your module and the engine would each have their own instantiation of the container code which would call its own new/delete.

Anyway, some thoughts for you to ponder on. :-)

Steve

avatar image Chrys Jul 31 '14 at 02:27 PM

what you say makes sense I think. My Library is Windows only yes so in theory DLL would be an option but I think I could also modify my build pipeline to spit out some UE4 specific Library that does'nt make use of my overrides.

if I do that, is there some caveats i need to think of before doing it ? I.e. are those new and delete operators meant to be used for UObjects or something like that ?

also I Wonder if the UE4 overrides were on a

 #ifndef UE4_OPERATORS
 #define UE4_OPERATORS
 ///blablabla
 
 #endif

and in my Library i had a

 #ifdef UE4_OPERATORS
 #undef UE4_OPERATORS
 
 //my blablabla
 
 #define UE4_OPERATORS
 
 
 or something like that

hm maybe i will just make my Library into a DLL and take the hit of having to declspecc everything :-/

avatar image Steve Robb STAFF Jul 31 '14 at 02:48 PM

Those overrides are used for every new/delete in UE4, not just UObjects.

I would go with a define like DISABLE_OPERATOR_OVERRIDES and then use that to build a variant of your library which can link against UE4... but it's not a UE4-specific problem so doesn't make sense to name the macro after UE.

Libraries which override operator new/delete is a bit 'rude' anyway, because it forces everything else being (statically) linked to use your operators. :-)

Steve

avatar image Bob_Gneu Mar 12 '15 at 12:58 AM

As a quick side note, you will get a very similar error reported if you put your PCH include after anything else.

 >MyModule.cpp.obj : error LNK2005: "void * __cdecl operator new(unsigned __int64)" (??2@YAPEAX_K@Z) already defined in MyModule.h.obj
 >MyModule.cpp.obj : error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPEAX@Z) already defined in MyModule.h.obj

The compiler appears to do some initialization including finding new and delete, which rolls over until it is overwritten by UE4. It is remedied by simply including your PCH first.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

you are absolutly right. I will do the adjustments in my Library and put them on a macro so i can link the version for UE without them.

Thanks for your help, I will mark this as resolved.

more ▼

answered Jul 31 '14 at 04:53 PM

avatar image

Chrys
301 21 27 44

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

If somebody has linker errors related to new and delete already being declared, it's sometimes due to adding ";" to the end of Macro lines, like declaration of Log Categories

DECLARE_LOG_CATEGORY_EXTERN(EditorLog, Verbose, All); <-- REMOVE THE ;

This took a long while to discover, but it was it. Just the habit... be careful!

more ▼

answered Feb 16 '18 at 10:38 AM

avatar image

darkgaze
189 9 13 18

avatar image old xia Jun 21 '18 at 07:23 AM

this is not correct.

avatar image amelville1 Jan 30 '19 at 04:30 AM

I encountered the same issue, and tried your solution wondering whether you removing the ';' caused something else to happen, which fixed the error. In doing this my error was resolved, and I believe that this occurred due to me editing the module's header file, where my log declarations are located. In summary if you edit your module's header file and then build, the issue may be resolved.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question