I’m trying to enter a number,n and get the least super lucky number that is more than or equal to n.
Super lucky: it’s decimal representation contains equal amount of digits 4 and 7. For example, numbers 47, 7744, 474477 are super lucky and 4, 744, 467 are not.
Here’s my code
#include<iostream>
#include<string>
using namespace std;
void superLucky(int n,string s, int count4, int count7)
{
if (s.size() > 10)
return;
if (( stoi(s) >= n ) && (count4 == count7) && (count4+count7)!=0)
{
cout << s << endl;
return;
}
superLucky(n, s + '4', count4+1, count7);
superLucky(n, s + '7',count4,count7+1);
}
int main()
{
int n;
cin >> n;
superLucky(n, "", 0, 0);
return 0;
}
Once I input some integer I get debug error R6010 — abort() has been called. What this means ? and how can I fix this ?
Всем доброго дня!
Использую поток от STL (std::thread) из нового стандарта (работаю с Microsoft Visual C++ Compiler Nov 2012 CTP (v120_CTP_Nov2012)). Есть класс TCPServer, содержащий в себе этот поток:
class TCPServer final : public WaitingPoint { public: boost::asio::io_service* ioService; private: std::atomic_bool isExit; std::thread thread; boost::asio::ip::tcp::acceptor* ioAcceptor; std::atomic_bool isWaitingAccept; std::shared_ptr<ConnectionPoint> connectionAccept; ... };
Затем в один из моментов я запускаю поток (то есть до этого функцию обработки я ему не указывал):
Result TCPServer::init(void) { isExit=false; ioService=new (std::nothrow) boost::asio::io_service(); if(!ioService) return Result::errorMemory; ioAcceptor=new (std::nothrow) boost::asio::ip::tcp::acceptor(*ioService); if(!ioAcceptor) return Result::errorMemory; tcp.set(ioAcceptor,nullptr); thread=std::thread(std::bind(&TCPServer::loop,std::static_pointer_cast<TCPServer>(shared_from_this()))); return Result::ok; }
Он крутит цикл:
Result TCPServer::loop(void) { boost::system::error_code ec; while(!isExit) { ioService->run_one(ec); ioService->reset(); } return Result::ok; }
И когда мне уже он не нужен, я вызываю это:
void TCPServer::free(void) { ioAcceptor->close(); connectionAccept.reset(); if(!isExit) { isExit=true; ioService->reset(); ioService->stop(); if(thread.joinable()) thread.join(); } }
Грубо говоря у меня есть функция release():
void WaitingPoint::release(void) { if(isRemoved) return; isRemoved=true; mConnectionPoints.lock(); size_t len=vConnectionPoints.size(); for(size_t i=0;i<len;i++) { auto& item=vConnectionPoints[i]; item->isRemoved=true; item->free(); } vConnectionPoints.clear(); mConnectionPoints.unlock(); std::shared_ptr<WaitingPoint> p(shared_from_this()); Core::instance->freeWaitingPoint(this); free(); }
Вызовом которой я удаляю сервер. Делаю я это вот так:
void release(void) { if(waitingPoint) waitingPoint->release(); while(true); if(connectionPoint) connectionPoint->release(); if(core) core->release(); std::cout<<std::endl<<"Press Enter for exit..."; std::cin.get(); }
Тут я намеренно зациклил программу. В общем после выполнения задуманного мной сценария
и дохода до бесконечного цикла, программа в какой-то момент (уже вися основным потоком в бесконечном цикле) рушится с сообщением:
И останавливается вот тут:
То есть проблема с потоком. В чём может быть ошибка и как её исправить?
-
Изменено
26 января 2013 г. 11:10
Lets take a close look at these lines lines:
for (...) {
...
std::promise<string> prom;
...
std::thread(findText,str,search,std::ref(prom)).detach();
}
First you create a local variable prom
, and then you pass a reference to that variable to the thread.
The problem with this is that once the loop iterates, the variable prom
goes out of scope and the object is destructed. The reference you once had no longer have anything it references. Using the reference will lead to undefined behavior.
So the solution is to not use references (or pointers to the prom
variable), which leads to problems because std::promise
can’t be copied. It can, however, be moved:
std::thread(findText,str,search,std::move(prom)).detach();
Fir this you might need to make the thread function take the promise argument as a rvalue reference:
void findText(std::string filename, std::string word , promise<string> &&prom) {
...
}
If the above solution doesn’t work, then you could use dynamic allocation using the new C++11 smart pointers like std::unique_ptr
.
Then the thread function should take the smart pointer by value, like
void findText(std::string filename, std::string word , std::unique_ptr<std::promise<string>> prom) {
...
}
And you create the thread like
auto prom = std::make_unique<std::promise<std::string>>();
// Or if you don't have std::make_unique
// std::unique_ptr<std::promise<std::string>> prom(new std::promise<std::string>);
futures.push_back(prom->get_future();
std::thread(findText,str,search,std::move(prom)).detach();
Remember that in your thread function (findText
) the variable prom
is a pointer and you need to use the arrow operator when using it, like e.g.
prom->set_value_at_thread_exit(filename);
// ^^
// Note "arrow" operator here
- Forum
- General C++ Programming
- R6010 -abort() has been called
R6010 -abort() has been called
Hi all and thanks in advance for any help that you may bring. I’m an avid reader of this site and use it daily to help advance my learning. I’m running into quite the trouble with this program I’m writing for school. As you can see from the topic I’m receiving this error message. «R6010 -abort() had been called» I believe I’m getting it from either a memory issue or something related to the two files I’m using, one to read and one to write to.
I’ll post my code.
|
|
There’s many edits done to this code and sections commented out as they were only for testing. The issue I’m running into is I need to read the file name «myfile.txt» which looks exactly like this:
Cefalo, Joseph 100
Marin, Alexa 97
Loftus, Jay 43
Pehl, Kaila 25
Cefalo, Dunkin 88
Marin, Cody 54
Bond, James 67
Ketchum, Ash 71
Kline, Stacey 16
Rogers, Emily 06
Once reading them into the vector it’s supposed to sort them by number highest to lowest and write to file named «finalfile.txt»
It reads the file as I dont get an error from it however it gives me the error upon trying to write and aborts.
I’m sure I’m missing something here as I’m a noob.
Thanks for any help.
It would fail in `sorting()’ if the string does not have a TAB character.
That is the case of `Marin, Alexa 97′
> it’s supposed to sort them by number highest to lowest and write to file named «finalfile.txt»
you are writing to `myfile.txt’
Last edited on
ne555 saying i love you would be an understatement. I knew it was something so minute that was causing the error. You are absolutely correct. Nice catch on the error of the tab. And yes I had commented out the «finalfile.txt» through all of my testing. Sometimes I go so crazy trying to test and retest and I forget to put stuff back in.
As a new programmer the abort error was making me nuts as I didn’t know why or how to fix it as there were no build errors.
Thanks for the second pair of eyes and your assistance. I’ve been staring at this code for almost 3 days trying diff ways over and over to fix it. Dummy me never checked the file.
Thanks again.
Topic archived. No new replies allowed.
Preface
R6010 -abort() has been called errors are often encountered during code debugging. Here, let’s analyze the problem.
The manifestation of the error:
The reasons for this are:
1. Illegal pointer access and memory leak;
2. Check it again, there must be a problem with the pointer. The pointer range you set is not right for your operation;
3. The pointer access memory is out of bounds and there is a problem;
4. It is because Chinese is not supported;
5. The memory is not allocated enough;
6. The problem found in the later inspection should be the problem of multi-threaded access to resources. ;
7. Check if exe and dll are mixed with different versions of crt;
Solution:
First: check that the requested space has not been released;
second: check whether the stack space has been fully allocated, it is recommended that the memory allocation should not be too large each time, and remember to release;
third: the pointer points to unexpected memory position;
In response to my question,
Found an error in the following code:
And the declaration of this function is:
extern void features( const Mat& image, Mat& features);
the reason:
The global variable image has been declared and defined in the program, and a mutex lock is used for the image variable during a short period of running. Multiple threads will use the image variable, and the image variable is used many times in the features function that has problems, so Error
Solution:
Where the image variable is used in the features function, use other variable names;
Another reason is,
There is a problem with the variable data in the code. For example, the function input should be a color RGB image, but the grayscale image used before, forgot to remove the grayscale part, so an error occurred.