deleting handle from within c++

Oct 12, 2012 at 9:44 PM

Hi Keith,


 is there a way to delete handle from within the code.

say for instance i create a handle

xll::handle<someobject> myhandle(new someobject);

then can i delete myhandle at a later stage.

or does myhandle and the underlying object gets automatically deleted when myhandle goes out of scope.



Oct 12, 2012 at 10:07 PM

If you follow the convention of always calling functions that return a handle from a single cell, then old handles will be automatically deleted. If you delete a cell containing a handle, the object does not get deleted. If you use a function that takes an argument that is a function returning a handle you get a leak. Every time the function is called you will create a new object that will never be deleted.

C++ object lifetimes in Excel are difficult to manage in general. My implementation is only a few lines of code and doesn't leak too badly if you stick to the conventions. If you really need to garbage collect, close and reopen Excel.

Oct 13, 2012 at 2:22 AM

thank you for clarifying.

is such a design not possible that every time a handle is created (from the time xll is deployed) a container keeps track of it.

in such a case we can call something like delete("all") to delete everything without closing excel.

and also do something like list() to list all the objects present, and then selectively delete any one of them we like.



Oct 13, 2012 at 2:51 PM

Not following you kaushikmatia. A containter is created for each handle type and you can do anything you please with the contents using standard C++. If you are proposing an Excel interface you need to tell me what functions and macros you have in mind. I'm not yet at the level where I can convert wishful thinking into code that executes on a computer, but I'm working on it. :-)

Oct 13, 2012 at 3:28 PM

ha ha..after i read your comment about the leaks it came to my mind and i was just trying to think out loud. this is what i have be mind:

1> from the instance the dll is deployed some handle counter/tracker keeps track of all the handles that are created (this part is internal there is no outside excel interface).

so even in situations like this "  If you use a function that takes an argument that is a function returning a handle " the "handle tracker" is counting and keeping track of the handles. no matter where a handle gets created (whether in c++ code or in excel cell) as long as a handle is created and memory is assigned for it the handle tracker will keep an account of it. 

2> now an excel interface function (say list() ) will have functionality of listing out all the handles that are present/created.

3> another excel interface function (say delete() ) will have the functionality of deleting handles that are listed using the list() function.

i am not sure how easy/hard it is to implement step 1.  If step 1 is possible then step 2 and 3 may be relatively easy to implement.





Oct 13, 2012 at 3:35 PM

Handles are type safe. Each type goes into a different container. How will list() know what types have been created in the spreadsheet? Also, handles from different dlls live in separate memory. How do you handle that?

It is trivial to write a delete(handle) function. You don't need me to do that for you.

Oct 13, 2012 at 4:25 PM

well every time a handle gets created something like

handle<object> h_(new object())

gets called so is it not possible to keep track of the object type too ? also we are only talking about one dll (the xll in this case). but of course  inside xll we can write a code that uses some third party dll. any object that is defined in the third party dll  lives where ? in the memory of xll or in the memory designated to the third party dll.

 i have to admit i do not know enough about these things. i am just giving my layman thought process.






Oct 13, 2012 at 4:32 PM

I'm afraid you will have to just make do with the existing code unless you decide to contribute something that executes on a computer.

Oct 13, 2012 at 4:40 PM

i do not know how to implement this but i have seen this feature in the past. exactly this feature that i am talking about. the dll somehow kept track of all the handles. listed the handles and if required selectively delete. the dll was written in c++ just like the the nice dll that you have. i do not know though how it was achieved.


Oct 13, 2012 at 4:48 PM

The QuantLib and the xlw library have this. Perhaps you should consider using those libraries if you are obssessing over this. You are the only person using the library that seems to think this is a problem so it is pretty low on my priority list.

You are correct that it is possible to do this. Go look at how other people have solved this problem for Excel add-ins. It's harder than you seem to think.

Are we done now?

Oct 13, 2012 at 5:04 PM

sure, sure..i thought you were implying that this feature is not possible and i was trying to say that even though i do not know how to do it i have seen this before, just that nothing more. i was not even implying that you do it in your library (i apologize if you got that impression). you already made available a nice open source and that itself is a big thing. how can anyone ask you to do more.  i was just playing around with your addin when all these thoughts about deleting handles came to my mind and resulted in so many emails. have a good weekend.