Skip to content

Commit

Permalink
Fix memory leaks related to layers and vendor icds
Browse files Browse the repository at this point in the history
fixes: KhronosGroup#157
Signed-off-by: Mateusz Jablonski <[email protected]>
  • Loading branch information
JablonskiMateusz committed Apr 11, 2023
1 parent 617580b commit 731d9ba
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions loader/icd.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ void khrIcdVendorAdd(const char *libraryName)
}
}

void khrIcdVendorCleanup() {
KHRicdVendor* nextVendor = NULL;
while (khrIcdVendors) {
nextVendor = khrIcdVendors->next;
free(khrIcdVendors);
khrIcdVendors = nextVendor;
}
}

#if defined(CL_ENABLE_LAYERS)
void khrIcdLayerAdd(const char *libraryName)
{
Expand Down Expand Up @@ -345,6 +354,15 @@ void khrIcdLayerAdd(const char *libraryName)
free(layer);
}
}

void khrIcdLayerCleanup() {
struct KHRLayer* nextLayer = NULL;
while (khrFirstLayer) {
nextLayer = khrFirstLayer->next;
free(khrFirstLayer);
khrFirstLayer = nextLayer;
}
}
#endif // defined(CL_ENABLE_LAYERS)

// Get next file or dirname given a string list or registry key path.
Expand Down
6 changes: 6 additions & 0 deletions loader/icd.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,18 @@ void khrIcdVendorsEnumerateEnv(void);
// add a vendor's implementation to the list of libraries
void khrIcdVendorAdd(const char *libraryName);

// cleanup a list of vendor icds
void khrIcdVendorCleanup();

// read layers from environment variables
void khrIcdLayersEnumerateEnv(void);

// add a layer to the layer chain
void khrIcdLayerAdd(const char *libraryName);

// cleanup a layer chain
void khrIcdLayerCleanup();

// dynamically load a library. returns NULL on failure
// n.b, this call is OS-specific
void *khrIcdOsLibraryLoad(const char *libraryName);
Expand Down
5 changes: 5 additions & 0 deletions loader/linux/icd_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,8 @@ void khrIcdOsLibraryUnload(void *library)
{
dlclose(library);
}

void __attribute__((destructor)) khrIcdGlobalDestructor() {
khrIcdVendorCleanup();
khrIcdLayerCleanup();
}
8 changes: 8 additions & 0 deletions loader/windows/icd_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,11 @@ void khrIcdOsLibraryUnload(void *library)
{
FreeLibrary( (HMODULE)library);
}

BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
if (fdwReason == DLL_PROCESS_DETACH) {
khrIcdVendorCleanup();
khrIcdLayerCleanup();
}
return TRUE;
}

0 comments on commit 731d9ba

Please sign in to comment.