#Returned variable isn't getting set properly

42 messages · Page 1 of 1 (latest)

sharp helm
#

I removed this text from the cpp help chat and posted it here so it doesn't disapear. The issue is that the retHandle variable doesn't get updated inside the GetNewSlot() method. I can see inside the GetDescriptorHandles() function that the debugger jumps to the return-statement upon entry, but then it jumps back up to the start of the method and runs it. I can also see inside the SetHeapIndex() method that the member variable "heapIndex" is updated to the input value "slotIndex". But once the method exits the objects member variables get reset to garbage values. What have I missed here?

    {
        std::vector<DescriptorHandle> handles;
        for (int i = 0; i < _numHandles; i++)
            handles.emplace_back(GetNewSlot());

        return handles;
    }
   
     DescriptorHandle HiddenDescriptorHeap::GetNewSlot()
    {
        DescriptorHandle retHandle;
        int slotIndex = descriptorSlots.LendKey();

        D3D12_CPU_DESCRIPTOR_HANDLE handleCPUAddress = handle.GetCPUHandle();
        handleCPUAddress.ptr += (SIZE_T)(slotIndex * descriptorSize);

        retHandle.SetHeapIndex(slotIndex);
        retHandle.SetHandle(handleCPUAddress);
        return retHandle;
    }


    void DescriptorHandle::SetHeapIndex(int _index) { heapIndex = _index; }
lean flameBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

clever pelican
#

I can see inside the GetDescriptorHandles() function that the debugger jumps to the return-statement upon entry, but then it jumps back up to the start of the method and runs it.
Most likely just a glitch in the debugger you should ignore.
As for the rest ... can't tell. Maybe you're calling the functions on an invalid HiddenDescriptorHeap or something. Reproducing the error on some online compiler would help. This looks like WinAPI stuff which won't be available there, but you can probably provide your own functions that just return some fixed number or something, the specific handle probably doesn't matter for the error.

sharp helm
#

Thanks

lean flameBOT
#

@sharp helm Has your question been resolved? If so, run !solved :)

sharp helm
#

.buff

#

@clever pelican I've simulated it using https://www.onlinegdb.com/online_c++_compiler but then it works... I'm so stuck lmao

clever pelican
#

Wrong link?

sharp helm
#

no. that is the site

#

but i can send the simulation

#
#include <vector>
#include <set>
using namespace std;

class UniqueIntList
{
private:
    std::set<int> keys;
    int maxKeys = 0;
    int incStep = 100;
public:

    /* Initializes the UniqueIntList object.
    @param _startNumKeys The number of unique integers for the UniqueIntList to start with. Goes from the range 0...._startNumKeys.
    @param _incStep The number of unique integers to increase with whenever all the current keys are already lent.
    */
    UniqueIntList(int _startNumKeys = 100, int _incStep = 100)
        :maxKeys(_startNumKeys), incStep(_incStep)
    {
        for (int i = 0; i < maxKeys; i++)
        {
            keys.emplace(i);
        }
    }

    /* Initializes the UniqueIntList object.
    @param _startNumKeys The number of unique integers for the UniqueIntList to start with. Goes from the range 0...._startNumKeys.
    @param _incStep The number of unique integers to increase with whenever all the current keys are already lent.
    @return void
    */
    void Init(int _startNumKeys = 100, int _incStep = 100)
    {
        maxKeys = _startNumKeys;
        incStep = _incStep;

        for (int i = 0; i < maxKeys; i++)
        {
            keys.emplace(i);
        }
    }

    /* Gets a unique integer value that preferably should be given back using UniqueIntList::ReturnKey() later.
    @return int The unique key.
    */
    int LendKey()
    {
        if (keys.size() == 0)
        {
            for (int i = maxKeys; i < maxKeys + incStep; i++)
            {
                keys.emplace(i);
            }
        }

        return keys.extract(keys.begin()).value();
    }
};


class DescriptorHandle
{
private:
public:
    float cpuHandle;
    int heapIndex;
    float gpuHandle;
    DescriptorHandle(const float& _cpuHandle, int _heapIndex)
    {
        cpuHandle = _cpuHandle;
        gpuHandle = -1;
        heapIndex = _heapIndex;
    }
};

#
 
class HiddenDescriptorHeap
{
    private:
    UniqueIntList list;
    public:
    void Init(int _numDescriptors)
    {
        list.Init(_numDescriptors);
    }
    
    DescriptorHandle GetNewSlot()
    {
        //DescriptorHandle retHandle;
        int slotIndex = list.LendKey();

        float handleCPUAddress = 12;

        /*retHandle.SetHeapIndex(slotIndex);
        retHandle.SetHandle(handleCPUAddress);*/
        return DescriptorHandle(handleCPUAddress, slotIndex);
    }

    std::vector<DescriptorHandle> GetDescriptorHandles(int _numHandles)
    {
        std::vector<DescriptorHandle> handles;
        for (int i = 0; i < _numHandles; i++)
            handles.emplace_back(GetNewSlot());

        return handles;
    }
};

int main()
{
    HiddenDescriptorHeap heap;
    heap.Init(100);
    std::vector<DescriptorHandle>handles = heap.GetDescriptorHandles(3);

    return 0;
}
clever pelican
#

I'm not sure what you mean by "but then it works... I'm so stuck". You'd think it working would make everything fine.

sharp helm
#

It works in the simulated code using the online compiler

#

but not in visual studio xd

clever pelican
#

Ah, the problem is that you couldn't reproduce the error.

sharp helm
#

yeah exactly. sry...

clever pelican
#

Well, that usually means the error is not in that code, so looking through it probably won't help.

sharp helm
#

Makes sense. But what could cause it then :/

#

I've tried rebuilding etc aswell

clever pelican
#

Some sites let you use msvc, maybe that'll do something. Alternatively it's a D3D12 problem.

sharp helm
#

Hm. Would suprise me if it is thought. Since the heapIndex doesn't have to do with D3D12.

#

its just an integer within my struct Descriptorhandle

#

in the simulated code i did change gpuHandle and cpuHandle to float types thought from d3d12 specific types since i didnt link d3d12.

#

but that "shouldnt" matter

#

@clever pelican I'm so done... I only did "Rebuild Solution" before. That didn't work. But just now I did "Clean Solution" and then "Build Solution" and it solved the issue... I thought "Rebuild Solution" did exactly the same as "Clean Solution" and "Build Solution" did together.

#

Nevermind... Now only heapIndex gets updated. lol

clever pelican
#

Build system issues are super annoying. You'd think build systems would know when to recompile what since that is their whole purpose, but sadly they don't.

sharp helm
#

Here handles has 0 elements. But then the returned one has 3 lol.

clever pelican
#

Looks like your setup is wonky. Not sure what to do about that. You could try another IDE or something.

#

Maybe there is an update available that fixes it.

sharp helm
#

Man... I just realised I might have forgot to initialize the correct list... The debugger didnt show that thought. Gonna test a thing...

#

Yeah man... Sorry for wasting your time... The debugger DID troll me thought. But sorry... works now.

#

@clever pelican

#

if the debugger actually showed the correct states of the variables I wouldve solved it instantly.

#

So my std::set had 0 elements. But the thing is that since it had 0, my function resize it and created elements. So that shouldve worked anyways. But oh well

#

!solved

lean flameBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

sharp helm
#

I think I found the problem. The heap wasnt initiated so when I called the GetCPUDescriptorHandleForHeapStart() it returned the value for the heap that wasnt initialized. that mightve caused UB or something similar, what do you think? @clever pelican

clever pelican
#

I have no idea. Understanding the somewhat complex issue from this screenshot is too difficult for me.