User forums > Help

Malloc() / Heap issue with debug build target

(1/3) > >>

Marnix:
I run Code::Blocks 17.12 on Windows 10

I have different behavior with the debug and release build targets in my CB. Because the program runs ok outside CB (release and debug builds), I want to rule out a CB setting issue.

I have a routine that reads a string from a file:


--- Code: ---char *ReadString(void)
{
  int32_t len;   /* length of the string */
  char    *str = NULL;

  /* First, read the length of the string. */
  if (!GetNextCode32(&len)) {
    PrintError(14, NULL, "ReadString()");
    return(NULL);
  }

  /* create space on heap */
  if ((str = (char *) malloc(len*sizeof(char))) == NULL) {
    PrintError(15, NULL, "ReadString()");
    return(NULL);
  }

  /* read the string */
  if (fread((void *) str, sizeof(char), len, datafile) != len) {
    PrintError(15, NULL, "ReadString()");
    return(NULL);
  }

  /* all went well */
  /* don't forget to free(str) in calling function */
  return(str);
}
--- End code ---

The program is a console application (.exe)

When I build the program without debug info it runs fine inside and outside CB.

When I build it with the Debug build target in CB:
- the program runs fine outside CB
- the program runs fine inside CB when I start it with Run (ctrl-F10, the green triangle)
- the program crashes on a malloc() when I start it with Debug/continue (ctrl F8, red triangle)

This is the stack trace:


--- Code: ---#0 0x772b8a76 ntdll!RtlRunOnceBeginInitialize() (C:\WINDOWS\SYSTEM32\ntdll.dll:??)
#1 0x772a7799 ntdll!RtlAllocateHeap() (C:\WINDOWS\SYSTEM32\ntdll.dll:??)
#2 0x772a5ec9 ntdll!RtlAllocateHeap() (C:\WINDOWS\SYSTEM32\ntdll.dll:??)
#3 0x772a5d3e ntdll!RtlAllocateHeap() (C:\WINDOWS\SYSTEM32\ntdll.dll:??)
#4 0x7734250d ntdll!RtlpNtSetValueKey() (C:\WINDOWS\SYSTEM32\ntdll.dll:??)
#5 0x772a6dd9 ntdll!RtlAllocateHeap() (C:\WINDOWS\SYSTEM32\ntdll.dll:??)
#6 0x772a5ec9 ntdll!RtlAllocateHeap() (C:\WINDOWS\SYSTEM32\ntdll.dll:??)
#7 0x772a5d3e ntdll!RtlAllocateHeap() (C:\WINDOWS\SYSTEM32\ntdll.dll:??)
#8 0x770772a0 msvcrt!malloc() (C:\WINDOWS\System32\msvcrt.dll:??)
#9 0x409904 ReadString() (D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\fileio.c:117)
#10 0x40aa4a ReadFlags(offset=706786) (D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\fileio.c:553)
#11 0x406f45 InitDirs() (D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\dirs.c:92)
#12 0x40d540 main(argc=1, argv=0xbe0dd8) (D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\init.c:286)
--- End code ---

This is part of the output from the Dr Memory tool for the debug build target:


--- Code: ---Error #1: UNADDRESSABLE ACCESS beyond heap bounds: writing 0x012f3a28-0x012f3a2c 4 byte(s)
# 0 ReadFlags               [D:/Marnix/Codeblocks/XVAN 2.4/01 - Interpreter/fileio.c:553]
# 1 InitDirs                [D:/Marnix/Codeblocks/XVAN 2.4/01 - Interpreter/dirs.c:92]
# 2 main                    [D:/Marnix/Codeblocks/XVAN 2.4/01 - Interpreter/init.c:286]
Note: @0:00:06.109 in thread 11816
Note: next higher malloc: 0x012f3a50-0x012f3a54
Note: refers to 0 byte(s) beyond last valid byte in prior malloc
Note: prev lower malloc:  0x012f3a28-0x012f3a28
Note: instruction: mov    %eax -> (%ebx)
--- End code ---

I hope someone can help me out with this.

stahta01:
Look at the build log!

http://wiki.codeblocks.org/index.php/FAQ-Compiling_(errors)#Q:_How_do_I_troubleshoot_a_compiler_problem.3F

Marnix:

I did that before I started the thread (I read the read-this-before-you-post message). Nothing strange, only 3 warnings for using int64_t for a long int.



--- Code: ----------------- Clean: Debug in Interpreter (compiler: GNU GCC Compiler)---------------

Cleaned "Interpreter - Debug"

-------------- Build: Debug in Interpreter (compiler: GNU GCC Compiler)---------------

mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\arithmtc.c" -o obj\Debug\arithmtc.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\checkpar.c" -o obj\Debug\checkpar.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\cleanup.c" -o obj\Debug\cleanup.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\debug.c" -o obj\Debug\debug.o
D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\debug.c: In function 'PrintLocationDirectory':
D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\debug.c:135:28: warning: format '%ld' expects argument of type 'long int', but argument 3 has type 'int64_t {aka long long int}' [-Wformat=]
     sprintf(text_to_print, "\nOffset: %ld\n\n", loc_dir[i].offset);
                            ^
