|
Boost Users : |
Subject: Re: [Boost-users] why is make_shared using so much of my stack....?
From: Jeff Flinn (TriumphSprint2000_at_[hidden])
Date: 2011-02-05 21:47:08
Michael Xu wrote:
> Hi everyone,
>
> I've been investigating our program's memory usage, and one of the spots
> that I keep seeing the stack hitting its limit is when using
> boost::make_shared.
>
> I've written a simple test program, that completes fine when I use
> new... but fails with a stack overflow when I use boost::make_shared
> ========================
> #include <iostream>
> #include <boost/shared_ptr.hpp>
> #include <boost/make_shared.hpp>
> using namespace std;
> struct arr
> {
> char arg[1024*1024];
> };
>
> struct test
> {
> struct arr array[1024];
> };
>
> int main()
> {
> test * myTest = new test(); //complete!
> if (!myTest)
> cout << "new alloc failed";
> boost::shared_ptr<test> testPtr = boost::make_shared<test>(); //fails
> with stack overflow!
> if (!testPtr)
> {
> cout << "alloc failed;";
> }
> }
> ===========================
>
> If you run this with a reasonable thread stack of 8 megabytes, it fails
> when using boost::make_shared.
>
> Here is the stack trace.....
>
> #0 boost::shared_ptr<test>::shared_ptr<test,
> boost::detail::sp_ms_deleter<test> >(test*,
> boost::detail::sp_ms_deleter<test>) ()
> #1 boost::shared_ptr<test> boost::make_shared<test>() ()
> #2 main ()
>
>
> Is there a reason this happens?
Do you mean heap rather than stack?
You are first allocating with new:
1024 * 1024 * 1024 = 1,073,741,824 = 1GB of contiguous memory.
Then your attempting to allocate another 1GB with make_shared. This has
a good chance of failing on 32bit windows systems. You don't mention the
OS.
I would expect that make_shared is throwing an exception like
std::bad_alloc which you are not cathing.
Jeff
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net