Overflows the stack

I have a function that creates many entities at once, but it overflows the stack. What is the best way for me to do this?

Unhandled exception at 0x00007FFD5AA4DB45 (UnrealEditor-Sanes.dll) in UnrealEditor.exe: The stack cookie instrumentation code encountered a stack-related buffer overflow. 
  1. My function:
void ASans::LabirintKostei(int vhodiVihodi[], int dlina, FVector skorostLabirinta)
{
    for (int i = 0; i < dlina; i++)
    {
        if (vhodiVihodi[i] < 1)
            vhodiVihodi[i] = 1;
        if (vhodiVihodi[i] > 9)
            vhodiVihodi[i] = 9;
    }

    float poyav = GetActorLocation().Y;
    
    for (int i = 0; i < dlina; i++)
    {
        if (vhodiVihodi[i] > vhodiVihodi[i + 1])
            for (int ii = vhodiVihodi[i]; ii > vhodiVihodi[i + 1]; ii--)
            {
                int stena[10]{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
                stena[ii] = 0;
                stena[ii+1] = 0;
                stena[ii-1] = 0;
                StenaKostei(4, stena, skorostLabirinta, poyav);
                poyav += 50;
                UE_LOG(LogTemp, Warning, TEXT("mass %d yach %d"), i, ii);
            }
        if (vhodiVihodi[i] < vhodiVihodi[i + 1])
            for (int ii = vhodiVihodi[i]; ii < vhodiVihodi[i + 1]; ii++)
            {
                int stena[10]{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
                stena[ii] = 0;
                stena[ii + 1] = 0;
                stena[ii - 1] = 0;
                StenaKostei(4, stena, skorostLabirinta, poyav);
                poyav += 50;
                UE_LOG(LogTemp, Warning, TEXT("mass %d yach %d"), i, ii);
            }
    }
}

2 Function

void ASans::StenaKostei(int dlinaVolni, int raspologKost[], FVector skorostVolni, float y)
{
    int schet = 0;
    bool spawnit;
    for (int i = 20; i <= 1370; i += 50)
    {
        if (schet%3 == 0)
            spawnit = raspologKost[schet/3] != 0;
        if (spawnit)
        {
            AKost* kost = SozdatKost(FVector(i, y, -100), dlinaVolni, skorostVolni, FRotator(0.0f, 0.0f, 0.0f));
            kost->ZadatTochkyPoyavlenia(y-100);
            if (y > 3450)
                kost->IzmenitGraniciY(50,y+50);
        }
        schet++;
    }
}

You are likely reading or writing something out of bound in the stack.

I suspect it could be the “stena” array of 10 elements. Can you check that “ii” variable never goes over 8 and never goes below 1 ? (do a print of the variable).

As you are doing stena[ii + 1] and stena[ii -1], and the array size is 10, “ii” MUST be in 1 and 8, otherwise… buffer overflow on the stack.

Also check that “i” variable value is always below the size of vhodiVihodi array - 2 (as you are checking for vhodiVihodi[i + 1])

No, the only problem is that the stack overflows due to the large number of created actors and if you reduce their number, then it all works fine. I would like to know how to get around this limitation

Unhandled exception at 0x00007FFD5AA4DB45 (UnrealEditor-Sanes.dll) in UnrealEditor.exe: The stack cookie instrumentation code encountered a stack-related buffer overflow. 

According to this reported error, it definitively looks like to be a stack buffer overflow (Stack buffer overflow - Wikipedia), where basically some code is writing in the stack (or worse, memory) where it should not. This is generally caused by improper index usage in arrays allocated in the stack (hence my proposal to look on that side).

This is very different from a stack overflow (ie. without the “buffer” word: Stack overflow - Wikipedia => compare and contrast the two wikipedia articles), where indeed, the issue is that there is too many things on the stack. This generally happens when there is some uncontrolled recursive call getting too deep. To be sure, check if you have some recursive process in your code.

So according to the error reported, my intuition says to me that the former case is more probable than the latter. I could be wrong of course, but just a hint to help you.

The fact it works with a few objects and not with many objects is not a symptom for which the cause is to be taken for granted. At the moment there is a buffer overflow somewhere, you enter in the “undefined behavior” (UB) world: stuff may or may not work, but the more occurences UB cases happens, the more chance something bad occurs. So the more objects, the more chance for a serious problem to happen.

I wish you good luck with these hard-to-find bugs :slight_smile: but I’m sure you’ll manage to find the issue.