![]() |
Multiple timer objects / deleted timers
Just a quick question for people who know C++ better than me,
I am writing some code for mechanisms on our robot and am trying to have one command for controlling the shooter in teleop and auto modes. I have in OI.cpp: Code:
buttonA->WhileHeld(new ShooterControl(1)); //forwardsand in ShooterControl.cpp Code:
#include "ShooterControl.h"Thanks, Drew |
Re: Multiple timer objects / deleted timers
If you are worried about weather instances getting deleted I think you should be declaring instances on stack and not the heap. With stack allocation do not have to be explicitly de-allocated. http://gribblelab.org/CBootcamp/7_Me...k_vs_Heap.html
|
Re: Multiple timer objects / deleted timers
The new operator creates objects on the heap, which means C++ will not clean them up automatically, and if you lose all pointers to them before cleaning them up you'll get a memory leak. As @teslalab2 said, you can allocate objects on the stack or associated with another object to have C++ automatically delete them when they go out of scope. If you need a heap object but don't want to worry about manually cleaning it up, you can use a std::shared_pointer. To create a shared Timer pointer, use std::make_shared<Timer>()
|
Re: Multiple timer objects / deleted timers
Thank you for the help! I ended up changing them to shared pointers anyway because WPILib switched to them for most of their code.
- Drew |
Re: Multiple timer objects / deleted timers
Quote:
|
Re: Multiple timer objects / deleted timers
In addition to Joe's point the constructor will only be called once. With that in mind you will likely need to move the following out of the constructor and into the initialize method.
timer->Reset(); timer->Start(); ... isDone = false; This will cause them to execute each time the command is ran instead of just once when the robot is started up. In this particular case (assuming your comments are correct) your command will only run once because isDone will already be true on subsequent executions of the command. The timer will likely also not behave as you expect if you don't reset and restart it in initialize. |
| All times are GMT -5. The time now is 09:11 AM. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi