Chapter 3. Memory Management and Smart Pointers

In this chapter, we are going to cover the following topics:

  • Unmanaged memory – using malloc()/free()
  • Unmanaged memory – using new/delete
  • Managed memory – using NewObject< > and ConstructObject< >
  • Managed memory – deallocating memory
  • Managed memory – smart pointers (TSharedPtr, TWeakPtr, TAutoPtr) to track an object
  • Using TScopedPointer to track an object
  • Unreal's garbage collection system and UPROPERTY()
  • Forcing garbage collection
  • Breakpoints and stepping through code
  • Finding bugs and using call stacks
  • Using the Profiler to identify hot spots

Introduction

Memory management is always one of the most important things to get right in your computer program to ensure stability and good, bug-free operation of your code. A dangling pointer (pointer referring to something that has been removed from memory) is an example of a bug that is hard to track if it occurs.

Introduction

In any computer program, memory management is extremely important. UE4's UObject reference counting system is the default way that memory is managed for Actors and UObject derivatives. This is the default way that your memory will be managed within your UE4 program.

If you write custom C++ classes of your own, which do not derive from UObject, you may find the TSharedPtr / TWeakPtr reference counted classes useful. These classes provide reference counting and automatic deletion for 0 reference objects.

This chapter provides recipes for memory management within UE4.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.127.68