Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9278605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:25:06+00:00 2026-06-18T17:25:06+00:00

I am getting unhandled exception and access violation reading location error. But sometimes it

  • 0

I am getting unhandled exception and access violation reading location error. But sometimes it is executing perfectly. Am i passing vector and vector iterators properly? What might be the possible cause of error.

struct DataStructure
{
    MapSmoother *m1;
    std::vector<Vertex *> v1;
    std::vector<Vertex *>::iterator vit_f;
    std::vector<Vertex *>::iterator vit_e;
    DataStructure() {
        m1 = NULL;
        v1;
        vit_f;
        vit_e;
    }
};
DWORD WINAPI thread_fun(void* p)
{
        DataStructure *input = (DataStructure*)p;
        while ( input->vit_f != input->vit_e ) {
            Vertex *v = *((input->vit_f)++);
            (*(input->m1)).relax(v);
        }
        return 0;
}
int main()
{
    //Read and encode color to Mesh
    // msmoother is an object
    DataStructure* input = new DataStructure();
    input->m1 = &msmoother;
    for(int i = 0; i < 7; ++i) {
        for(int color = 1; color <= k; color++) {
            std::vector<Vertex *> verList;
          //all the vertices with the same color index will be stored in verList vector
            input->v1 = verList;    //Copied to structure
            std::vector<Vertex *>::iterator vit1 = verList.begin();
            std::vector<Vertex *>::iterator vit2 = verList.end();
            input->vit_f = vit1;
            input->vit_e = vit2;
            HANDLE hThread[50];
            cout << "     Processing for color: " << color << endl;
            for(int j = 0; j < 50; ++j){
                hThread[j] = CreateThread(0,0,(LPTHREAD_START_ROUTINE)&thread_fun,input,0,NULL);
            }
            WaitForMultipleObjects(THREAD_COUNT,hThread,TRUE,INFINITE);
            //clear verList vector
            //Close Handles to all threads
        }
    }
}

Error screenshot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T17:25:08+00:00Added an answer on June 18, 2026 at 5:25 pm

    No, you’re not passing them in correctly.

    This code:

    input->v1 = verList;    //Copied to structure
    

    Makes a copy of the vector, which is intentional by the looks of it. Since the vector contains mere pointers to actual data, you’re just making a copy of a bunch of pointers. However, the followup code:

    std::vector<Vertex *>::iterator vit1 = verList.begin();
    std::vector<Vertex *>::iterator vit2 = verList.end();
    input->vit_f = vit1;
    input->vit_e = vit2;
    

    is where the problem lays. The iterators you’re setting in your input object are, in fact, from the local verList; not from your input->v1 list.

    Try:

    input->vit_f = input->v1.begin();
    input->vit_e = input->v1.end();
    

    Addendum

    Updated for OP to avoid race condition and uneeded vector copy.

    First, redefine DataStructure as:

    struct DataStructure
    {
        MapSmoother *m1;
        std::vector<Vertex *>::iterator first, last
        DataStructure() : m1(NULL) {}
    };
    

    Next, define your thread function as:

    DWORD WINAPI thread_fun(void* p)
    {
        DataStructure *input = static_cast<DataStructure*>(p);
        if (input && input->m1)
        {
            for (std::vector<Vertex *>::iterator it = input->first
                 it != input->last; ++it) 
            {
                 input->m1->relax(*it);
            }
        }
        delete input;
        return 0;
    }
    

    And finally, invoke this as: (inside your loops)

    // all the vertices with the same color index will be stored in verList vector
    std::vector<Vertex *> verList;
    // ... store vector data ...
    
    cout << "     Processing for color: " << color << endl;
    HANDLE hThread[50] = {NULL};
    for(int j = 0; j < _countof(hThread); ++j)
    {
        DataStructure *input= new DataStructure;
        input->first = verList.begin();
        input->last = verList.end();
        input->m1 = &msmoother
        hThread[j] = CreateThread(0,0,(LPTHREAD_START_ROUTINE)&thread_fun,input,0,NULL);
    }
    WaitForMultipleObjects(THREAD_COUNT,hThread,TRUE,INFINITE);
    

    Or something like that. I just hammered this online so I’ve no idea if it even compiles, but should hopefully give a decent enough idea to you. Every thread gets its own pair of iterators into the verList array, and as such concurrent access. note: they cannot modify the vector; only use it. The structure they get the iterators in is allocated by the creator-thread, but owned by the thread itself, which is ultimately responsible for destroying it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting Unhandled exception at 0x00263ACB in Trees.exe: 0xC0000005: Access violation reading location
I am using PhysX, OpenGL and assimp. I am getting 'unhandled exception ..... Access
I am getting the error Format Exception was unhandled at Do While objectReader.Peek <>
I'm trying to access the email address in the DetailsView but getting this error:
I'm getting this unhandled exception error: An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in
Error im getting: An unhandled exception of type 'System.StackOverflowException' occurred in System.Management.dll My callstack:
I keep getting an error of: Unhandled exception at 0x5a6fca58 (msvcr100d.dll) in Gofish.exe: 0xC0000005:
I am getting this error: An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
I'm getting error on second memcpy memcpy(&check_user, &ZZZ, (int)&main - (int)&check_user); Unhandled exception at
I'm getting a (to me) weird run-time access violation error. I have a class,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.