How can I prevent the new operator from being globally overriden?

I’m try to implement a ThirdParty library which is all working fine so far. However, I’m at the point where I have to tie the library to Unreal objects, for example UTexture2D.

For that I have to #include "Engine/Texture2D.h", this in returns also includes ObjectBase.h, which has an overrider defined for the new operator.

This means, that after I include this class, I lose the possibility to instantiate normal Unreal-unrelated classes coming from the ThirdParty library.

How can I bypass this and still use UTexture2D?

Example:

TBBitmapUE4 *bitmap = new TBBitmapUE4(this);

http://puu.sh/8RSox.png

new in this case complains its missing arguments, whereas TBBitmapUE4 extends from a normal C++ class defined somewhere in the library I have no control over. That is, untill I remove the Texture2D header, which I need.

Can you use Malloc instead?

TBBitmapUE4 *bitmap = (TBBitmapUE4*) malloc(sizeof(TBBitmapUE4));

...

free(bitmap);

Rama

Hi

Is your class TBBitmapUE4 derived from UTexture2D? It sounds like it may be using UObject-based functionality.

If you are doing that then your class needs to be constructed using ConstructObject() rather than operator new.

This is quite a dangerous pattern to follow, the object would not be properly constructed & destroyed in this case, so you would need to directly call the constructor & destructor.

Apologies - I think the forum auto-accepts my answers as I am marked as ‘staff’.

ObjectBase.h does not declare a global operator new - it declares a DECLARE_CLASS macro that provides a operator new to UObject-derived classes. This macro is used in the *.generated.h files that UHT creates for all UObject-derived classes.

How does your class use UTexture2D? Does it store a raw or weak pointer to the texture?

Can you paste the output from your compiler please? I’d like to get more information that just the VS-snippet error. It may be that the actual error is elsewhere.

Do you actually get a compiler error, or is it just intellisense?

Whoever is asking accepting answers, please don’t do that. I know a lot of people don’t ever come back to their posts after they had an answer, but I do.

That said: TBBitmapUE4 xtends from a class inside the library, which has no relation to UE4 at all. TBBitmapUE4 uses a UTexture2D variable, hence I need the include.

I guess it’s a raw pointer:

private:
    UTexture2D *texture;

After a lot of digging, I figured out the mistake: I used the “Add new item” option of Visual Studio, but this places the source files at the Intermediate/ProjectFiles, rather than Source/[ModuleName]. This causes all kinds of weird issues.

The fix is to move the files to the right directory.

It would be cool if this behaviour can be fixed, as it’s easy to miss! As a work-around, create files directly in the source directory, then include them using Visual Studio’s “Show All Files” button.