-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve memory management and string handling in ItemInput class #256
Conversation
- Removed unnecessary dynamic memory allocation - Updated value assignment logic for better efficiency
WalkthroughThe changes made to the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Outside diff range comments (1)
src/ItemInput.h (1)
Line range hint
1-304
: Improve class design with proper memory managementThe class needs several architectural improvements to ensure proper memory management and prevent resource leaks:
- Implement the Rule of Three (destructor, copy constructor, assignment operator)
- Consider using RAII principles
- Add clear ownership semantics for the
value
pointerHere's a suggested implementation of the missing methods:
class ItemInput : public MenuItem { public: // ... existing constructors ... // Destructor ~ItemInput() { delete[] value; } // Copy constructor ItemInput(const ItemInput& other) : MenuItem(other.text), value(new char[strlen(other.value) + 1]), view(other.view), cursor(other.cursor), callback(other.callback) { strcpy(value, other.value); } // Assignment operator ItemInput& operator=(const ItemInput& other) { if (this != &other) { MenuItem::operator=(other); char* newValue = new char[strlen(other.value) + 1]; strcpy(newValue, other.value); delete[] value; value = newValue; view = other.view; cursor = other.cursor; callback = other.callback; } return *this; } // ... rest of the class ... };Consider also:
- Using smart pointers if available in your environment
- Implementing move semantics (Rule of Five) if C++11 is available
- Using a fixed-size buffer for embedded systems to avoid dynamic allocation
- Dynamically allocate buffer for value - Update value pointer after concatenation - Free previous value memory allocation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Memory usage change @ 894f51c
Click for full report table
Click for full report CSV
|
Summary by CodeRabbit