Skip to content

Commit

Permalink
drivers/video: Update unlink() and video_uninitialize() operation
Browse files Browse the repository at this point in the history
Add unregister_driver() execution to unlink() operation
to delete device file.
Also, add resource cleanup to video_uninitialize() operation
to avoid memory leak without unlink() execution for compatibility.
  • Loading branch information
SPRESENSE committed Mar 8, 2024
1 parent 1883b91 commit 0ca5469
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions drivers/video/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <assert.h>
#include <errno.h>
#include <poll.h>
#include <sys/stat.h>
#include <nuttx/fs/fs.h>

#include <nuttx/kmalloc.h>
#include <nuttx/mutex.h>
Expand Down Expand Up @@ -1015,6 +1017,18 @@ static void cleanup_resources(FAR video_mng_t *vmng)
cleanup_scenes_parameter(vmng);
}

static void cleanup_private_data(FAR struct inode *inode)
{
FAR video_mng_t *priv = inode->i_private;

nxmutex_unlock(&priv->lock_open_num);
nxmutex_destroy(&priv->lock_open_num);
unregister_driver(priv->devpath);
kmm_free(priv->devpath);
kmm_free(priv);
inode->i_private = NULL;
}

static bool is_sem_waited(FAR sem_t *sem)
{
int semcount;
Expand Down Expand Up @@ -1094,11 +1108,7 @@ static int video_close(FAR struct file *filep)
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
if (priv->unlinked)
{
nxmutex_unlock(&priv->lock_open_num);
nxmutex_destroy(&priv->lock_open_num);
kmm_free(priv->devpath);
kmm_free(priv);
inode->i_private = NULL;
cleanup_private_data(inode);
return OK;
}

Expand Down Expand Up @@ -1128,11 +1138,7 @@ static int video_unlink(FAR struct inode *inode)
nxmutex_lock(&priv->lock_open_num);
if (priv->open_num == 0)
{
nxmutex_unlock(&priv->lock_open_num);
nxmutex_destroy(&priv->lock_open_num);
kmm_free(priv->devpath);
kmm_free(priv);
inode->i_private = NULL;
cleanup_private_data(inode);
}
else
{
Expand Down Expand Up @@ -3488,7 +3494,16 @@ int video_register(FAR const char *devpath,

int video_unregister(FAR const char *devpath)
{
return unregister_driver(devpath);
int ret;
struct stat buf;

ret = nx_stat(devpath, &buf, 1);
if (ret == 0)
{
ret = nx_unlink(devpath);
}

return ret;
}

int imgsensor_register(FAR struct imgsensor_s *sensor)
Expand Down

0 comments on commit 0ca5469

Please sign in to comment.