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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:19:32+00:00 2026-06-17T17:19:32+00:00

I have a memory issue with a class of mine. The issue occurs when

  • 0

I have a memory issue with a class of mine. The issue occurs when I create an object in a member function of a class. It is about the class below. I removed the member functions because they aren’t necessary:

class User
{
private:
    bool locked;
    bool active;

    std::vector<City> * userCitys;
    UserData userData;
    Credentials credentials;

The problem occurs when I call this function:

int User::addCity(CityData cityData) 
{
    lockUserObject(); //Everything is fine here

    City cityToAdd; //When this object is created, the memory of userCitys will get overridden
    cityToAdd.activate();
    userCitys->push_back(cityToAdd);
    int cityID = userCitys->size() - 1;

    userCitys->at(cityID).editCityData(cityData);

    unlockUserObject();
    return cityID;
}

In the first place I created userCitys on the stack. For test purpose I placed it on the Heap. The address of userCitys get overridden by some data. I can’t find the problem. the City is just a basic class:

Part of the header:

class City
{
private:
    bool active;
    Supplies supplies;
    std::vector<Building> buildings;
    std::vector<Company> companies;
    std::vector<Share> shares;
    std::vector<Troop> troops;
    CityData cityData;

Constructor:

City::City()
{
    active = false; 
}

How is it possible that userCitys get overridden? This all happens on a single Thread so that can’t be a problem. I tried a lot of thing, but I can’t get it to work. What is the best approach to find the problem?

Edit:
Lock function:

void User::lockUserObject()
{
    for( int i = 0; locked ; i++)
    {
        crossSleep(Settings::userLockSleepInterval);

        if( i >= Settings::userLockMaxTimes )
            Error::addError("User lock is over userLockMaxTimes",2);
    }

    locked = true;
}

I call the code here (Test function):

City * addCity(User * user)
{
    Location location;
    location.x = 0;
    location.y = 1;

    CityData citydata;
    citydata.location = location;
    citydata.villagers = 0;
    citydata.cityName = "test city";

    int cityID = user->addCity(citydata); //addCity is called here
    City * city = user->cityAction(cityID);;

    if( city == NULL)
        Error::addError("Could not create a city",2);

    return city;
}

The add user (Test code):

User * addUser()
{
    UserData test;
    test.name = "testtest";
    Credentials testc("testtest",3);

    //Create object user
    int userID = UserControle::addUser(test,testc);
    User * user = UserControle::UserAction(userID);

    if( user == NULL)
        Error::addError("Could not create a user",2);

    return user;
}

My test function:

void testCode()
{
    User * user = addUser();
    City * city = addCity(user);
}

This function in called in main:

int main()
{
    testCode();
    return 0;
}

Here are UserAction and addUser in UserControle:

int UserControle::addUser(UserData userdata, Credentials credentials)
{
    int insertID = -1;
    for( int i = 0; i < (int)UserControle::users.size(); i++)
    {
        if( !UserControle::users.at(i).isActive() )
        {
            insertID = i;
            break;
        }   
    }

    User userToInsert(userdata,credentials);

    if( insertID != -1 )
    {
        UserControle::users.insert( UserControle::users.begin() + insertID,userToInsert);
        return insertID;
    }
    else
    {
        UserControle::users.push_back(userToInsert);
        return UserControle::users.size() - 1;
    }
}

User* UserControle::UserAction(int userID) //check all indexes if greater then 0!
{
    if( (int)UserControle::users.size() <= userID )
    {
        Error::addError("UserAction is out of range",3);
        return NULL;
    }

    if( !UserControle::users.at(userID).isActive())
    {
        Error::addError("UserAction, the user is not active.",3);
        return NULL;
    }

    return &UserControle::users[userID];
}
  • 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-17T17:19:33+00:00Added an answer on June 17, 2026 at 5:19 pm

    There’s a few things you could try:

    • Remove code until the fault goes away. In other words, distill a minimal example from your code. I guess you’ll then see the error yourself, otherwise post that small example program here and others will.
    • Don’t use raw pointers. The question with those is always who owns what they point to. Use smart pointers instead, e.g. unique_ptr (C++11) or auto_ptr (C++98) for exclusive ownership.
    • If you have pointer members like “userCities”, you need to think about what happens when copying instances of that class (you already wrote a proper destructor, or?). So, either prevent copying (make copy-constructor and assignment operator private and without implementing it) or implement them in a way that the vectors are properly cloned and not shared between different instances.
    • Don’t use C-style casts. If those are necessary to get anything through the compiler, the code is probably broken.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing memory leak issue with very simple code.I have class 'TestClass' @interface
This has been killing me.. Because its a memory management issue... I have a
I have an issue with a memory leak. I have a base-class pointer. From
Here is my issue. I have a class to create timed events. It takes
I have having some sort of memory or winsock issue that only occurs when
I have a memory issue with mongoengine (in python). Let's say I have a
I have problem with my widget related to performance and memory: Issue : My
Anyone have a idea about solve memory leak issues i have found one memory
The issue: I have a UINavigationController as as subview of UIWindow, a rootViewController class
I have the following MyType::Is_Inst () function which is throwing an invalid memory access

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.