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"

Using FCanvas

I'm using an FCanvas in my game to draw some basic shapes into a texture and I'm just wondering what the proper way to use the struct is. All the examples I've seen on the Answer Hub and forums use new and allocate the memory manually that way since it seems you can't have one in a UClass/UStruct because it doesn't have a default constructor. If you plan on keeping an FCanvas around and updating it each tick is this the intended way of doing it? I was under the impression that you should never use new yourself in UE4 but that seems to be the only way to do this. Or are you not supposed to keep one between ticks and just recreate it every time you need it?

What I'm doing isn't complicated enough for me to worry about memory leaks but I'd like to use UE4's memory management instead if possible.

Product Version: UE 4.10
Tags:
more ▼

asked Feb 24 '16 at 07:33 PM in C++ Programming

avatar image

kgamble
919 40 58 204

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

1 answer: sort voted first

You can use new, in fact you need to in some cases, if you extending editor and for example want to add your own asset time, the Asset type info is keeped in non-UObject class that need to be manually allocated. Just you can't use it on UObjects which is tied with UE4 memory management.You also need to remember that if you use it you are reponcible to manage it if you allocate and lose pointer without deleting the object first then you got memory leak.

I never used it in FCanvas outside of AHUD class so not sure if this will work, but if you don't want to deal with memory allocation you could always try delere it staticly:

FCanvas Canvas;

If you do that FCanvas (or any other struct or varable that is not a pointer) becames integral part of class memory structure and it allocated and deallocated together with it, this is safest way to go. Just remember you can't do that with UObjects

more ▼

answered Feb 25 '16 at 01:26 AM

avatar image

Shadowriver
35.9k 928 169 1105

avatar image kgamble Feb 25 '16 at 10:11 PM

I did try to allocate it statically, but FCanvas doesn't have a default constructor and I can't use just default values for it in my object initializer list and reconstruct it once I have the correct parameters since its constructor makes sure I'm not passing in a null pointer.

I did end up just newing it but thanks for the explanation.

(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