xiaohongshu
xiaohongshu
custom-memory-heap-crash
Debugging crashes related to custom memory heap implementations, particularly release-only crashes involving static destruction order, use-after-free during program shutdown, and memory lifecycle issues between custom allocators and standard library internals. This skill should be used when debug...
Full skill instructions
This skill provides guidance for debugging crashes related to custom memory heap implementations, with particular focus on release-only crashes that occur during static destruction. These issues are notoriously difficult to diagnose because they involve subtle interactions between custom allocators, C++ standard library internals, and static initialization/destruction order.
Apply this skill when encountering:
main() returns)Identify crash timing: Determine if the crash occurs:
main() returns (static destruction phase)Gather crash information:
Compare debug vs release behavior:
Map static object lifetimes: Identify all static/global objects and their destruction order:
Trace allocation sources: For each allocation involved in the crash:
Identify ordering conflicts: Look for scenarios where:
Examine library internals: When crashes involve standard library:
_Facet_Register_impl, _Fac_tidy_reg_t)Understand optimization effects: Release builds may:
Verify assumptions: Add temporary instrumentation to confirm:
Trigger library allocations before custom heap is created, ensuring they use standard malloc:
void force_early_initialization() {
// Force locale/facet initialization
std::ostringstream oss;
oss << 42; // Triggers numeric facet registration
std::locale loc = std::locale();
// Force iostream initialization
std::cout.flush();
std::cerr.flush();
}
// Call this BEFORE creating custom heap
int main() {
force_early_initialization(); // Standard malloc used
create_custom_heap(); // Now safe to create heap
// ...
}
Keep custom heap alive until after all dependent static objects are destroyed:
// Use shared_ptr to extend lifetime
static std::shared_ptr<CustomHeap> g_heap;
// Or use atexit() for manual cleanup
void cleanup_heap() {
// Destroy heap last
}
int main() {
atexit(cleanup_heap);
// ...
}
Implement tracking to identify which allocations came from custom heap:
class CustomHeap {
std::unordered_set<void*> tracked_allocations;
public:
void* allocate(size_t size) {
void* ptr = internal_alloc(size);
tracked_allocations.insert(ptr);
return ptr;
}
bool owns(void* ptr) {
return tracked_allocations.count(ptr) > 0;
}
};
Ensure all allocations route through custom heap consistently:
void* operator new(size_t size) {
if (g_custom_heap && g_custom_heap->is_active()) {
return g_custom_heap->allocate(size);
}
return std::malloc(size);
}
void operator delete(void* ptr) noexcept {
if (g_custom_heap && g_custom_heap->owns(ptr)) {
g_custom_heap->deallocate(ptr);
} else {
std::free(ptr);
}
}
After implementing a fix, verify:
Simply calling std::cout.flush() may not trigger all necessary library initializations. Use operations that exercise the specific subsystems involved (locale formatting, facet registration, etc.).
Static destruction order can vary between:
Valgrind's "still reachable" memory often indicates intentionally leaked static data, but verify it's not masking the actual issue.
Debug builds may mask timing-dependent issues due to:
# Set breakpoint on static destructors
break __cxa_finalize
# Print static destruction order
info frame
# Watch memory access
watch *0xaddress
# Full memory check
valgrind --leak-check=full --track-origins=yes ./program
# Check with release build (may need debug symbols)
valgrind --leak-check=full ./program_release
# Release optimization with debug symbols
g++ -O2 -g -o program_release_debug source.cpp
# Address sanitizer (may change behavior)
g++ -O2 -fsanitize=address -o program_asan source.cpp
For detailed technical information about memory lifecycle analysis and library internals, see references/debugging_guide.md.
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience