Posted by
Toyin Akin on
URL: http://quantlib.414.s1.nabble.com/eval-date-problem-solved-tp1121p1127.html
Hi Luigi,
Okay, so you are saying that Dispose() really plays with smart_pointers
rather than the actual data,
But it doesn't explain the fact that Dispose() needs to be called and only
in certain cases.
According to Juraj, this was needed in this case but not when he swaped
SwapHelper for BondHelper.
Dispose() should not really be called unless you are closing non .NET
resources. Resources not under the control of .NET such as files opened by
non .NET code and you need to close these ASAP, you don't have time to wait
for the garbage collector to act.
One may state the fact that the resource in this case are C++ objects,
however if as you state these are resources to smart pointers, then I don't
think this qualifies. Regular .NET objects fall under this category of
reference counted objects.
I cannot believe not deleting a few memory pointers (in this particular
example) will cause the slow down in computation here. Within the loop, if
.NET delays the deletion of the pointers until after the loop, then in
theory you would have 4*30 (roughly within this example) pointers (memory
addresses) left to be deleted at the end of the loop. But these pointers
only occupy addresses and not actual data (as you stated). This is not a lot
of memory.
And this surely cannot be the reason why the computation speed is affected.
Calling Dispose() in this manner is not natural. You have to call Dispose()
not to delete the object, but to decrement the reference count on the object
explicitly. What happens if you call Dispose() but you have not added the
object to any collection? Will the object be deleted?
It's almost like going back to the old days of when you have to call delete
yourself on newed objects but the twist in this case is that you may be
either deleting the object within the C++ layer or decrementing the
reference count.
You really need to carefully construct your program because this is not the
natural way of coding .NET applications.
If, as Juraj suggested, it works well with BondHelpers but not with
SwapHelpers, then this suggests to me a possible bug with the way
SwapHelpers are wrapped.
Having said all this, I am not a swig expert (I didn't know about the
Dispose() managing references) and thus there may be other magic at work
here and everything stated may be easily explained by some swig guru...
Best Regards,
Toyin Akin,
CapeTools QuantTools
www.QuantTools.com
>From: Luigi Ballabio <
[hidden email]>
>To: JURAJ HUSKA <
[hidden email]>
>CC: Toyin Akin
><
[hidden email]>,
[hidden email]
>Subject: Re: [Quantlib-users] eval date-problem solved
>Date: Tue, 10 Jul 2007 09:04:22 +0200
>
>
> > On 7/9/07, Toyin Akin <
[hidden email]> wrote:
> >
> > Hi,
> >
> > The C# solution to the problem posed before looks really
> > wierd...
> >
> > If you look at the following snippet...
> >
> > ########################################################
> > for(int i=0;i<30;i++)
> > {
> > Quote quote = new SimpleQuote(
> > Convert.ToDouble(dfData[i]/100) );
> > QuoteHandle quoteHandle = new
> > QuoteHandle( quote );
> > NQuantLib.Period period = new Period(i+1,
> > NQuantLib.TimeUnit.Years );
> >
> >
> > RateHelper swapRateHelper = new
> > SwapRateHelper(
> > Convert.ToDouble(dfData[i]/100),
> > period,
> > 0,
> > calendar,
> > fixedLegFrequency,
> > fixedLegConvention,
> > fixedLegDayCounter,
> > index
> > );
> >
> > instruments.Add(swapRateHelper);
> >
> > //testing only - works!!!
> > swapRateHelper.Dispose();
> > quoteHandle.Dispose ();
> > quote.Dispose();
> > period.Dispose();
> >
> > }
> > index.Dispose();
> > ########################################################
> >
> > A swapRateHelper object is added into the instruments array,
> > then on the next line, it is disposed of. Well the disposed
> > swapRateHelper object still points to the reference stored
> > within the array and in effect, you are also disposing the
> > object within the array (the swapRateHelper object has not
> > been assigned to another object yet).
>
>Not really. In the bindings, the RateHelper hides what in fact is a
>shared_ptr<RateHelper>. When you add the helper to the array, you're
>making a copy of the pointer. When you dispose of the original helper,
>you're deleting the first pointer; but since you've made another copy
>and shared_ptr is reference-counted, the actual RateHelper is still very
>much alive.
>
>Luigi
>
>
>--
>
>Vin: It's like this fellow I knew in El Paso. One day, he just took
>all his clothes off and jumped in a mess of cactus. I asked him that
>same question, "Why?"
>Calvera: And?
>Vin: He said, "It seemed like a good idea at the time."
>-- The Magnificent Seven
>
>
>
>-------------------------------------------------------------------------
>This SF.net email is sponsored by DB2 Express
>Download DB2 Express C - the FREE version of DB2 express and take
>control of your XML. No limits. Just data. Click to get it now.
>
http://sourceforge.net/powerbar/db2/>_______________________________________________
>QuantLib-users mailing list
>
[hidden email]
>
https://lists.sourceforge.net/lists/listinfo/quantlib-users_________________________________________________________________
The next generation of Hotmail is here!
http://www.newhotmail.co.uk/-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users