This analysis identifies raw pointer usage patterns in the KAI codebase, focusing on the core library files in Include/KAI and Source/Library. The analysis reveals several areas where raw pointers are used and could benefit from conversion to smart pointers.
- Issue: Heavy use of raw pointers for memory management
- Critical Areas:
StorageBase *in instance managementTree *tree_member variable (line 59)const ClassBase *pointers in class registry- Multiple factory methods returning raw
Storage<T>*pointers
- Risk: Manual memory management with unclear ownership semantics
- Issue: Factory methods return raw pointers
- Critical Methods:
StorageBase *NewStorage()(line 39)Storage<T> *TypedStorage()(line 75)- Arithmetic operations returning
StorageBase *
- Risk: Potential memory leaks if callers don't properly manage returned pointers
- Issue: External library integration using raw pointers
- Critical Areas:
ENet::RakPeerInterface *peer_in multiple classesENet::Packet *used throughout for packet handlingDomain *domain_in NetPointer
- Risk: Dependency on external library's memory management
- Issue: Parser and lexer components use raw pointers
- Critical Areas:
const LexerBase *lexerin TokenBaseRegistry *reg_in ProcessCommon- Character pointer operations for string processing
- Risk: Potential null pointer dereferences and memory leaks
- Issue: Low-level memory management requires raw pointers
- Critical Areas:
- IAllocator interface methods
- Placement new operations
- Note: This is acceptable for low-level memory management
- Registry::tree_ - Raw pointer member with unclear lifetime
- StorageBase pointers in Registry containers
- Factory methods returning raw pointers without clear ownership transfer
- Network peer pointers - External resource management
- Parser/Lexer token management
- Method and Function factory functions using raw
new - Class system's type conversion operations
- C-string interfaces (
const char *) for compatibility - Memory allocator internals (by design)
- Temporary pointers in local scopes
- Convert
tree_tostd::unique_ptr<Tree> - Use
std::shared_ptr<StorageBase>for instance management - Return
std::unique_ptrfrom factory methods
- Change factory methods to return
std::unique_ptr<StorageBase> - Use
std::weak_ptrfor parent-child relationships - Implement RAII for all dynamically allocated objects
- Wrap ENet pointers in RAII wrappers
- Use
std::unique_ptrwith custom deleters for external resources - Implement safe packet handling with automatic cleanup
- Convert lexer/parser pointers to
std::shared_ptr - Use
std::string_viewinstead ofconst char*where possible - Implement proper ownership semantics for AST nodes
- Phase 1: Registry and StorageBase management (highest risk)
- Phase 2: Factory method return types
- Phase 3: Network layer RAII wrappers
- Phase 4: Language processing components
- The codebase already has smart pointer infrastructure (
SmartPointer.h) - Some raw pointer usage is intentional for C API compatibility
- Memory allocator system should remain using raw pointers by design
- Focus should be on areas with unclear ownership and manual
new/deletepairs