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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:06:53+00:00 2026-06-09T23:06:53+00:00

I am using a jagged array to hold the map segments (Clusters) in a

  • 0

I am using a jagged array to hold the map segments (Clusters) in a game I am making. The position in the array corresponds to the Cluster’s position in the map. Furthermore, if the inner array (the jagged part) no longer has any Clusters loaded into it (Clusters are removed from the array after a not-used timeout) that element of the outer array is set back to null to keep the map’s memory usage down.

The problem arises seemingly at random when the game tries to get a Cluster from the map:

public Cluster getCluster(int xIndex, int yIndex)
{
    lock (xAxis)
    {
        loadCluster(xIndex, yIndex);
        return xAxis[xIndex][yIndex];
    }
}

public void loadCluster(int xIndex, int yIndex)
{
    lock (xAxis)
    {
        if (xAxis[xIndex] == null)
            xAxis[xIndex] = new Cluster[(int)worldSize.Y];
        if (xAxis[xIndex][yIndex] == null)
            xAxis[xIndex][yIndex] = new Cluster(this, new Vector2(xIndex * 256, yIndex * 256), worldLoader.loadClusterData(xIndex, yIndex));
    }
}

The loadCluster(int, int) method should ensure the Cluster is loaded into the array before the getCluster(int, int) method retrieves it and almost all the time it does, but occasionally loadCluster(int, int) somehow fails to add the Cluster.

It doesn’t seem to happen with any particular cluster, but always happens when the inner array has not been added (however, most of the time it creates the inner arrays with no problems at all).
Also, when Visual Studio catches the ensuing null pointer exception, stepping back and re-calling loadCluster(int, int) always (at least so far) works as normal. Adding extra calls to loadCluster(int, int) in the getCluster(int, int) method also greatly decreases the frequency of this bug.

I honestly have no idea what is causing this rather straightforward function to not work, seemingly at random even. Any help would be much appreciated

EDIT: Other code that edits xAxis[][]

public override void Update(GameTime gameTime)
{
    for (int x = 0; x < worldSize.X; x++)
    {
        if (xAxis[x] == null) continue;
        int loaded = 0;
        for (int y = 0; y < worldSize.Y; y++)
        {
            if (xAxis[x][y] == null)
            {
                continue;
            }
            xAxis[x][y].Update(gameTime);

            if (xAxis[x][y].clusterLoaded)
            {
                loaded++;
            }
            else if (xAxis[x][y].clusterTimer == 0)
            {
                xAxis[x][y] = null;
            }
        }

        if(loaded == 0) xAxis[x] = null;
    }
}

Cluster.clusterLoaded is a bool showing if the Cluster is currently in use.
Cluster.clusterTimer is an int which counts down once clusterLoaded becomes false. It is reset if to its maximum value if clusterLoaded becomes true again. It is decreased by 1 every time Cluster.Update(GameTime) is run while clusterLoaded is false.
loaded is used to count how many Clusters in the current inner array are loaded.

  • 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-09T23:06:55+00:00Added an answer on June 9, 2026 at 11:06 pm

    First thing to change here is to modify the loadCluster method to return the xAxis[xIndex][yIndex] value right from it. Then there will be no need to lock in the getCluster method.
    What about other sections of the code – from the description it seems to be a multi-threading issue. Although you’ve locked on xAxis variable, I don’t know what else you do somewhere else.
    One more point – arrays in the language are really a core functionality, so it’s really unexpected to have a bug there (I mean Microsoft).

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

Sidebar

Related Questions

I want to remove items of a Jagged array using indizes. int[] toRemove; (e.g,
I have a 2-dimensional jagged array (though it's always rectangular), which I initialize using
I'm using jagged arrays and have a bit of a problem (or at least
I'm making a line graph on the iphone using core graphics and instead of
I need to create 2D jagged array. Think of a matrix. The number of
I've created a 2d array of c-strings using : char ** my_array = new
I am developing a 2d game on Android ICS using OpenGL es 2.0 and
I have what is essentially a jagged array of name value pairs - i
Edit: I apologize everybody. I used the term jagged array when I actually meant
How do I get elements of a column from a jagged array as a

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.