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 493971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:27:16+00:00 2026-05-13T05:27:16+00:00

The following code compile without errors. Basically, the C#2005 Console application calls VC++2005 class

  • 0

The following code compile without errors. Basically, the C#2005 Console application calls VC++2005 class library which in turn calls native VC++6 code. I get the following error when I run the C#2005 application:

“Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.“

What is the cause of this error? And how to go about correcting it?

Edit1: It crashes at the line StdStringWrapper ssw = w.GetNext();

Edit2: I followed the advice of Naveen and used an integer index instead of iterators and there is no more errors now. A big thanks to all who commented as well!

Code Written in C#2005 as Console Application:

class Program
{
    static void Main(string[] args)
    {
        Class1 test= new Class1();
        test.PerformAction();
        test.PerformAction();
        test.PerformAction();
        test.PerformAction();
    }
}

Code Written in VC++2005 as Class Library:

public ref class Class1
{
    public:
        void PerformAction();       
};

void Class1::PerformAction()
{
    DoSomethingClass d;
    StdStringContainer w;
    d.PerformAction(w);

    for(int i=0; i<w.GetSize(); i++)
    {
        StdStringWrapper ssw = w.GetNext();
        std::cout << ssw.CStr() << std::endl;
    }
}

Code Written in VC++6 as Dynamic Link Library:

#ifdef NATIVECODE_EXPORTS
    #define NATIVECODE_API __declspec(dllexport)
#else
    #define NATIVECODE_API __declspec(dllimport)
#endif

class NATIVECODE_API StdStringWrapper
{
    private:
        std::string _s;

    public:     
        StdStringWrapper();
        StdStringWrapper(const char *s);
        void Append(const char *s);
        const char* CStr() const;       
};

StdStringWrapper::StdStringWrapper()
{
}

StdStringWrapper::StdStringWrapper(const char *s)
{
    _s.append(s);
}

void StdStringWrapper::Append(const char *s)
{
    _s.append(s);
}

const char* StdStringWrapper::CStr() const
{
    return _s.c_str();
}

//

class NATIVECODE_API StdStringContainer
{
    private:
        std::vector<StdStringWrapper> _items;
        std::vector<StdStringWrapper>::iterator _it;

    public: 
        void Add(const StdStringWrapper& item);
        int GetSize() const;        
        StdStringWrapper& GetNext();  
};

void StdStringContainer::Add(const StdStringWrapper &item)
{
    _items.insert(_items.end(),item);
}

int StdStringContainer::GetSize() const
{
    return _items.size();
}

StdStringWrapper& StdStringContainer::GetNext()
{
    std::vector<StdStringWrapper>::iterator it = _it;
    _it++;

    return *it;
}

//

class NATIVECODE_API DoSomethingClass
{
    public:
        void PerformAction(StdStringContainer &s);
};

void DoSomethingClass::PerformAction(StdStringContainer &s)
{
    StdStringWrapper w1;
    w1.Append("This is string one");
    s.Add(w1);

    StdStringWrapper w2;
    w2.Append("This is string two");
    s.Add(w2);
}
  • 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-05-13T05:27:17+00:00Added an answer on May 13, 2026 at 5:27 am

    The main problem from my point of view is you are storing an iterator to a vector in your stdStringContainer class. Remember that whenever vector resizes all the existing iterators are invalidated. So whenever you do insert operation into the vector it may be possible that it resizes and your existing iterator becomes invalid. If you try to to dereference it in GetNext() then it will access invalid memory location. For checking whether this really the case try to reserve the initial vector size to some relatively big number so that the resizing doesn’t happen. You can reserve the size using reserve() method, in which case it is guaranteed that the capacity() of the vector is greater than or equal to the reserved value.

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

Sidebar

Related Questions

When I compile with the following code there are no errors: @class RootViewController; //#import
I have the following code which compiles without errors under Linux and Mac OS
Consider the following code: int main() { int *p; ++((int){5}); //compile without err/warning &((int){5});
Consider the following code which shows compile time error : #include <stdio.h> int main(int
I know why the following code doesn't compile: public class Main { public static
The following two code snippets compiles without any errors/warnings but while running it crashes.
The following code will cause compile errors in g++4.4: // File test.cpp namespace A
The following code does not compile: public class GenericsTest { public static void main(String[]
When I compile following code in Eclipse - there are no any errors. There
The following code executes without error and prints In some , which means that

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.