[BUG] read access violation when using developer_tools/malloc_dbg #99

Closed
opened 2026-01-29 15:03:21 +00:00 by claunia · 9 comments
Owner

Originally created by @sebbeutler on GitHub (Aug 25, 2021).

Description

When using malloc_dbg.h to keep track of memory allocations, the program throw a read access violation. tmp was nullptr. in file malloc_dbg.c at line :
cc241f58c2/developer_tools/malloc_dbg.c (L215)

when calling malloc _dbg inside a loop.

This issue is happening because of the malloc_debug fonction.

	pos = inList(filename, line);
	if (pos == -1)    
	{
		// Add a new element in the mem_info list
		memoryInformation = addMemInfo(memoryInformation, ptrToReturn, bytes, line, filename, functionName);
		if (!memoryInformation)
		{
			free(ptrToReturn);
			return NULL;
		}
	}
	else           // If malloc was already called from the same file at the same line
	{
                // Here there are no call of [addMemInfo](https://github.com/TheAlgorithms/C/blob/cc241f58c253c533ac94e07151ef91a5ef7e5719/developer_tools/malloc_dbg.c#L40-L70) so the pointer that we return is not saved in the linked list
                // so we will never be able to free it later.
		editInfo(pos, bytes);
	}
	return ptrToReturn;

Expected behavior

ptrToReturn being added to the linked list

Actual behavior

only the sizeof ptrToReturn is saved into the list and it throws a read access violation

Possible fix

remove the inList() condition check and the editInfo() call

Steps to reproduce

Replace developer_tools/test_malloc_dbg.c main() by :

int main(int argc, char* argv[])
{
    int* test_list[5] = {NULL};

    for (unsigned i = 0; i < 5; i++)
    {
        test_list[i] = malloc(sizeof(int));
    }

    for (unsigned i = 0; i < 5; i++)
    {
        free(test_list[i]);
    }
    
    return 0;
}

Context

Got this bug when trying to allocate multiple blocks of memory in a loop and using malloc_dbg macros.

Additional information

No response

Originally created by @sebbeutler on GitHub (Aug 25, 2021). ### Description When using malloc_dbg.h to keep track of memory allocations, the program throw a `read access violation. tmp was nullptr.` in file `malloc_dbg.c` at line : https://github.com/TheAlgorithms/C/blob/cc241f58c253c533ac94e07151ef91a5ef7e5719/developer_tools/malloc_dbg.c#L215 when calling malloc _dbg inside a loop. This issue is happening because of the [malloc_debug ](https://github.com/TheAlgorithms/C/blob/cc241f58c253c533ac94e07151ef91a5ef7e5719/developer_tools/malloc_dbg.c#L120-L161)fonction. ``` pos = inList(filename, line); if (pos == -1) { // Add a new element in the mem_info list memoryInformation = addMemInfo(memoryInformation, ptrToReturn, bytes, line, filename, functionName); if (!memoryInformation) { free(ptrToReturn); return NULL; } } else // If malloc was already called from the same file at the same line { // Here there are no call of [addMemInfo](https://github.com/TheAlgorithms/C/blob/cc241f58c253c533ac94e07151ef91a5ef7e5719/developer_tools/malloc_dbg.c#L40-L70) so the pointer that we return is not saved in the linked list // so we will never be able to free it later. editInfo(pos, bytes); } return ptrToReturn; ``` ### Expected behavior ptrToReturn being added to the linked list ### Actual behavior only the sizeof ptrToReturn is saved into the list and it throws a read access violation ### Possible fix remove the inList() condition check and the editInfo() call ### Steps to reproduce Replace [developer_tools/test_malloc_dbg.c](https://github.com/TheAlgorithms/C/blob/master/developer_tools/test_malloc_dbg.c) main() by : ``` int main(int argc, char* argv[]) { int* test_list[5] = {NULL}; for (unsigned i = 0; i < 5; i++) { test_list[i] = malloc(sizeof(int)); } for (unsigned i = 0; i < 5; i++) { free(test_list[i]); } return 0; } ``` ### Context Got this bug when trying to allocate multiple blocks of memory in a loop and using malloc_dbg macros. ### Additional information _No response_
claunia added the Stalebug labels 2026-01-29 15:03:21 +00:00
Author
Owner

@github-actions[bot] commented on GitHub (Oct 5, 2021):

This issue has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions[bot] commented on GitHub (Oct 5, 2021): This issue has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Author
Owner

@github-actions[bot] commented on GitHub (Nov 5, 2021):

This issue has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions[bot] commented on GitHub (Nov 5, 2021): This issue has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Author
Owner

@github-actions[bot] commented on GitHub (Dec 6, 2021):

This issue has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions[bot] commented on GitHub (Dec 6, 2021): This issue has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Author
Owner

@github-actions[bot] commented on GitHub (Dec 14, 2021):

Please ping one of the maintainers once you add more information and updates here. If this is not the case and you need some help, feel free to ask for help in our Gitter channel or our Discord server. Thank you for your contributions!

@github-actions[bot] commented on GitHub (Dec 14, 2021): Please ping one of the maintainers once you add more information and updates here. If this is not the case and you need some help, feel free to ask for help in our [Gitter](https://gitter.im/TheAlgorithms) channel or our [Discord server](https://discord.gg/c7MnfGFGa6). Thank you for your contributions!
Author
Owner

@github-actions[bot] commented on GitHub (Jan 14, 2022):

This issue has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions[bot] commented on GitHub (Jan 14, 2022): This issue has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Author
Owner

@github-actions[bot] commented on GitHub (Jan 22, 2022):

Please ping one of the maintainers once you add more information and updates here. If this is not the case and you need some help, feel free to ask for help in our Gitter channel or our Discord server. Thank you for your contributions!

@github-actions[bot] commented on GitHub (Jan 22, 2022): Please ping one of the maintainers once you add more information and updates here. If this is not the case and you need some help, feel free to ask for help in our [Gitter](https://gitter.im/TheAlgorithms) channel or our [Discord server](https://discord.gg/c7MnfGFGa6). Thank you for your contributions!
Author
Owner

@github-actions[bot] commented on GitHub (Feb 22, 2022):

This issue has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions[bot] commented on GitHub (Feb 22, 2022): This issue has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Author
Owner

@github-actions[bot] commented on GitHub (Mar 25, 2022):

This issue has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions[bot] commented on GitHub (Mar 25, 2022): This issue has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Author
Owner

@github-actions[bot] commented on GitHub (Apr 1, 2022):

Please ping one of the maintainers once you add more information and updates here. If this is not the case and you need some help, feel free to ask for help in our Gitter channel or our Discord server. Thank you for your contributions!

@github-actions[bot] commented on GitHub (Apr 1, 2022): Please ping one of the maintainers once you add more information and updates here. If this is not the case and you need some help, feel free to ask for help in our [Gitter](https://gitter.im/TheAlgorithms) channel or our [Discord server](https://discord.gg/c7MnfGFGa6). Thank you for your contributions!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/C#99