Skip to content

Commit

Permalink
fix double free due to using cast rather than QueryInterface (#74)
Browse files Browse the repository at this point in the history
* fix: potential double free in README example #19

* fix: potential double free in examples #19

* fix: potential double free in utils #19
  • Loading branch information
mrob95 committed May 1, 2023
1 parent f9d2768 commit c3cc4d7
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 10 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ choco install visualcpp-build-tools
## Usage

```Python
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(
IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
volume = interface.QueryInterface(IAudioEndpointVolume)
volume.GetMute()
volume.GetMasterVolumeLevel()
volume.GetVolumeRange()
Expand Down
4 changes: 1 addition & 3 deletions examples/audio_endpoint_volume_example.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""
Get and set access to master volume example.
"""
from ctypes import POINTER, cast

from comtypes import CLSCTX_ALL

from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
Expand All @@ -11,7 +9,7 @@
def main():
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
volume = interface.QueryInterface(IAudioEndpointVolume)
print("volume.GetMute(): %s" % volume.GetMute())
print("volume.GetMasterVolumeLevel(): %s" % volume.GetMasterVolumeLevel())
print("volume.GetVolumeRange(): (%s, %s, %s)" % volume.GetVolumeRange())
Expand Down
4 changes: 1 addition & 3 deletions examples/volume_callback_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
IAudioEndpointVolumeCallback.OnNotify() example.
The OnNotify() callback method gets called on volume change.
"""
from ctypes import POINTER, cast

from comtypes import CLSCTX_ALL, COMObject

from pycaw.pycaw import (
Expand All @@ -23,7 +21,7 @@ def OnNotify(self, pNotify):
def main():
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
volume = interface.QueryInterface(IAudioEndpointVolume)
callback = AudioEndpointVolumeCallback()
volume.RegisterControlChangeNotify(callback)
for i in range(3):
Expand Down
3 changes: 1 addition & 2 deletions pycaw/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import warnings
from ctypes import POINTER, cast

import comtypes
import psutil
Expand Down Expand Up @@ -49,7 +48,7 @@ def EndpointVolume(self):
iface = self._dev.Activate(
IAudioEndpointVolume._iid_, comtypes.CLSCTX_ALL, None
)
self._volume = cast(iface, POINTER(IAudioEndpointVolume))
self._volume = iface.QueryInterface(IAudioEndpointVolume)
return self._volume


Expand Down

0 comments on commit c3cc4d7

Please sign in to comment.