[English | æĨæŽčŠ]
ðŊ Quick Problem Resolution
This guide helps you quickly identify and resolve common issues with the Shared Memory Communication Manager.
ðĻ Common Issues & Solutions
1. ð Permission and Access Issues
"Permission denied" when accessing shared memory
# Error message
terminate called after throwing an instance of 'std::runtime_error'
what(): Failed to create shared memory: Permission denied
ð§ Solutions:
# Method 1: Fix shared memory permissions
sudo chmod 666 /dev/shm/*
# Method 2: Run with appropriate permissions
sudo ./your_program
# Method 3: Add user to shared memory group
sudo usermod -a -G shm $USER
newgrp shm
"Address already in use" error
# Error message
std::runtime_error: Shared memory segment already exists
ð§ Solutions:
# List shared memory segments
ls -la /dev/shm/
# Remove specific segments
sudo rm -f /dev/shm/shm_*
# Or clean all (be careful!)
sudo rm -f /dev/shm/*
2. ð Connection and Communication Issues
Publisher/Subscriber not communicating
# Symptoms
- Publisher sends data but Subscriber receives nothing
- subscribe() returns false consistently
- No error messages shown
ð§ Diagnostic Steps:
Publisher<int> pub("sensor_data");
Subscriber<int> sub("sensor_data");
Publisher<float> pub("data");
Subscriber<float> sub("data");
#include <sys/stat.h>
struct stat buffer;
if (stat("/dev/shm/shm_sensor_data", &buffer) == 0) {
std::cout << "Shared memory exists\n";
} else {
std::cout << "Shared memory not found\n";
}
ð§ Solutions:
bool success;
int data = sub.subscribe(&success);
if (!success) {
std::cout << "Failed to receive data - check publisher\n";
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
Service request/response failures
# Symptoms
- waitForResponse() always returns false
- Server never receives requests
- Response never arrives at client
ð§ Solutions:
ServiceServer<int, int> server("calc");
if (client.waitForResponse(5000000)) {
} else {
std::cout << "Timeout - check server status\n";
}
ServiceServer<int, float> server("calc");
ServiceClient<int, float> client("calc");
3. ð§ Memory and Performance Issues
Memory leaks detected
# Valgrind output
==12345== LEAK SUMMARY:
==12345== definitely lost: 1,024 bytes in 1 blocks
==12345== indirectly lost: 0 bytes in 0 blocks
ð§ Solutions:
{
Publisher<int> pub("topic");
pub.publish(42);
}
Publisher<int>* pub = new Publisher<int>("topic");
Publisher<int> pub("topic");
High CPU usage
# Symptoms
- CPU usage constantly high (>50%)
- System becomes unresponsive
- Excessive context switching
ð§ Solutions:
while (true) {
data, success = sub.subscribe();
if (success) {
process_data(data);
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
bool success;
do {
data = sub.subscribe(&success);
if (!success) {
std::this_thread::sleep_for(std::chrono::microseconds(100));
}
} while (!success);
4. ðĶ Compilation and Build Issues
Header not found
# Error message
fatal error: shm_pub_sub.hpp: No such file or directory
ð§ Solutions:
# 1. Check include paths
g++ -I./include -I./src/include your_file.cpp
# 2. Copy headers to system location
sudo cp include/*.hpp /usr/local/include/
# 3. Use full path
#include "/full/path/to/shm_pub_sub.hpp"
Linker errors
# Error message
undefined reference to `irlab::shm::Publisher<int>::Publisher(std::string const&)'
ð§ Solutions:
# 1. Link pthread library
g++ -pthread your_file.cpp
# 2. Use correct C++ standard
g++ -std=c++17 your_file.cpp
# 3. Link rt library (if needed)
g++ -lrt your_file.cpp
5. ð Python Binding Issues
Import errors
# Error message
ImportError: No module named 'shm_pub_sub'
ð§ Solutions:
# 1. Check Python path
export PYTHONPATH=$PYTHONPATH:/path/to/shm/python/bindings
# 2. Install with pip
pip install ./python_bindings
# 3. Build Python module
cd python_bindings
python setup.py build
python setup.py install
Type conversion errors
# Error message
TypeError: No to_python (by-value) converter found
ð§ Solutions:
# 1. Use correct data types
pub = shm_pub_sub.Publisher("topic", 0, 3) # int default
pub.publish(42) # Use int, not float
# 2. Explicit type conversion
pub.publish(int(42.0)) # Convert float to int
6. ð§ Runtime and Logic Issues
Data corruption or unexpected values
# Symptoms
- Received data is random garbage
- Values change unexpectedly
- Type casting errors
ð§ Solutions:
struct SensorData {
float temperature;
int timestamp;
};
Publisher<SensorData> pub("sensor");
Subscriber<SensorData> sub("sensor");
struct alignas(8) AlignedData {
double value;
int32_t timestamp;
};
bool success;
SensorData data = sub.subscribe(&success);
if (success && data.temperature > -100 && data.temperature < 200) {
process_data(data);
}
Race conditions
# Symptoms
- Intermittent crashes
- Inconsistent behavior
- Segmentation faults
ð§ Solutions:
std::mutex data_mutex;
std::lock_guard<std::mutex> lock(data_mutex);
Publisher<int> pub("topic");
std::this_thread::sleep_for(std::chrono::milliseconds(10));
Subscriber<int> sub("topic");
std::atomic<bool> running{true};
while (running.load()) {
}
ð Debugging Tools and Techniques
1. ð Memory Inspection
# Check shared memory usage
ls -la /dev/shm/
df -h /dev/shm/
# Monitor memory usage
watch -n 1 'ls -la /dev/shm/ | grep shm_'
2. ðŽ Process Monitoring
# Monitor CPU usage
top -p $(pgrep your_program)
# Check file descriptors
lsof -p $(pgrep your_program)
# System calls tracing
strace -p $(pgrep your_program)
3. ð§° Debug Build
# Compile with debug info
g++ -g -O0 -DDEBUG your_file.cpp
# Run with GDB
gdb ./your_program
(gdb) run
(gdb) bt # Backtrace on crash
4. ð Logging and Diagnostics
#ifdef DEBUG
std::cout << "Publisher created for topic: " << topic_name << std::endl;
std::cout << "Shared memory size: " << shm_size << std::endl;
#endif
auto start = std::chrono::high_resolution_clock::now();
pub.publish(data);
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << "Publish time: " << duration.count() << " Ξs" << std::endl;
ð Emergency Procedures
System Cleanup
#!/bin/bash
# emergency_cleanup.sh
echo "Cleaning up shared memory..."
sudo rm -f /dev/shm/shm_*
echo "Killing hanging processes..."
pkill -f your_program_name
echo "Resetting permissions..."
sudo chmod 1777 /dev/shm
echo "Cleanup complete!"
Recovery Steps
# 1. Stop all processes
sudo pkill -f shm_
# 2. Clean shared memory
sudo rm -f /dev/shm/shm_*
# 3. Reset system limits
echo "kernel.shm_max = 268435456" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
# 4. Restart processes
./your_program
ð Getting Help
ð Self-Diagnosis Checklist
- [ ] Are topic names exactly matching?
- [ ] Are data types identical?
- [ ] Is shared memory accessible?
- [ ] Are permissions correct?
- [ ] Is there enough memory?
- [ ] Are both processes running?
ð Report Template
When reporting issues, include:
**Environment:**
- OS: Ubuntu 20.04
- Compiler: GCC 9.3.0
- Library version:
**Problem:**
- What you were trying to do
- What happened instead
- Error messages (full text)
**Code:**
- Minimal reproducing example
- Compilation command used
**System Info:**
- ls -la /dev/shm/
- free -h
- ulimit -a
ðĪ Community Support
- GitHub Issues: Report bugs and get help
- Documentation: Complete API Reference
- Examples: Sample Code Collection
ðĄ Pro Tip: Most issues are solved by checking topic names, data types, and shared memory permissions. Start with these basics before diving deeper! ð