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

  • Home
  • SEARCH
  • 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 8855269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:05:50+00:00 2026-06-14T14:05:50+00:00

I have written a device driver kext for a hot-plug SCSI device, based somewhat

  • 0

I have written a device driver kext for a hot-plug SCSI device, based somewhat on Wagerlabs code (using a driver-user client-application model) and everything works. The only remaining concern is that the driver appears not to be consistently freed, especially if the application crashes. For example, when I try to unload the kext, even with the device disconnected and the application closed, there are still outstanding instances of the driver and user client (with the driver generally outnumbering the user client).

I have logging in the driver functions like free(), and when I shut down the computer, I can see these being executed, so the instances can obviously still be terminated. What is the “right” way to ensure the driver instance is terminated and freed, even if the host application crashes, terminates improperly or things generally don’t go to plan?

  • 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-06-14T14:05:51+00:00Added an answer on June 14, 2026 at 2:05 pm

    If you’ve got user client class instances when no user client app is running, then you’re definitely retaining the user client instances more often than you’re releasing them. For example, you might be keeping a retained reference to client instances in the main driver class. In your user client class’s stop() method, make sure to remove that client instance from the driver.

    Another thing to watch out for: make sure you call superclass implementations from your overridden versions of the built-in IOService methods such as stop(), free() etc. Not doing so will usually put the IO Kit into an inconsistent state.

    Finally, a useful technique for debugging retain leaks in I/O Kit drivers, is to actually log the retains and releases by overriding the methods with logging versions:

    void MyClass::taggedRetain(const void* tag) const
    {
        OSReportWithBacktrace(
            "MyClass" CLASS_OBJECT_FORMAT_STRING "::taggedRetain(tag=%p)\n", CLASS_OBJECT_FORMAT(this), tag);
        IOService::taggedRetain(tag);
    }
    void MyClass::taggedRelease(const void * tag) const
    {
        OSReportWithBacktrace(
            "MyClass" CLASS_OBJECT_FORMAT_STRING "::taggedRelease(tag=%p)\n", CLASS_OBJECT_FORMAT(this), tag);
        int count = getRetainCount();
        IOService::taggedRelease(tag);
        if (count == 1)
            printf(
                "MyClass::taggedRelease(tag=%p) final done\n", tag);
        else
            printf(
                "MyClass" CLASS_OBJECT_FORMAT_STRING "::taggedRelease(tag=%p) done\n", CLASS_OBJECT_FORMAT(this), tag);
    }
    

    The macros in this code are defined in a header as follows:

    #define CLASS_OBJECT_FORMAT_STRING "[%s@%p:%dx]"
    #define CLASS_OBJECT_FORMAT(obj) myClassName(obj), obj, myRefCount(obj)
    
    inline int myRefCount(const OSObject* obj)
    {
        return obj ? obj->getRetainCount() : 0;
    }
    
    inline const char* myClassName(const OSObject* obj)
    {
        if (!obj) return "(null)";
        return obj->getMetaClass()->getClassName();
    }
    #endif
    

    I should explain that taggedRetain() and taggedRelease() are the actual underlying implementation of retain() and release() – if you override the latter, you won’t see any retains and releases coming from OSCollections, as they use the tagged versions (with a non-null tag).

    The backtrace generated by OSReportWithBacktrace() is unfortunately just a bunch of hex pointers, but you can look those up using gdb.

    In any case, by logging retains and releases for your objects, you can go through all retains and make sure they are matched by a release in the right place. Watch out for cycles!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a device driver for a device but each time I connect
I find the type of device using WURFL.Also i have written plugin to set
I have written a device driver and need to pass an int value to
I have written a PCIe device driver and the read/write functions are not working
I have written a server/client application using sockets in C# for .NET 3.5. I'm
I have written a kernel driver hello_kernel which registers as a character device on
I have written a small code for usb detection for android device but its
I have written this code inside a servlet to delete certain records from three
I have written this code in JavaScript and works perfectly fine when I include
I have written a Linux kernel-module to act as an FPGA driver for a

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.