Skip to content

Commit

Permalink
drm_preview: fix unreleased resources and a typo
Browse files Browse the repository at this point in the history
In DrmPreview::findCrtc() a series of drmModeGet*() functions is used. Those functions actually allocate memory that needs to be released with the corresponding drmModeFree*() call. In some cases resources are not freed causing a memory leak which can be confirmed with Valgrind.

Free all the allocated resources before losing the pointer.

Also fix a clear typo.

Signed-off-by: Luca Magrone <[email protected]>
  • Loading branch information
Leuca authored and naushir committed Oct 18, 2023
1 parent f317e33 commit 28b2a4f
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion preview/drm_preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ void DrmPreview::findCrtc()
throw std::runtime_error("drmModeGetResources failed: " + std::string(ERRSTR));

if (res->count_crtcs <= 0)
{
drmModeFreeResources(res);
throw std::runtime_error("drm: no crts");
}

max_image_width_ = res->max_width;
max_image_height_ = res->max_height;
Expand Down Expand Up @@ -114,10 +117,23 @@ void DrmPreview::findCrtc()
LOG(2, "Connector " << con->connector_id << " (crtc " << (crtc ? crtc->crtc_id : 0) << "): type "
<< con->connector_type << ", " << (crtc ? crtc->width : 0) << "x"
<< (crtc ? crtc->height : 0) << (conId_ == (int)con->connector_id ? " (chosen)" : ""));

if (con->encoder_id)
{
drmModeFreeEncoder(enc);
if (enc->crtc_id)
{
drmModeFreeCrtc(crtc);
}
}
drmModeFreeConnector(con);
}

if (!conId_)
{
drmModeFreeResources(res);
throw std::runtime_error("No suitable enabled connector found");
}
}

crtcIdx_ = -1;
Expand Down Expand Up @@ -167,6 +183,9 @@ void DrmPreview::findCrtc()
height_ = crtc->height;
drmModeFreeCrtc(crtc);
}

drmModeFreeConnector(c);
drmModeFreeResources(res);
}

void DrmPreview::findPlane()
Expand All @@ -185,7 +204,7 @@ void DrmPreview::findPlane()
for (i = 0; i < planes->count_planes; ++i)
{
plane = drmModeGetPlane(drmfd_, planes->planes[i]);
if (!planes)
if (!plane)
throw std::runtime_error("drmModeGetPlane failed: " + std::string(ERRSTR));

if (!(plane->possible_crtcs & (1 << crtcIdx_)))
Expand Down

0 comments on commit 28b2a4f

Please sign in to comment.