Detecting QStack overflow
-
Hello,
According to the parent class, QVector, there's a limit of 2GB but that's not a safe guess. So I'd like to detect when there's no more available memory for QStack::push().
What's the right way of doing this?
And if you ask...simulated recursive function. The "tree of calls" size cannot be estimated and can be HUGE. So I need a way of avoiding creating new "branches" and forcing backtrack.
Thanks for your comments.
-
Hi,
Based on the documentation, the worst case scenario is: (2^31-32)/sizeof(YourStoredClass).
However, if you have that kind of limit consideration you might want to implement some known safe values that allows your application to recover in case of spike or stop gracefully if there's an overload.
-
Hi,
Based on the documentation, the worst case scenario is: (2^31-32)/sizeof(YourStoredClass).
However, if you have that kind of limit consideration you might want to implement some known safe values that allows your application to recover in case of spike or stop gracefully if there's an overload.
-
It depends on what you store and what could happen that fills your queue faster than it can process.
-
@Alvein said in Detecting QStack overflow:
Such as?
You want to tell us that you've more than 8GB (*) of contiguous memory available and also use this?
If you really want more, use std::vector or wait for Qt6
(*): 2^31 * 4 bytes = 8589934592 ~ 8.5GB, assumption that you only want to store an integer in the vector.
-
Not sure if you people get my idea.
It's not that I need more memory.
It's that I want to detect when there's no more available memory for my QStack, to stop the recursive calls at that point.
If QStack::push() returned a bool result, meaning allocation success or something alike, this would be easy.
-
@Alvein said in Detecting QStack overflow:
It's that I want to detect when there's no more available memory for my QStack, to stop the recursive calls at that point.
I would guess it's too late then but: https://en.cppreference.com/w/cpp/memory/new/bad_alloc
-
@Alvein said in Detecting QStack overflow:
It's that I want to detect when there's no more available memory for my QStack, to stop the recursive calls at that point.
I would guess it's too late then but: https://en.cppreference.com/w/cpp/memory/new/bad_alloc
OK. Will try that. Thanks. :)
-
@Alvein said in Detecting QStack overflow:
It's that I want to detect when there's no more available memory for my QStack, to stop the recursive calls at that point.
I would guess it's too late then but: https://en.cppreference.com/w/cpp/memory/new/bad_alloc
@Christian-Ehrlicher Hello. Tried std::bad_alloc but no exception is being thrown.
Can't find a clear way of enabling those (if something needs to be enabled, that is - because it shouldn't, by default, IMHO).
-
Hi
It does throw something
asclass BIG { std::array<int, 100000> test; };
does go to the catch part.Hmm
Win 10, visual studio 32bit does throw bad_allocbut app terminates right after it leaves the scope so i think its too late at this point to continue.
-
Thanks.
Finally, I implemented something like this...
template <class itemType> bool safePush(QStack<itemType> &stkStack,itemType stkItem) { bool bSP=true; try { stkStack.push(stkItem); } catch (std::bad_alloc) { bSP=false; } return bSP; }
...and it works now.
Not sure what was causing the exception ignoring, tho.
-
Thanks.
Finally, I implemented something like this...
template <class itemType> bool safePush(QStack<itemType> &stkStack,itemType stkItem) { bool bSP=true; try { stkStack.push(stkItem); } catch (std::bad_alloc) { bSP=false; } return bSP; }
...and it works now.
Not sure what was causing the exception ignoring, tho.
@Alvein
Just to say: if you are so close to running out of memory that pushing one item to a stack causesstd::bad_alloc
, you may succeed in trapping the error and continuing as you have, but is it not likely that as you continue something else fails disastrously as there is no memory left to allocate? -
@Alvein
Just to say: if you are so close to running out of memory that pushing one item to a stack causesstd::bad_alloc
, you may succeed in trapping the error and continuing as you have, but is it not likely that as you continue something else fails disastrously as there is no memory left to allocate?@JonB Not exactly sure if the "no memory" exception means no memory for a single continuous block. But I reckon I'm just being paranoid. I'm handling the stack myself since originally the call stack was not enough, so I just added the check because it looks good (and for learning something in the middle).
In the end, even if I catch the exception, that's not a dead end, since as it's coded, the process can be restarted from where it was, with an empty stack again.
TBH, by design, not even a restart is required because the process backtracks when the memory is full, freeing private stack space. I'll have to see it running live. But the probability of seeing it reaching the exception is terribly low.
-
@JonB Not exactly sure if the "no memory" exception means no memory for a single continuous block. But I reckon I'm just being paranoid. I'm handling the stack myself since originally the call stack was not enough, so I just added the check because it looks good (and for learning something in the middle).
In the end, even if I catch the exception, that's not a dead end, since as it's coded, the process can be restarted from where it was, with an empty stack again.
TBH, by design, not even a restart is required because the process backtracks when the memory is full, freeing private stack space. I'll have to see it running live. But the probability of seeing it reaching the exception is terribly low.
because the process backtracks when the memory is full, freeing private stack space
Again, just an observation :) I think you might be being too optimistic! You seem to assume two things:
-
Freed blocks will get re-used, at this point, for another allocation. May depend on whether it's an odd or even day of the week ;)
-
The code executed for backtracking does not itself do any allocation, which might fail where you are. There are a lot of things you might call without realizing they do some allocation!
Best of luck.
-
-
because the process backtracks when the memory is full, freeing private stack space
Again, just an observation :) I think you might be being too optimistic! You seem to assume two things:
-
Freed blocks will get re-used, at this point, for another allocation. May depend on whether it's an odd or even day of the week ;)
-
The code executed for backtracking does not itself do any allocation, which might fail where you are. There are a lot of things you might call without realizing they do some allocation!
Best of luck.
Thank you very much for your advice.
- Freed blocks will get re-used, at this point, for another allocation. May depend on whether it's an odd or even day of the week ;)
Yes, I'm aware of that. My intention was simply avoiding my program to crash and tell the users what's happening, letting them to stop the process if they wish.
- The code executed for backtracking does not itself do any allocation, which might fail where you are. There are a lot of things you might call without realizing they do some allocation!
Backtracking, for me, means just popping out items from the private stack. Remember this is about simulating recursion, so such backtracking is also simulated. In the end, there's two options for the program, either using the recently freed memory, or behaving like if there was no more, in case that memory was immediately seized by other things. Each option has been considered and handled by code.
-