Implementation:CARLA simulator Carla LifetimeProfiled
| Knowledge Sources | |
|---|---|
| Domains | Profiling, Debugging |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
The LifetimeProfiled class provides compile-time-configurable object lifetime tracking that counts live instances of any class that inherits from it.
Description
carla::profiler::LifetimeProfiled works differently depending on the LIBCARLA_ENABLE_LIFETIME_PROFILER compile flag:
When enabled: The class maintains a std::unordered_map<std::string, std::pair<size_t, size_t>> mapping type names to (count_created, count_alive) pairs. The constructor increments both counters and the destructor decrements the alive counter. A static header instance outputs all tracked lifetime data to the log on program shutdown.
The Header nested class destructs at process exit and iterates over all registered types, logging the count created and count alive for each, providing leak detection and object lifetime analysis.
When disabled: The class becomes an empty base class with no overhead (zero-cost abstraction).
The type name is demangled from typeid(*this).name() using abi::__cxa_demangle on GCC/Clang or used directly on MSVC.
Usage
Inherit from this class to automatically track object lifetimes during development and debugging. Useful for detecting memory leaks or unexpected object accumulation.
Code Reference
Source Location
- Repository: CARLA
- File:
LibCarla/source/carla/profiler/LifetimeProfiled.cpp
Signature
class LifetimeProfiled {
public:
LifetimeProfiled();
~LifetimeProfiled();
private:
struct Header {
~Header(); // Logs all lifetime data on shutdown
};
static Header header;
static std::unordered_map<std::string, std::pair<size_t, size_t>> _objects;
};
Import
#include "carla/profiler/LifetimeProfiled.h"
I/O Contract
| Input | Type | Description |
|---|---|---|
typeid | Type name is extracted from the derived class at construction
|
| Output | Type | Description |
|---|---|---|
log_info | On shutdown: type name, count created, count alive
|
Usage Examples
// Inherit to track lifetime
class MyActor : public LifetimeProfiled {
public:
MyActor() : LifetimeProfiled() { /* ... */ }
};
// At shutdown, log output shows:
// "MyActor created: 150, alive: 0"