• Welcome to Theos PowerBasic Museum 2017.

Object Lifetime and Scope

Started by Eriq VanBibber, April 07, 2009, 09:57:56 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

Quote
The msgbox is executed twice. as should.

Of course. When you do o = Class "Class1", PB calls c.Release and assigns to it the pointer of the new create class. But the question asked by Eriq was "Could there be times where passing an object reference doesn't trigger addref?", and the answer is when you pass it by reference.

Eriq VanBibber

Ah fun with COM.  It never dies.

I figured it out.
I did in deed have a situation, buried deep, that was a weak reference in which i was forced to call addref on my own.

The problem was (mainly because of my lack of experience with PB directly) that i was also asuming that i needed to call release since i called addref.

When i commented out the release code, all was working fine.

Seems PB is smarter than we thought.  If an object var has a non-null object handle (as in OBJPTR(var) <> 0) then PB will call release when it goes out of scope, regardless of how the object was instantiated.

I suppose i could have also fixed it by calling release AND setting to object to nothing, or even just setting it to nothing, since var=NOTHING calls release (if necessary) and also sets the object pointer to zero.

i ended up just commenting the code to state that although addref was called, PB will call release.

For any that care about the specifics of what i'm doing...i'm working with MAPI profiles and there's a HACK that has to be used with some profiles where you pickup an object that exists only at an offset of a current one.  That's why i have a coded addref.
here's a link to some explanation of the HACK. http://mapispy.com/articles/prprovider/MAPIProfileProvider.htm
Look for the text 'HACK CENTRAL' in the page and you see the C++ version of the hack.

Thanks guys.