Skip to content

Commit

Permalink
Fixed: Minor memory leak locating a resource by name
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Jan 5, 2012
1 parent f2b31ab commit 9944a36
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 38 deletions.
8 changes: 8 additions & 0 deletions doomsday/engine/api/uri.h
Expand Up @@ -41,6 +41,14 @@ void Uri_Delete(Uri* uri);
void Uri_Clear(Uri* uri);
Uri* Uri_Copy(Uri* uri, const Uri* other);

/**
* Attempt to compose a resolved copy of this Uri. Substitutes known symbolics
* in the possibly templated path. Resulting path is a well-formed, filesys
* compatible path (perhaps base-relative).
*
* @return Resolved path else @c NULL if non-resolvable. Caller should ensure
* to Str_Delete when no longer needed.
*/
ddstring_t* Uri_Resolved(const Uri* uri);

const ddstring_t* Uri_Scheme(const Uri* uri);
Expand Down
64 changes: 34 additions & 30 deletions doomsday/engine/portable/src/sys_reslocator.c
Expand Up @@ -510,7 +510,7 @@ static int findResource(resourceclass_t rclass, const Uri* const* list,

// Ignore incomplete paths.
resolvedPath = Uri_Resolved(searchPath);
if(!resolvedPath || Str_IsEmpty(resolvedPath)) continue;
if(!resolvedPath) continue;

// If this is an absolute path, locate using it.
if(F_IsAbsolute(resolvedPath))
Expand Down Expand Up @@ -1026,43 +1026,47 @@ void F_DestroyUriList(Uri** list)
ddstring_t** F_ResolvePathList2(resourceclass_t defaultResourceClass,
const ddstring_t* pathList, size_t* count, char delimiter)
{
{Uri** uris;
if((uris = F_CreateUriListStr(RC_NULL, pathList)) != 0)
{
ddstring_t** paths = 0;
size_t numResolvedPaths = 0;
Uri** uriList = F_CreateUriListStr(RC_NULL, pathList);
size_t resolvedPathCount = 0;
ddstring_t** paths = NULL;

{ Uri** ptr;
for(ptr = uris; *ptr; ++ptr)
if(uriList)
{
Uri** uriIt;
for(uriIt = uriList; *uriIt; ++uriIt)
{
if(Uri_Resolved(*ptr) != 0) // Ignore incomplete paths.
++numResolvedPaths;
}}
// Ignore incomplete paths.
ddstring_t* resolvedPath = Uri_Resolved(*uriIt);
if(!resolvedPath) continue;

++resolvedPathCount;
Str_Delete(resolvedPath);
}

if(numResolvedPaths != 0)
if(resolvedPathCount)
{
uint n = 0;
paths = malloc(sizeof(*paths) * (numResolvedPaths+1));
{ Uri** ptr;
for(ptr = uris; *ptr; ++ptr)

paths = (ddstring_t**)malloc(sizeof(*paths) * (resolvedPathCount+1));
if(!paths) Con_Error("F_ResolvePathList: Failed on allocation of %lu bytes for new path list.", (unsigned long) sizeof(*paths) * (resolvedPathCount+1));

for(uriIt = uriList; *uriIt; ++uriIt)
{
ddstring_t* resolvedPath;
if((resolvedPath = Uri_Resolved(*ptr)) == 0)
continue; // Incomplete path; ignore it.
// Let's try to make it a relative path.
F_RemoveBasePath(resolvedPath, resolvedPath);
// Ignore incomplete paths.
ddstring_t* resolvedPath = Uri_Resolved(*uriIt);
if(!resolvedPath) continue;

paths[n++] = resolvedPath;
}}
paths[n] = 0; // Terminate.
}

paths[n] = NULL; // Terminate.
}
F_DestroyUriList(uris);
if(count)
*count = numResolvedPaths;
return paths;
}}
if(count)
*count = 0;
return 0;

F_DestroyUriList(uriList);
}

if(count) *count = resolvedPathCount;
return paths;
}

ddstring_t** F_ResolvePathList(resourceclass_t defaultResourceClass,
Expand Down
8 changes: 0 additions & 8 deletions doomsday/engine/portable/src/uri.c
Expand Up @@ -83,11 +83,6 @@ static void parseScheme(Uri* uri, resourceclass_t defaultResourceClass)
}
}


/**
* Substitute known symbols in the possibly templated path.
* Resulting path is a well-formed, sys_filein-compatible file path (perhaps base-relative).
*/
static ddstring_t* resolveUri(const Uri* uri)
{
ddstring_t part, *dest = Str_New();
Expand Down Expand Up @@ -179,9 +174,6 @@ static ddstring_t* resolveUri(const Uri* uri)
Str_Append(dest, Str_Text(&part));
}

// Expand any old-style base-relative path directives.
F_ExpandBasePath(dest, dest);

// No errors detected.
successful = true;

Expand Down

0 comments on commit 9944a36

Please sign in to comment.