Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1072055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:47:59+00:00 2026-05-16T20:47:59+00:00

Consider the following python ctypes – c++ binding: // C++ class A { public:

  • 0

Consider the following python ctypes – c++ binding:

// C++
class A
{
public:
    void someFunc();
};

A* A_new() { return new A(); }
void A_someFunc(A* obj) { obj->someFunc(); }
void A_destruct(A* obj) { delete obj; }

# python
from ctypes import cdll

libA = cdll.LoadLibrary(some_path)

class A:
    def __init__(self):
        self.obj = libA.A_new()

    def some_func(self):
        libA.A_someFunc(self.obj)

What’s the best way to delete the c++ object, when the python object is not needed any more.

[edit]
I added the delete function that was suggested, however the problem remains by whom and when the function is called. It should be as convenient as possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:48:00+00:00Added an answer on May 16, 2026 at 8:48 pm

    You could implement the __del__ method, which calls a destructor function you would have to define:

    C++

    class A
    {
    public:
        void someFunc();
    };
    
    A* A_new() { return new A(); }
    void delete_A(A* obj) { delete obj; }
    void A_someFunc(A* obj) { obj->someFunc(); }
    

    Python

    from ctypes import cdll
    
    libA = cdll.LoadLibrary(some_path)
    
    class A:
        def __init__(self):
            fun = libA.A_new
            fun.argtypes = []
            fun.restype = ctypes.c_void_p
            self.obj = fun()
    
        def __del__(self):
            fun = libA.delete_A
            fun.argtypes = [ctypes.c_void_p]
            fun.restype = None
            fun(self.obj)
    
        def some_func(self):
            fun = libA.A_someFunc
            fun.argtypes = [ctypes.c_void_p]
            fun.restype = None
            fun(self.obj)
    

    Also note that you left out the self parameter on the __init__ method. Futhermore, you have to specify the return type/argument type explicitly, because ctypes defaults to 32 bit integers, while a pointer is likely 64 bits on modern systems.

    Some think __del__ is evil. As an alternative, you can use with syntax:

    class A:
        def __init__(self):
            fun = libA.A_new
            fun.argtypes = []
            fun.restype = ctypes.c_void_p
            self.obj = fun()
    
        def __enter__(self):
            return self
    
        def __exit__(self, exc_type, exc_val, exc_tb):
            fun = libA.delete_A
            fun.argtypes = [ctypes.c_void_p]
            fun.restype = None
            fun(self.obj)
    
        def some_func(self):
            fun = libA.A_someFunc
            fun.argtypes = [ctypes.c_void_p]
            fun.restype = None
            fun(self.obj)
    
    with A() as a:
        # Do some work
        a.some_func()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code in Python: class A(object): CLASS_ATTRIBUTE = 42 def f(self): return
Consider following 2 programs giving same error First calss: public class Testing { Testing
consider following: 1st APPROACH: public void f3() { f2(); f1(); } and this ...
consider the following python code: import gtk class MainWindow(): def __init__(self): self.window = gtk.Window()
Consider the following Python (3.x) code: class Foo(object): def bar(self): pass foo = Foo()
consider the following python code class Base(object): def __init__(self): self.foo = 5 class Derived(Base):
Consider the following example: #include Python.h #include <boost/python.hpp> #include <boost/shared_ptr.hpp> class A {}; class
Let's consider the following table models for sqlalchemy in python. class Worker(Base): id Column(Integer,
Consider the following python class: class Event(Document): name = StringField() time = DateField() location
Consider the following example to understand better my question: public class ClassName { public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.