The problem is it prints the full name but not the rest of the lines about the person.
Could someone, please guide me?
I do really appreciate your help!
auto itr = find(my_vec.begin(), my_vec.end(), search );
if(itr != my_vec.end())
{
std::cout << "Match found " << search << std::endl;
std::cout << "\nFull name: " << search << std::endl;
} else {
std::cout << "Match not found "<< std::endl;
}
There are a few style problems with your code:
No need to explicitly initialize strings, they will be empty by default (see here).
Keep a consistent style. For example, either always start brackets in the same line as the function signature or in the next line.
No need to close the file explicitly at the end of the function, this is done when the object goes out of scope (see (destructor) here).
No need to include <map> and <iomanip> headers.
Don't keep unused variables.
Give suggestive names to your variables.
Do not return error codes to the OS when the app is working as it should. Not finding a name is not an error, is it?
It seems your file has 6 entries per contact, so all you have to do is print 5 more lines. You do not need to store the lines in a vector, just parse and print them as you go. Here is an example:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
void findContact(std::string fullName, std::string contactListPath) {
std::ifstream inFile{contactListPath};
if (!inFile) {
std::cerr << "File could not be open!" << std::endl;
return;
}
std::string line;
while (std::getline(inFile, line)) {
if (line == fullName) {
std::cout << "Match found: \n";
std::cout << "\nFull name: " << fullName;
std::cout << "\nAddress: " << (std::getline(inFile, line), line);
std::cout << "\nE-mail: " << (std::getline(inFile, line), line);
std::cout << "\nPhone: " << (std::getline(inFile, line), line);
std::cout << "\nBirthday: " << (std::getline(inFile, line), line);
std::cout << "\nNote: " << (std::getline(inFile, line), line) << std::endl;
return;
}
}
std::cout << "Match not found " << std::endl;
}
int main() {
std::string fullName;
std::string contactListPath;
std::cout << "Enter full name to search: ";
std::getline(std::cin, fullName);
std::cout << "Enter path to contact list: ";
std::getline(std::cin, contactListPath);
findContact(fullName, contactListPath);
return 0;
}
If every entry contains 6 lines. Then you can print all the lines starting from the line that you found like:
auto itr = find(my_vec.begin(), my_vec.end(), search );
if(itr != my_vec.end())
{
std::cout << "Match found " << std::endl;
// print the next 6 lines
for(int remaining = 6;remaining > 0 && itr!=my_vec.end(); itr++,remaining--) {
std::cout << *itr << std::endl;
}
} else {
std::cout << "Match not found "<< std::endl;
}
I am new to multithreading. I made program which works fine when single threaded.but when i add another thread to run asio io service "with main thread running same io service" it gives me this error :
Exception thrown at (ntdll.dll) Access violation reading location
I use vs 2017 and i tried to use call stack to locate error but it happens at ntdll.dll code which i do not have its source.
in addition ,i tried to exclude parts of code but it appears that error happens when async function returns from io service.
the program is large so i did not put code.
i want to know if there is way using vs2017 to link the ntdll.dll error to the part of my source code which lead to this error.
call stack at error onlu shows:
ntdll.dll!__except_handler4()
ntdll.dll!ExecuteHandler2#20()
ntdll.dll!ExecuteHandler#20()
if files are required ,i will put their code.
thanks
UPDATE:
this function throws alot even though i launch it after locking mutex.
i think there is something wrong regarding memory usage.it gives me access violation .
std::string HTTPRequest::output_compressed_file_2(boost::shared_ptr<unsigned char> data_bin_buffer, size_t buffer_size, const char * file_name)
{
std::string file_name_path_string = file_name;
std::ofstream ofs2(file_name_path_string, std::ios::binary);
ofs2.write(reinterpret_cast<const char*>(data_bin_buffer.get()), buffer_size);
ofs2.close();
return file_name_path_string;
}
it makes error related to memory .when i inspect call stack and threads , i found that it is called in both threads.in one thread it is just called ,in the other thread it is already called and it calls inside function sputn ,xsputn,copy and memcpy where throw happens.
i think that calling it in the other thread before it finishes in the first thread leads to error.i am trying to find any shared resource but i can not se any shared resource.this function is called inside another function which is called inside third function which is ran inside io service and this third function is member of class instance HttpRequest .
there are two instances of HttpRequest.
i can not find the reason of memcpy error.
here is the code in which this function is called :
void HTTPRequest::Execute(boost::asio::yield_context yield_r, std::string request_name, boost::shared_ptr<std::map<std::string, boost::shared_ptr<HTTPResponse>>> mHTTPClient_Responses_Map_shared_pointer)
{
std::map<std::string, boost::shared_ptr<HTTPResponse>> & mHTTPClient_Responses_Map = boost::ref(*mHTTPClient_Responses_Map_shared_pointer).get() ;
ptime startFetch = second_clock::local_time();
//??5-17-2020 isolate multithreaded error
/*
boost::unique_lock<boost::mutex> cancel_lock(mCancelMutex);
if (mWasCancelled)
{
cancel_lock.unlock();
OnFinish(boost::system::error_code(boost::asio::error::operation_aborted));
m_formatting_ostream << "Request #" << this->GetId() << " for " << mUrl << " has been cancelled by the user at start of HTTPRequest::Execute coroutine." << std::endl;
m_formatting_ostream.flush();
////allam2020 change UniqueSignalValue to url
boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "c_E", request_name, m_formatting_ostream_string, mBOOST_LOGMutex);
//5-16-2020m_formatting_ostream.clear();
m_formatting_ostream_string.clear();
//p.set_value(request_name);
//return request_name;
//5-6-2020 i think i should return when cancelled
return;
}
cancel_lock.unlock();
*/
bool iterator_failed = true;
boost::system::error_code ec;
//5-7-2020
// Compose the request message.
mRequestBuf += "GET " + mUri + " HTTP/1.1\r\n";
// Add mandatory header.
mRequestBuf += "Host: " + mHost + "\r\n";
mRequestBuf += "\r\n";
for (auto iterator_resolve : *mRequestSharedPtrVecResolverIterator)
{
//??5-17-2020 isolate multithreaded error
BOOST_LOG((*mHTTPRequest_LoggingInstance_shared_pointer).mloggerCoutLog) << boost::this_thread::get_id() << "\t"<<"Request #" << this->GetId() << " for " << mUrl <<" trying to send request using " << iterator_resolve->endpoint().address().to_string() << std::endl<<"\n";
//5-8-2020 changing continue to endless loop except for 404
for (int mIrange2 =1; mIrange2 < ATTEMPTS; ++mIrange2)
{
++HTTPRequest::mIrange ;
resolver_iterator iterator_connect = boost::asio::async_connect(mSock, iterator_resolve, yield_r[ec]);////allam 2020 this gets us back to io_stream_run
if (ec.value() == boost::system::errc::errc_t::success)
{
////allam 2020
iterator_failed = false;
//??5-17-2020 isolate multithreaded error
boost::unique_lock<boost::mutex> cancel_lock(mCancelMutex);
if (mWasCancelled)
{
cancel_lock.unlock();
OnFinish(boost::system::error_code(boost::asio::error::operation_aborted));
m_formatting_ostream << "Request #" << this->GetId() << " for " << mUrl << " has been cancelled by the user after returning from async_connect inside HTTPRequest::Execute using"<< iterator_resolve->endpoint().address().to_string() << std::endl;
m_formatting_ostream.flush();
//4-26-2020
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "cancel_async_connect_Execute", iterator_resolve->endpoint().address().to_string(), m_formatting_ostream_string);
boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, mHTTPRequest_Symbol_str, "c_a_c_E", request_name,m_formatting_ostream_string, mBOOST_LOGMutex);
//5-16-2020m_formatting_ostream.clear();
m_formatting_ostream_string.clear();
//p.set_value(request_name);
//return request_name;
return;
}
cancel_lock.unlock();
// Send the request message.
SendRequest(yield_r);
}
else if (ec.value() != boost::system::errc::errc_t::success)
{
OnFinish(ec);
//??5-17-2020 isolate multithreaded error
BOOST_LOG((*mHTTPRequest_LoggingInstance_shared_pointer).mloggerCoutLog) <<"Request #" << this->GetId() << " for " << mUrl <<" failed after trying " << mIrange2 << "times" << " to async_connect inside HTTPRequest::Execute " << std::endl;
continue;
}
if (mContinue_for==true)
{
mContinue_for = !(mContinue_for);
//5-5-2020 async timer
mAsyncTimer.expires_from_now(boost::asio::chrono::milliseconds(mIrange));//5-5-2020
mAsyncTimer.async_wait(yield_r);
//5-7-2020
if (mSendRequest == 0)
{
if (mReadStatusLine == 0)
{
if (mHttp_1_1 == 0)
{
if (mStatusCodeNot200 == 0)
{
if (mReadResponseHeaders == 0)
{
if (mReadResponseBody == 0)
{
////allam2020 4-4-2020 no error present and response is recieved in its mHTTPResponse SO DO NOTHING
}
else if (mReadResponseBody != 0)
{
mIrange2--;
}
}
else if (mReadResponseHeaders != 0)
{
mIrange2--;
}
}
else if (mStatusCodeNot200 != 0)
{
}
}
else if (mHttp_1_1 != 0)
{
mIrange2--;
}
}
else if (mReadStatusLine != 0)
{
mIrange2--;
}
}
else if (mSendRequest != 0)
{
mIrange2--;
}
continue;
}
// Response is correct.
//??5-17-2020 isolate multithreaded error
m_formatting_ostream << boost::this_thread::get_id() << "\t" << "Request #" << this->GetId() << " for " << mUrl << " Fetched " << mUrl << " completed in : " << (second_clock::local_time() - startFetch) << "with HTTP code :" << mResponsePtr->get_status_code() << "\t" << "and the code reasonPhrase is :" << HttpStatus::reasonPhrase(static_cast<int>(mResponsePtr->get_status_code())) << "with certain resolver iterator " << iterator_resolve->endpoint().address().to_string() << std::endl;
m_formatting_ostream.flush();
//4-26-2020
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "http_request_completed", HttpStatus::reasonPhrase(static_cast<int>(mResponsePtr->get_status_code())), m_formatting_ostream_string, mBOOST_LOGMutex);
boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, mHTTPRequest_Symbol_str, "req_comp", HttpStatus::reasonPhrase(static_cast<int>(mResponsePtr->get_status_code())), m_formatting_ostream_string, mBOOST_LOGMutex);
//5-16-2020m_formatting_ostream.clear();
m_formatting_ostream_string.clear();
if (mResponsePtr->get_response_buf().size() <= 0)
{
//??5-17-2020 isolate multithreaded error
m_formatting_ostream << boost::this_thread::get_id() << "\t" << "Request #" << this->GetId() << " for " << mUrl << " Fetched " << mUrl << " with Buffer for " << mUrl << " is empty " << "\n" << "with HTTP code :" << mResponsePtr->get_status_code() << "\n" << "and the code reasonPhrase is :" << HttpStatus::reasonPhrase(static_cast<int>(mResponsePtr->get_status_code())) << std::endl;
m_formatting_ostream.flush();
//4-26-2020
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "http_request_completed_empty", HttpStatus::reasonPhrase(static_cast<int>(mResponsePtr->get_status_code())), m_formatting_ostream_string, mBOOST_LOGMutex);
boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, mHTTPRequest_Symbol_str, "req_comp_empty", HttpStatus::reasonPhrase(static_cast<int>(mResponsePtr->get_status_code())), m_formatting_ostream_string, mBOOST_LOGMutex);
//5-16-2020m_formatting_ostream.clear();
m_formatting_ostream_string.clear();
}
//continue work on response
mHTTPRequest_response_name = "response_" + request_name;
mHTTPClient_Responses_Map[mHTTPRequest_response_name] = GetResponseSharedPtr();
//4-24-2020 here i will save to file
std::size_t pos = mUrl.find("h_ticks.bi5"); // position of "h_ticks.bi5" in str
std::string mHTTPRequest_file_name = mUrl.substr(pos - 2); // get from "h_ticks.bi5" to the end
std::string mHTTPRequest_hour_name = mUrl.substr(pos - 2,2); // get from "h_ticks.bi5" to the end
date mHTTPRequest_file_name_ptime_epoch_date =mHTTPRequest_HTTPClient_shared_pointer->m_HttpClient_Day_Full_DateGet() ;
ptime mHTTPRequest_file_name_ptime_epoch(mHTTPRequest_file_name_ptime_epoch_date, pt::hours(std::stoi(mHTTPRequest_hour_name)));
std::string compressed_file_path_string =output_compressed_file(mResponsePtr->get_response_buf(), mHTTPRequest_file_name);
path compressed_file_path{ compressed_file_path_string };
read_bi5_main(compressed_file_path, mHTTPRequest_file_name_ptime_epoch);
break;
}
//??5-17-2020 isolate multithreaded error
/*
//the following conditions test the result of send request
if (mSendRequest == 0)
{
if (mReadStatusLine == 0)
{
if (mHttp_1_1 == 0)
{
if (mStatusCodeNot200 == 0)
{
if (mReadResponseHeaders == 0)
{
if (mReadResponseBody == 0)
{
////allam2020 4-4-2020 no error present and response is recieved in its mHTTPResponse SO DO NOTHING
}
else if (mReadResponseBody != 0)
{
m_formatting_ostream << "Request #" << this->GetId() << " for " << mUrl << " has failed completely after trying" << ATTEMPTS << "times" << " to async_read inside HTTPRequest::ReadResponseBody to get ResponseBody " << "with certain resolver iterator " << iterator_resolve->endpoint().address().to_string() << std::endl;
m_formatting_ostream.flush();
//4-26-2020
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "failed_async_read_inside_HTTPRequest_ReadResponseBody", "requestFailed_ReadResponseBody_Iterator_" + iterator_resolve->endpoint().address().to_string(), m_formatting_ostream_string);
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "fail_a_r_in_RRB", "reqFail_RRB_It_" + iterator_resolve->endpoint().address().to_string(),m_formatting_ostream_string, mBOOST_LOGMutex);
boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, mHTTPRequest_Symbol_str, "f_ar_RRB", "_" + request_name, m_formatting_ostream_string, mBOOST_LOGMutex);
//5-16-2020m_formatting_ostream.clear();
m_formatting_ostream_string.clear();
}
}
else if (mReadResponseHeaders != 0)
{
m_formatting_ostream << "Request #" << this->GetId() << " for " << mUrl << " has failed completely after trying" << ATTEMPTS << "times" << " to async_read_until inside HTTPRequest::ReadResponseHeadersto get ResponseHeaders " << "with certain resolver iterator " << iterator_resolve->endpoint().address().to_string() << std::endl;
m_formatting_ostream.flush();
//4-26-2020
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "failed_async_read_until_inside_HTTPRequest_ReadResponseHeaders", "requestFailed_ReadResponseHeaders_Iterator_" + iterator_resolve->endpoint().address().to_string(), m_formatting_ostream_string);
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "fail_a_r_until_in_RRH", "reqFail_RRH_It_" + iterator_resolve->endpoint().address().to_string(), m_formatting_ostream_string, mBOOST_LOGMutex);
boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, mHTTPRequest_Symbol_str, "f_aru_RRH", "_" + request_name, m_formatting_ostream_string, mBOOST_LOGMutex);
//5-16-2020m_formatting_ostream.clear();
m_formatting_ostream_string.clear();
}
}
else if (mStatusCodeNot200 != 0)
{
m_formatting_ostream << "Request #" << this->GetId() << " for " << mUrl << " has failed completely after" << ATTEMPTS << "times" << " to async_read_until inside HTTPRequest::ReadStatusLine because of status_code not 200:" << http_errors::invalid_response << "the error code is :" << mStatusCode << "\n" << "and the error reasonPhrase is :" << HttpStatus::reasonPhrase(static_cast<int>(std::stoul(mStatusCode))) << "with certain resolver iterator " << iterator_resolve->endpoint().address().to_string() << std::endl;
m_formatting_ostream.flush();
//4-26-2020
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "failed_StatusCodeNot200_inside_HTTPRequest_ReadStatusLine", "requestFailed_ReadStatusLine_StatusCodeNot200:StatusCode_is_" + mStatusCode + "with_Iterator_" + iterator_resolve->endpoint().address().to_string(), m_formatting_ostream_string);
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "fail_Not200_in_RSL", "reqFail_RSL_Not200:St_is_" + mStatusCode + "with_It_" + iterator_resolve->endpoint().address().to_string(), m_formatting_ostream_string, mBOOST_LOGMutex);
boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, mHTTPRequest_Symbol_str, "f_N200_RSL", "_" + mStatusCode + "_" + request_name, m_formatting_ostream_string, mBOOST_LOGMutex);
//5-16-2020m_formatting_ostream.clear();
m_formatting_ostream_string.clear();
}
}
else if (mHttp_1_1 != 0)
{
////4-2-2020
m_formatting_ostream << "Request #" << this->GetId() << " for " << mUrl << " after trying " << ATTEMPTS << "times" << " to async_read_until inside HTTPRequest::ReadStatusLine because of bad not http/1.1 version response" << mHTTP_Version << "recieved with certain resolver iterator " << iterator_resolve->endpoint().address().to_string() << std::endl;
m_formatting_ostream.flush();
//4-26-2020
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "failed_Http_1_1_inside_HTTPRequest_ReadStatusLine", "requestFailed_ReadStatusLine_Http_1_1_Iterator_" + iterator_resolve->endpoint().address().to_string(), m_formatting_ostream_string);
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "fail_Http_1_1_in_RSL", "reqFail_RSL_Http_1_1_It_" + iterator_resolve->endpoint().address().to_string(), m_formatting_ostream_string, mBOOST_LOGMutex);
boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, mHTTPRequest_Symbol_str, "f_H11_RSL", "_" + request_name, m_formatting_ostream_string, mBOOST_LOGMutex);
//5-16-2020m_formatting_ostream.clear();
m_formatting_ostream_string.clear();
}
}
else if (mReadStatusLine != 0)
{
////4-2-2020
m_formatting_ostream << "Request #" << this->GetId() << " for " << mUrl << " failed completely after trying " << ATTEMPTS << "times" << " to async_read_until inside HTTPRequest::ReadStatusLine with certain resolver iterator " << iterator_resolve->endpoint().address().to_string() << std::endl;
m_formatting_ostream.flush();
//4-26-2020
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "failed_async_read_until_inside_HTTPRequest_ReadStatusLine", "requestFailed_ReadStatusLine_Iterator_" + iterator_resolve->endpoint().address().to_string(), m_formatting_ostream_string, mBOOST_LOGMutex);
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "fail_a_r_u_in_RSL", "reqFail_RSL_It_" + iterator_resolve->endpoint().address().to_string(), m_formatting_ostream_string, mBOOST_LOGMutex);
boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, mHTTPRequest_Symbol_str, "f_aru_RSL", "_" + request_name, m_formatting_ostream_string, mBOOST_LOGMutex);
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "fail_aru_in_RSL", "_" , m_formatting_ostream_string, mBOOST_LOGMutex);
//5-16-2020m_formatting_ostream.clear();
m_formatting_ostream_string.clear();
}
}
else if (mSendRequest != 0)
{
////4-2-2020
m_formatting_ostream << "Request #" << this->GetId() << " for " << mUrl << " failed after trying " << ATTEMPTS << "times" << " to async_write inside HTTPRequest::SendRequest with certain resolver iterator " << iterator_resolve->endpoint().address().to_string() << std::endl;
m_formatting_ostream.flush();
//4-26-2020
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "failed_async_write_inside_HTTPRequest_SendRequest", "requestFailed_SendRequest_Iterator_" + iterator_resolve->endpoint().address().to_string(), m_formatting_ostream_string, mBOOST_LOGMutex);
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "fail_aw_in_SR", "_" + iterator_resolve->endpoint().address().to_string(), m_formatting_ostream_string, mBOOST_LOGMutex);
boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, mHTTPRequest_Symbol_str, "f_aw_SR", "_" + request_name, m_formatting_ostream_string, mBOOST_LOGMutex);
//5-16-2020m_formatting_ostream.clear();
m_formatting_ostream_string.clear();
}
*/
if (iterator_failed == true)
{
//??5-17-2020 isolate multithreaded error
m_formatting_ostream << "Request failed for " << mUrl << " after trying " << ATTEMPTS << "times" << " to async_connect inside HTTPRequest::Execute with certain resolver iterator "<< iterator_resolve->endpoint().address().to_string() << std::endl;
m_formatting_ostream.flush();
////allam 2020 i might need to pass iterator resolve which has failed
//4-26-2020
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "failed_async_connect_inside_HTTPRequest_Execute", "requestFailed_Iterator_" + iterator_resolve->endpoint().address().to_string(), m_formatting_ostream_string, mBOOST_LOGMutex);
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "f_ac_E", "_" + iterator_resolve->endpoint().address().to_string()+ request_name, m_formatting_ostream_string, mBOOST_LOGMutex);
std::string string_replace = { mSock.remote_endpoint().address().to_string() };
replace(string_replace, ".", "");
boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, mHTTPRequest_Symbol_str, "f_ac_E", "_" + string_replace + request_name, m_formatting_ostream_string, mBOOST_LOGMutex);
//5-16-2020m_formatting_ostream.clear();
m_formatting_ostream_string.clear();
continue;////allam 2020 here i should continue for next iterator
}
}
//??5-17-2020 isolate multithreaded error
if (iterator_failed == true)
{
m_formatting_ostream << "Request failed for " << mUrl << " after trying " << ATTEMPTS << "times" << " to async_connect inside HTTPRequest::Execute with ALL resolver iterators" << std::endl;
m_formatting_ostream.flush();
////allam 2020 i might need to pass iterator resolve which has failed
//4-26-2020
//boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, "failed_async_connect_inside_HTTPRequest_Execute", "requestFailed_Iterator_" + GetmUrlLogger(), m_formatting_ostream_string, mBOOST_LOGMutex);////allam2020 ?????i might need to change this from GetmUrlLogger to request name argument of Execute???????????????????4-2-2020
boost_log_function(mHTTPRequest_LoggingInstance_shared_pointer, mHTTPRequest_Symbol_str, "f_ac_E", "_" + GetmUrlLogger()+ request_name,m_formatting_ostream_string, mBOOST_LOGMutex);////allam2020 ?????i might need to change this from GetmUrlLogger to request name argument of Execute???????????????????4-2-2020
//5-16-2020m_formatting_ostream.clear();
m_formatting_ostream_string.clear();
}
////allam2020 should i put if conditions for mSendRequest ....mReadResponseBody????? to identify final complete error at these functions and end of 5 attempts
}
and this is function read_bi5_main:
int HTTPRequest::read_bi5_main(boost::filesystem::path p, ptime epoch)
{
unsigned char *buffer;
size_t buffer_size;
int counter;
size_t raw_size = 0;
std::string filename_string = p.generic_string();
path p2 = p;
p2.replace_extension(".bin");
std::string filename_string_2_bin =p2.generic_string() ;
path p3 = p;
p3.replace_extension(".csv");
std::string filename_string_2_csv = p3.generic_string();
const char *filename = filename_string.c_str();
const char *filename_2_bin = filename_string_2_bin.c_str();
const char *filename_2_csv = filename_string_2_csv.c_str();
if (fs::exists(p) && fs::is_regular(p))
{
buffer_size = fs::file_size(p);
buffer = new unsigned char[buffer_size];
}
else {
//??5-17-2020 isolate multithreaded error
BOOST_LOG((*mHTTPRequest_LoggingInstance_shared_pointer).mloggerCoutLog) << "Error: couldn't access the data file. |"
<< filename << "|" << std::endl;
return 2;
}
std::ifstream fin(filename, std::ifstream::binary);
//fin.open(filename, std::ifstream::binary);
fin.read(reinterpret_cast<char*>(buffer), buffer_size);
fin.close();
//5-11-2020 the next line will be commented and put in HTTPCLIent constructor
//mHTTPRequest_Symbol_str= mHTTPRequest_HTTPClient_shared_pointer->Get_mHttpClient_HttpSymbolPrepareGet_shared_pointer()->mSymbol_strGet() ;
std::size_t pos = mHTTPRequest_Symbol_str.find("JPY");// position of "h_ticks.bi5" in str
double PV;
if (pos != std::string::npos)
{
PV = PV_YEN_PAIR;
}
else
{
PV = PV_DOLLAR_PAIR;
}
boost::shared_ptr<unsigned char> data_bin_buffer = boost::make_shared<unsigned char>() ;
//boost::unique_lock<boost::mutex> read_bi5_to_bin_lock(m_read_bi5_to_binMutex);
n47::tick_data *data = n47::read_bi5_to_bin(
buffer, buffer_size, epoch, PV, &raw_size, data_bin_buffer.get());
//read_bi5_to_bin_lock.unlock();
//5-11-2020 here i will save binary file
std::string file_name_path_string=output_compressed_file_2(data_bin_buffer, raw_size, filename_2_bin);
path file_name_path_2{ file_name_path_string };
buffer_size = 0;
if (fs::exists(file_name_path_2) && fs::is_regular(file_name_path_2))
{
//??5-17-2020 isolate multithreaded error
BOOST_LOG((*mHTTPRequest_LoggingInstance_shared_pointer).mloggerCoutLog) << boost::this_thread::get_id() <<"\t we can access the data .bin file. |"
<< filename_2_bin << "| with size ="<< fs::file_size(file_name_path_2) << std::endl;
}
else {
//??5-17-2020 isolate multithreaded error
BOOST_LOG((*mHTTPRequest_LoggingInstance_shared_pointer).mloggerCoutLog) << "Error: couldn't access the data .bin file. |"
<< filename_2_bin << "|" << std::endl;
return 2;
}
n47::tick_data_iterator iter;
//5-11-2020 here i will save file.csv from data which is pointer to vector to pointers to ticks
if (data == 0) {
//??5-17-2020 isolate multithreaded error
BOOST_LOG((*mHTTPRequest_LoggingInstance_shared_pointer).mloggerCoutLog) << "Failure: Failed to load the data!" << std::endl;
//5-14-2020 file is empty
//return 0;
}
//5-15-2020 take care that without else ,error happens with empty files because data is pointer to vector of pointers to ticks .so when data is made inside read_bi5 ,it is made as null pointer and later it is assigned to vector if file has ticks.if file does not have ticks ,then it is just returned as null pointer .so when dereferencing null pointer we got error
else if (data->size() != (raw_size / n47::ROW_SIZE)) {
//??5-17-2020 isolate multithreaded error
BOOST_LOG((*mHTTPRequest_LoggingInstance_shared_pointer).mloggerCoutLog) << "Failure: Loaded " << data->size()
<< " ticks but file size indicates we should have loaded "
<< (raw_size / n47::ROW_SIZE) << std::endl;
//5-14-2020 file is empty
//return 0;
}
//??5-17-2020 isolate multithreaded error
BOOST_LOG((*mHTTPRequest_LoggingInstance_shared_pointer).mloggerCoutLog) << "time, bid, bid_vol, ask, ask_vol" << std::endl;
counter = 0;
std::ofstream out_csv(filename_string_2_csv);
if (data == 0)
{
}
else if (data != 0)
{
////read_bi5_to_bin_lock.lock();
for (iter = data->begin(); iter != data->end(); iter++) {
//5-11-2020 here i will save file.csv from data which is pointer to vector to pointers to ticks>>>>>>>here i should open file stream for output and save data to it
out_csv << ((*iter)->epoch + (*iter)->td) << ", "
<< (*iter)->bid << ", " << (*iter)->bidv << ", "
<< (*iter)->ask << ", " << (*iter)->askv << std::endl;
//??5-17-2020 isolate multithreaded error
BOOST_LOG((*mHTTPRequest_LoggingInstance_shared_pointer).mloggerCoutLog) <<
boost::this_thread::get_id() << "\t"<<((*iter)->epoch + (*iter)->td) << ", "
<< (*iter)->bid << ", " << (*iter)->bidv << ", "
<< (*iter)->ask << ", " << (*iter)->askv << std::endl;
counter++;
}
////read_bi5_to_bin_lock.unlock();
}
out_csv.close();
//5-13-2020
//??5-17-2020 isolate multithreaded error
BOOST_LOG((*mHTTPRequest_LoggingInstance_shared_pointer).mloggerCoutLog) << ".end." << std::endl << std::endl
<< "From " << raw_size << " bytes we read " << counter
<< " records." << std::endl
<< raw_size << " / " << n47::ROW_SIZE << " = "
<< (raw_size / n47::ROW_SIZE) << std::endl;
delete data;
delete[] buffer;
return 0;
}
this function uses lzma decompression files which i didnot include.if required i will include.
i wish you give me mind map of how to reach error source.
thanks
UPDATE:
this is read_bi5_to_bin
tick_data* read_bi5_to_bin(
unsigned char *lzma_buffer, size_t lzma_buffer_size, pt::ptime epoch,
float point_value, size_t *bytes_read, unsigned char* buffer_decompressed) {
tick_data *result = 0;
// decompress
int status;
buffer_decompressed = n47::lzma::decompress(lzma_buffer,
lzma_buffer_size, &status, bytes_read);
if (status != N47_E_OK)
{
bytes_read = 0;
}
else {
// convert to tick data (with read_bin).
result = read_bin(buffer_decompressed, *bytes_read, epoch, point_value);
//delete[] buffer;
}
return result;
}
Oh. Now I remember. I still see the same problems in the code. Mainly, it is (vastly) over-complicated. I see many things worth noting, but I'm not sure it will help, seeing that the last time didn't help much either: boose wait_for_any with asio io-service...how can i return future suitable for wait_for_any?
boost::shared_ptr<unsigned char> data_bin_buffer = boost::make_shared<unsigned char>() ;
Why are you creating "buffers" of single byte? Why are you creating them as a shared resource?
your read_bi5_main does it almost correctly, and seems to be copied from https://github.com/ninety47/dukascopy/blob/8654ad197bdf55579544cf71735369f0d227569f/test/test_read_bi5.cpp#L42 or similar.
n47::read_bi5 returns the bytes read in raw_size, and you use that as a valid buffer size.
I cannot find documentation for read_bi5_to_bin, and consequently don't know how exactly it uses that bin_buffer, BUT I can tell that you're likely doing bogus with it:
output_compressed_file_2(data_bin_buffer, raw_size, filename_2_bin);
Here you're treating data_bin_buffer as if it points to a buffer of any size raw_size where the only valid sizes could ever be 1 or 0 (because you never allocate more).
So that's invoking Undefined Behaviour.
Even if you made it so that read_bi5_to_bin is safe for a bin buffer of 1 byte, that would be pretty useless, as one n47::ROW_SIZE is 20 bytes.
In
resolver_iterator iterator_connect = boost::asio::async_connect(mSock, iterator_resolve, yield_r[ec]);
you didn't heed the advice to not iterate resolver results manually. Now you're doing everything double (if you had 5 resolver results, you try connecting (ATTEMPTS-1)*(5+4+3+2+1) times).
Apart from ATTEMPTS-1 looking like a mistake (the loop condition seems wrong or the loop-var initializer should be 0?), this is certainly not what you want/need. However, the many (double) increments/decrements of mIrange2 [sic] make it really hard to predict how many attempts are gonna be made, and how they will be numbered according to the log statements. In fact, I wouldn't be surprised if there was a potential for an infinite loop.
There's mUri and mUrl. This is bound to lead to errors.
SUMMARIZING
I think that #3 from the list is your source of a crash here.
In related news, I created a sample for another recent question that somewhat reminds me of your code: How to read data from Internet using muli-threading with connecting only once?
Maybe it helps you see how to create vastly simpler code to do what you're trying to achieve. By that I mean, you should end up with 20% of the code.
I found that I am passing pointer to unsigned char "data_bin_buffer" as argument to function read_bi5_to_bin ,
which is some how treated as variable ,
which is copied inside function and the copy is assigned the array,
but the passed pointer remained null.
so I changed data_bin_buffer from pointer unsigned char to value unsigned char,
and then took its address by &,
and passed this to read_bi5_to_bin,
and problem solved.
but I have another problem now.should i delete or delete[] &data_bin_buffer???
and if I delete it "not the array delete",what happens to the array on the heap pointed to by &data_bin_buffer???
My son is implementing a server on a Raspberry Pi that allows control of the GPIO pins via a network connection. He has discovered some strange behaviour, which at first seemed like a bug (but see answer below).
First, the OS being used is Raspbian, a version of Debian Linux. He is using the standard system file to control the GPIO ports.
We start with a GPIO pin, e.g. pin 17, in a non-exported state. For example,
echo "17" > /sys/class/gpio/unexport
Now, if the server is asked to turn on pin 17, it does the following:
Opens the /sys/class/gpio/export, writes "17" to it, and closes the export file
Open the /sys/class/gpio/gpio17/direction file for read, examines it to see if it is set as input or output. Closes the file. Then, if necessary, re-opens the file for write and writes "out" to the file, to set the pin as an output pin, and closes the direction file.
At this point, we should be able to open /sys/class/gpio/gpio17/value for write, and write a "1" to it.
However, the permissions on the /sys/class/gpio/gpio17/value file exists but the group permissions is read-only. If we put in a "sleep" in order to wait for a fraction of a second, the permissions change so the group permission has write permissions.
I would have expected that the OS should not return from the write to the direction file until it had set the permissions on the value file correctly.
Why is this happening? It seems like a bug. Where should I report this (with more detail...)? See answer below.
What follows are the relevant bits of code. The code has been edited and paraphrased a bit, but it is essentially what is being used. (Keep in mind it's the code of a grade 12 student trying to learn C++ and Unix concepts):
class GpioFileOut
{
private:
const string m_fName;
fstream m_fs;
public:
GpioFileOut(const string& sName)
: m_fName(("/sys/class/gpio/" + sName).c_str())
{
m_fs.open(m_fName.c_str());
if (m_fs.fail())
{
cout<<"ERROR: attempted to open " << m_fName << " but failed" << endl << endl;
}
else
{
cout << m_fName << " opened" << endl;
}
}
~GpioFileOut()
{
m_fs.close();
cout << m_fName << " closed" << endl << endl;
}
void reOpen()
{
m_fs.close();
m_fs.open(m_fName);
if (m_fs.fail())
{
cout<<"ERROR: attempted to re-open " << m_fName << " but failed" << endl << endl;
}
else
{
cout << m_fName << " re-opened" << endl;
}
}
GpioFileOut& operator<<(const string &s)
{
m_fs << s << endl;
cout << s << " sent to " << m_fName << endl;
return *this;
}
GpioFileOut& operator<<(int n)
{
return *this << to_string(n); //ostringstream
}
bool fail()
{
return m_fs.fail();
}
};
class GpioFileIn
{
private:
ifstream m_fs;
string m_fName;
public:
GpioFileIn(const string& sName)
: m_fs( ("/sys/class/gpio/" + sName).c_str())
, m_fName(("/sys/class/gpio/" + sName).c_str())
{
if (m_fs <= 0 || m_fs.fail())
{
cout<<"ERROR: attempted to open " << m_fName << " but failed" << endl;
}
else
{
cout << m_fName << " opened" << endl;
}
}
~GpioFileIn()
{
m_fs.close();
cout << m_fName << " closed" << endl << endl;
}
void reOpen()
{
m_fs.close();
m_fs.open(m_fName);
if (m_fs <= 0 || m_fs.fail())
{
cout<<"ERROR: attempted to re-open " << m_fName << " but failed" << endl;
}
else
{
cout << m_fName << " re-opened" << endl;
}
}
GpioFileIn& operator>>(string &s)
{
m_fs >> s;
cout << s << " read from " << m_fName << endl;
return *this;
}
bool fail()
{
return m_fs.fail();
}
};
class Gpio
{
public:
static const bool OUT = true;
static const bool IN = false;
static const bool ON = true;
static const bool OFF = false;
static bool setPinDirection(const int pinId, const bool direction)
{
GpioFileOut dirFOut(("gpio" + to_string(pinId) + "/direction").c_str());
if (dirFOut.fail())
{
if (!openPin(pinId))
{
cout << "ERROR! Pin direction not set: Failed to export pin" << endl;
return false;
}
dirFOut.reOpen();
}
dirFOut << (direction == OUT ? "out" : "in");
}
static bool setPinValue(const int pinId, const bool pinValue)
{
string s;
{
GpioFileIn dirFIn(("gpio" + to_string(pinId) + "/direction").c_str());
if (dirFIn.fail())
{
if (!openPin(pinId))
{
cout << "ERROR! Pin not set: Failed to export pin"<<endl;
return false;
}
dirFIn.reOpen();
}
dirFIn >> s;
}
if (strncmp(s.c_str(), "out", 3) == 0)
{
struct stat _stat;
int nTries = 0;
string fname("/sys/class/gpio/gpio"+to_string(pinId)+"/value");
for(;;)
{
if (stat(fname.c_str(), &_stat) == 0)
{
cout << _stat.st_mode << endl;
if (_stat.st_mode & 020 )
break;
}
else
{
cout << "stat failed. (Did the pin get exported successfully?)" << endl;
}
cout << "sleeping until value file appears with correct permissions." << endl;
if (++nTries > 10)
{
cout << "giving up!";
return false;
}
usleep(100*1000);
};
GpioFileOut(("gpio" + to_string(pinId) + "/value").c_str()) << pinValue;
return true;
}
return false;
}
static bool openPin(const int pinId)
{
GpioFileOut fOut("export");
if (fOut.fail())
return false;
fOut << to_string(pinId);
return true;
}
}
int main()
{
Gpio::openPin(17);
Gpio::setPinDirection(17, Gpio::OUT)
Gpio::setPinValue(17, Gpio::ON);
}
The key point is this: without the for(;;) loop that stat's the file, the execution fails, and we can see the permissions change on the file within 100ms.
From a kernel perspective, the 'value' files for each GPIO pin that has been exported are created with mode 0644 and ownership root:root. The kernel does not do anything to change this when you write to the 'direction' file.
The behavior you are describing is due to the operation of the systemd udev service. This service listens for events from the kernel about changes in device state, and applies rules accordingly.
When I tested on my own Pi, I did not experience the behavior you described - the gpio files in /sys are all owned by root:root and have mode 0644, and did not change regardless of direction. However I am running Pidora, and I could not find any udev rules in my system relating to this. I am assuming that Raspbian (or maybe some package you have added to your system) has added such rules.
I did find this thread where some suggested rules are mentioned. In particular this rule which would have the effect you describe:
SUBSYSTEM=="gpio*", PROGRAM="/bin/sh -c 'chown -R root:gpio /sys/class/gpio; chmod -R 770 /sys/class/gpio; chown -R root:gpio /sys/devices/virtual/gpio; chmod -R 770 /sys/devices/virtual/gpio'"
You can search in /lib/udev/rules.d, /usr/lib/udev/rules.d and /etc/udev/rules.d for any files containing the text 'gpio' to confirm if you have such rules. By the way, I would be surprised if the change was triggered by changing direction on the pin, more likely by the action of exporting the pin to userspace.
The reason you need to sleep for a while after exporting the device is that until your process sleeps, the systemd service may not get a chance to run and action the rules.
The reason it is done like this, rather than just having the kernel take care of it, is to push policy implementation to userspace in order to provide maximum flexibility without overly complicating the kernel itself.
See: systemd-udevd.service man page and udev man page.