[C++-sig] Howto delete python object from C++ code.

Grayyoga grayyoga at gmail.com
Tue Jan 9 10:03:24 CET 2007


On 1/9/07, Stefan Seefeld <seefeld at sympatico.ca> wrote:
> Siarhei Rachytski wrote:
> > Hi
> >
> > I have a python wrapper for a class which implements a Win32 window in
> > a separate thread. When it's created the message loop is initiated
> > from a separate thread, and when loop exits(this means the user
> > pressed the close button and the window should be destroyed), I want
> > to delete a C++ object and its python counterpart. I delete C++ object
> > using boost::python::decref, however the python variable this object
> > has been assigned to still exists, the "print" function shows me smth
> > like this (cgepython.Window object at 0xADDRHERE). But I want it to be
> > deleted too. How could I implement this behaviour?
>
> Python uses garbage collection to reclaim unreferenced objects (for most
> objects that simply means reference counting). Thus, there is no guarantee
> that the object will be deleted at a particular point in time.
> Some other part of the program may still refer to it, and even if not,
> the cleanup may be implemented differently.
>
> Regards,
>                 Stefan
>

Referenceing to the deleted object is an error isn't it? I've checked
it carefully - in the part of the program(at the end of the thread
function) the object is deleted, the destructor is called, resources
are freed, window destroyed, and so forth, so next time somebody will
try to access this object it will probably result in a crash.

Maybe some code will clarify the problem, cause my English isn't very good :)

In python

d = loadDisplayDriver()
w = d.createWindowContext(500, 200, u'test window')
//here the window appears in the separate thread(see thread function
code below),
//we work with it for some time and then close it, the C++ code
deletes object, but...
print w
/// shows something like this
(cgepython.Window object at 0xADDRHERE)
/// but after explicit deletion of the object...
del w
/// ... everything is ok, print w spits with error


C++ code

		DWORD WINAPI ListenWindow(LPVOID Param)
		{
			SWindowInitParam * pParam = (SWindowInitParam*)Param;

			WNDCLASSEX wc;

			if (!GetClassInfoEx(GetModuleHandle(0), L"cge_python_window", &wc))
			{
				wc.cbClsExtra    = 0;
				wc.cbSize		 = sizeof(WNDCLASSEX);
				wc.cbWndExtra    = 0;
				wc.hbrBackground = reinterpret_cast<HBRUSH>(GetStockObject(GRAY_BRUSH));
				wc.hIconSm       = LoadIcon(GetModuleHandle(0), IDI_APPLICATION);
				wc.hCursor       = LoadCursor(GetModuleHandle(0), IDC_ARROW);
				wc.hIcon         = LoadIcon(GetModuleHandle(0), IDI_APPLICATION);
				wc.hInstance     = GetModuleHandle(0);
				wc.lpfnWndProc   = WindowProc;
				wc.lpszClassName = L"cge_python_window";
				wc.lpszMenuName  = NULL;
				wc.style		 = 0;

				if (!RegisterClassEx(&wc)) return 0;
			}


			HWND Window = CreateWindow(	
				L"cge_python_window",
				pParam->m_WindowName.c_str(),
				WS_OVERLAPPEDWINDOW,
				0,
				0,
				pParam->m_Width,
				pParam->m_Height,
				(HWND)0,
				(HMENU)0,
				GetModuleHandle(0),
				(LPVOID)0);

			pParam->m_Window = Window;
			SetEvent(pParam->m_WindowCreated);

			HANDLE WindowContextInitialized = pParam->m_WindowContextInitialized;
			WaitForSingleObject(WindowContextInitialized, INFINITE);
			CloseHandle(WindowContextInitialized);

			CWindow * pWindow =
reinterpret_cast<CWindow*>(GetWindowLong(Window, GWL_USERDATA));

			ShowWindow(Window, SW_SHOW);

			MSG msg;

			while (true)
			{
				int res = GetMessage(&msg, Window, 0, 0);

				if ( (res == 0) || (res== -1) ) break;

				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}

			boost::python::object obj(pWindow);

                        /// when obj goes out of scope the destructor
on CWindow object is called

			return 0;
		}



> --
>
>       ...ich hab' noch einen Koffer in Berlin...
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
>



More information about the Cplusplus-sig mailing list