D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\debug.c: In function 'PrintObjectDirectory':
D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\debug.c:181:28: warning: format '%ld' expects argument of type 'long int', but argument 3 has type 'int64_t {aka long long int}' [-Wformat=]
     sprintf(text_to_print, "\nOffset: %ld\n\n", obj_dir[i].offset);
                            ^
D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\debug.c: In function 'PrintVerbDir':
D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\debug.c:598:12: warning: format '%ld' expects argument of type 'long int', but argument 2 has type 'int64_t {aka long long int}' [-Wformat=]
     printf("offset: %ld\n", verb_dir[i].offset);
            ^
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\dirs.c" -o obj\Debug\dirs.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\disambig.c" -o obj\Debug\disambig.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\endian.c" -o obj\Debug\endian.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\errors.c" -o obj\Debug\errors.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\execute.c" -o obj\Debug\execute.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\fileio.c" -o obj\Debug\fileio.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\init.c" -o obj\Debug\init.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\input.c" -o obj\Debug\input.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\json.c" -o obj\Debug\json.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\Languages\ENG\ENG-article.c" -o obj\Debug\Languages\ENG\ENG-article.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\Languages\ENG\ENG-checksyntax.c" -o obj\Debug\Languages\ENG\ENG-checksyntax.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\Languages\ENG\ENG-moreinfo.c" -o obj\Debug\Languages\ENG\ENG-moreinfo.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\Languages\ENG\ENG-xeqfun.c" -o obj\Debug\Languages\ENG\ENG-xeqfun.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\Languages\NL\NL-article.c" -o obj\Debug\Languages\NL\NL-article.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\Languages\NL\NL-checksyntax.c" -o obj\Debug\Languages\NL\NL-checksyntax.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\Languages\NL\NL-moreinfo.c" -o obj\Debug\Languages\NL\NL-moreinfo.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\Languages\NL\NL-xeqfun.c" -o obj\Debug\Languages\NL\NL-xeqfun.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\lets-try.c" -o obj\Debug\lets-try.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\letsplay.c" -o obj\Debug\letsplay.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\output.c" -o obj\Debug\output.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\readfun.c" -o obj\Debug\readfun.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\readloc.c" -o obj\Debug\readloc.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\readobj.c" -o obj\Debug\readobj.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\readtrig.c" -o obj\Debug\readtrig.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\readverb.c" -o obj\Debug\readverb.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\resglobs.c" -o obj\Debug\resglobs.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\restart.c" -o obj\Debug\restart.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\restore.c" -o obj\Debug\restore.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\save.c" -o obj\Debug\save.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\spanTree.c" -o obj\Debug\spanTree.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\syntax.c" -o obj\Debug\syntax.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\sysfunc.c" -o obj\Debug\sysfunc.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\timers.c" -o obj\Debug\timers.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\trnslate.c" -o obj\Debug\trnslate.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\xeqfun.c" -o obj\Debug\xeqfun.o
mingw32-g++.exe  -o bin\Debug\Interpreter.exe obj\Debug\arithmtc.o obj\Debug\checkpar.o obj\Debug\cleanup.o obj\Debug\debug.o obj\Debug\dirs.o obj\Debug\disambig.o obj\Debug\endian.o obj\Debug\errors.o obj\Debug\execute.o obj\Debug\fileio.o obj\Debug\init.o obj\Debug\input.o obj\Debug\json.o obj\Debug\Languages\ENG\ENG-article.o obj\Debug\Languages\ENG\ENG-checksyntax.o obj\Debug\Languages\ENG\ENG-moreinfo.o obj\Debug\Languages\ENG\ENG-xeqfun.o obj\Debug\Languages\NL\NL-article.o obj\Debug\Languages\NL\NL-checksyntax.o obj\Debug\Languages\NL\NL-moreinfo.o obj\Debug\Languages\NL\NL-xeqfun.o obj\Debug\lets-try.o obj\Debug\letsplay.o obj\Debug\output.o obj\Debug\readfun.o obj\Debug\readloc.o obj\Debug\readobj.o obj\Debug\readtrig.o obj\Debug\readverb.o obj\Debug\resglobs.o obj\Debug\restart.o obj\Debug\restore.o obj\Debug\save.o obj\Debug\spanTree.o obj\Debug\syntax.o obj\Debug\sysfunc.o obj\Debug\timers.o obj\Debug\trnslate.o obj\Debug\xeqfun.o   
Output file is bin\Debug\Interpreter.exe with size 403.39 KB
Process terminated with status 0 (0 minute(s), 5 second(s))
0 error(s), 3 warning(s) (0 minute(s), 5 second(s))
 

--- End code ---

stahta01:
You are going to have to wait for an CB Debugging person. They will likely ask for a full debugging log.
I have no idea how to turn on the debugging log or how to find the log. I do not use the CB Debugger very much.

Tim S.

BlueHazzard:
Have you enabled watch local variables, or watch function arguments?
What is the exact crash reason? SIGSEG or an other sig?
Have you set a breakpoint?
Does it reach this breakpoint?
Have you tried to set a breakpoint at the beginning of main, and step until your application crashes?

I suspect you watch a variable that is not initialized and as soon as the debugger tries to access it the application crashes....

Navigation

[0] Message Index

[#] Next page

Go to full version