Reference count on class object and import()
Hey again, I'm trying to implement a load / unload module functionallity. When I delete the module in the __main__ dict the module's reference count is still > 1. I've been trying to find if I reference it anywhere else in my code but I can't find anything! When I did a check in vc++ debugger found something. When I run this code: object obj = import(name.c_str()); *obj*'s *ob_refcnt* member is 2 when I check in the vc++ debugger. Shouldn't it be 1? After that line, I put it in the __main__ dict like: mMainNamespace[ name.c_str() ] = obj; The reference count shows 4, as expected. When I'm entering my unload() function when I want to remove the module I do like: void unload() { object t = mMainNamespace[ name.c_str() ]; // reference count in t is now 4 ? mMainNamespace[ name.c_str() ].del(); // I delete but it's not unloaded properly because it's still referenced somewhere .. } What does import() do? It must save some reference somewhere?
Of course, I did find another reference that I didn't notice. Python reference works as expected .. hehe! On Mon, Oct 25, 2010 at 7:35 PM, Simon W <simwarg@gmail.com> wrote:
Hey again,
I'm trying to implement a load / unload module functionallity. When I delete the module in the __main__ dict the module's reference count is still
1. I've been trying to find if I reference it anywhere else in my code but I can't find anything! When I did a check in vc++ debugger found something. When I run this code:
object obj = import(name.c_str());
*obj*'s *ob_refcnt* member is 2 when I check in the vc++ debugger. Shouldn't it be 1?
After that line, I put it in the __main__ dict like:
mMainNamespace[ name.c_str() ] = obj;
The reference count shows 4, as expected.
When I'm entering my unload() function when I want to remove the module I do like: void unload() { object t = mMainNamespace[ name.c_str() ]; // reference count in t is now 4 ?
mMainNamespace[ name.c_str() ].del(); // I delete but it's not unloaded properly because it's still referenced somewhere .. }
What does import() do? It must save some reference somewhere?
Alright, it was a couple of days ago since I last had a look on this and it turns out that issue described in my first mail still persists. Although I had a typo. Swap 4 with 3 on the reference count. Also, when Im using PyImport_Import() directly it also shows 2 reference counts? On Tue, Oct 26, 2010 at 11:29 PM, Simon W <simwarg@gmail.com> wrote:
Of course, I did find another reference that I didn't notice. Python reference works as expected .. hehe!
On Mon, Oct 25, 2010 at 7:35 PM, Simon W <simwarg@gmail.com> wrote:
Hey again,
I'm trying to implement a load / unload module functionallity. When I delete the module in the __main__ dict the module's reference count is still
1. I've been trying to find if I reference it anywhere else in my code but I can't find anything! When I did a check in vc++ debugger found something. When I run this code:
object obj = import(name.c_str());
*obj*'s *ob_refcnt* member is 2 when I check in the vc++ debugger. Shouldn't it be 1?
After that line, I put it in the __main__ dict like:
mMainNamespace[ name.c_str() ] = obj;
The reference count shows 4, as expected.
When I'm entering my unload() function when I want to remove the module I do like: void unload() { object t = mMainNamespace[ name.c_str() ]; // reference count in t is now 4 ?
mMainNamespace[ name.c_str() ].del(); // I delete but it's not unloaded properly because it's still referenced somewhere .. }
What does import() do? It must save some reference somewhere?
On 11/02/2010 11:04 AM, Simon W wrote:
Alright, it was a couple of days ago since I last had a look on this and it turns out that issue described in my first mail still persists. Although I had a typo. Swap 4 with 3 on the reference count. Also, when Im using PyImport_Import() directly it also shows 2 reference counts?
Python stores a global list of loaded modules, as a cache to speed up future imports. You can look them up in sys.modules, though I don't think trying to delete things from that list is allowed. To my knowledge, there is no way to unload a Python module, though one can of course force a reload. Jim
participants (2)
-
Jim Bosch -
Simon W