Visual Studio memory leak detection not printing file name and line number #2 - memory-leaks

I have the same problem as reported by
Visual Studio memory leak detection not printing file name and line number
and several others. I am not getting the line number and file name in the output. Thus the title. I get all the other information; for example:
Dumping objects ->
{79} normal block at 0x000002193D173F30, 56 bytes long.
Data: <0? = 0? = > 30 3F 17 3D 19 02 00 00 30 3F 17 3D 19 02 00 00
Object dump complete.
HOWEVER
I am using _DEBUG; it is in the project's preprocesser properties.
I have
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
at the beginning of the code
I call
_CrtDumpMemoryLeaks();
at program termination
and most of all, I am using C with calloc and free and not C++ 'new'. Most of the problems were with respect to not including the special macro to handle the fact they were using 'new'. I got so desperate I defined the macro anyways and of course it made no difference. The program is quite simple:
#define _CRTDBG_MAP_ALLOC
#include <stdio.h>
#include <stdlib.h>
#include <crtdbg.h>
#include "UnitTestMethods.h"
int main()
{
//_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
printf(getMderFloatFromStringTests() ? "==> PASS: " : "==> !! FAIL: ");
printf("Overall result of MderFloat from String tests\n");
printf(getBtleTimeFromDtmTests() ? "==> PASS: " : "==> !! FAIL: ");
printf("Overall result for DTM to BTLE time stamp tests\n");
printf(loadStringFromJsonTests() ? "==> PASS: " : "==> !! FAIL: ");
printf("Overall result of Load String from JSON tests\n");
printf(hexStringToBytesLittleEndianTest() ? "==> PASS: " : "==> !! FAIL: ");
printf("Overall result for HEX string to byte array little endian tests\n");
printf(bytesToHexTest() ? "==> PASS: " : "==> !! FAIL: ");
printf("Overall result for byte array to HEX tests\n");
printf(encodeCommonControlsBatteryTests() ? "==> PASS: " : "==> !! FAIL: ");
printf("Overall result for encode common controls battery tests\n");
printf(encodeCommonControlsDisTests() ? "==> PASS: " : "==> !! FAIL: ");
printf("Overall result for encode common controls DIS tests\n");
printf(encodeCommonControlsDateTimeTests() ? "==> PASS: " : "==> !! FAIL: ");
printf("Overall result for encode common controls DateTime tests\n");
printf(encodeCommonControlsDateTimeEmptyTests() ? "==> PASS: " : "==> !! FAIL: ");
printf("Overall result for encode common controls DateTime Empty tests\n");
printf(encodeCommonControlsCtsTests() ? "==> PASS: " : "==> !! FAIL: ");
printf("Overall result for encode common controls CTS tests\n");
printf(encodeCommonControlsCtsEmptyTests() ? "==> PASS: " : "==> !! FAIL: ");
printf("Overall result for encode common controls CTS empty tests\n");
printf(encodeThermParamsTests() ? "==> PASS: " : "==> !! FAIL: ");
printf("Overall result for encode ThermParam tests\n");
_CrtDumpMemoryLeaks();
}
Is there anything else I could be missing? I found all my other memory leaks except the one above by carefully parsing the code, but it would have been much easier IF the name and line number had been printed for those!

Related

Linux setxattr: possible to use Unicode string?

I wrote the following code in VS Code and ran it to set file attribute. It seemed to have run successfully, but when I checked the value, the text was not correct. Is Unicode string supported for file extended attributes? If so, how can I fix the code below?
#include <stdio.h>
#include <sys/xattr.h>
int main()
{
printf("ねこ\n");
ssize_t res = setxattr("/mnt/cat/test.txt", "user.dog"
, "ねこ", 2, 0); /*also tested 4 and 8*/
printf("Result = %lu\n", (unsigned long)res);
return 0;
}
Programme output
ねこ
Result = 0
Reading attribute
$ getfattr test.txt -d
# file: test.txt
user.dog=0s44E=
Obviously ねこ can't be stored in 2 bytes. The characters are U+306D and U+3053, encoded in UTF-8 as E3 81 AD E3 81 93 so length must be set to 6. If you did that you'll see that getfattr test.txt -d outputs
user.dog=0s44Gt44GT
That's because -d doesn't what format the data is in and just dumps it as binary. The 0s prefix means that the data is in base64 as stated from the manpage:
-d, --dump
Dump the values of all matched extended attributes.
-e en, --encoding=en
Encode values after retrieving them. Valid values of en are "text", "hex", and "base64". Values encoded as text strings are enclosed in double quotes ("), while strings encoded as hexidecimal and base64 are prefixed with 0x and 0s, respectively.
Just plug 44Gt44GT into any base64 decoder or run echo 44Gt44GT | base64 --decode and you'll see the correct string printed out. To see the string directly from getfattr you need to specify the format with -e text
$ getfattr -n user.dog -e text test.txt
# file: test.txt
user.dog="ねこ"

How to recognize tab space in my search string while using awk?

My input file is .h file containing
#define __CON__ 2
#define __CON_MINOR__ 23
#define __CON_PREREQ(maj, min) \
((__CON__ << 16) + __CON_MINOR__ >= ((maj) << 16) + (min))
I need to search for __CON_ which prints out 2 and 23 as 2.23. There's a tab space after CON__. I'm using the command
awk '/__CON__/&&'/__CON_MINOR/' {print $3;}' features.h
getting a wrong output for this.
You can do something like this:
awk '/__CON__/ {con=$3;} /__CON_MINOR__/ {print con "." $3; exit;}' features.h

In Perl, why is a utf-8 string printed differently when split into characters?

A specially constructed string is printed differently when I use
print $b;
or
print for split //, $b;
A minimal example is:
#!perl
use warnings;
use strict;
use Encode;
my $b = decode 'utf8', "\x{C3}\x{A1}\x{E2}\x{80}\x{93}\x{C3}\x{A1}"; # 'á–á' in Unicode;
print $b, "\n";
print for split //, $b
The output on the console screen (I think I use cp860) is:
Wide character in print at xx.pl line 9.
├íÔÇô├í
Wide character in print at xx.pl line 10.
ßÔÇôß
or in hex:
C3 A1 E2 80 93 C3 A1
E1 E2 80 93 E1
(separated by 0D 0A of course, i.e., \r\n).
The question is WHY is the character rendered differently?
Surprisingly, the effect disappears without the em-dash. The effect is seen for longer strings, as the following example shows.
For the string 'Él es mi tío Toño –Antonio Pérez' (typed as Unicode in the program; note that the two lines are different!):
Wide character in print at xx.pl line 14.
├ël es mi t├¡o To├▒o ÔÇôAntonio P├®rez
Wide character in print at xx.pl line 15.
╔l es mi tÝo To±o ÔÇôAntonio PÚrez
However, for the string 'Él es mi tío Toño, Antonio Pérez':
╔l es mi tÝo To±o, Antonio PÚrez
╔l es mi tÝo To±o, Antonio PÚrez
nothing bad happens, and the two lines are rendered in the same way. The only difference is the presence of an en-dash –, i.e., '\x{E2}\x{80}\x{93}'!
Also, print join '', split //, $b; gives the same result as print $b; but different from print for split //, $b;.
If I add binmode STDOUT, 'utf8';, then both outputs are ÔÇô├í = E2 80 93 C3 A1.
So my question is not exactly about how to avoid it, but about why this happens: why does the same string behave differently when split?
Apparently in both cases the utf8 flag is on. Here is a more detailed program that shows more information about both strings: $a before decode and $b after decode:
#!perl
use warnings;
use strict;
use 5.010;
use Encode;
my $a = "\x{C3}\x{A1}\x{E2}\x{80}\x{93}\x{C3}\x{A1}"; # 'á–á' in Unicode;
my $b = decode 'utf8', $a;
say '------- length and utf8 ---------';
say "Length (a)=", length $a, ", is_uft8(a)=", (Encode::is_utf8 ($a) // 'no'), ".";
say "Length (b)=", length $b, ", is_uft8(b)=", (Encode::is_utf8 ($b) // 'no'), ".";
say '------- as a variable---------';
say "a: $a";
say "b: $b", ' <== *** WHY?! ***';
say '------- split ---------';
print "a: "; print for split //, $a; say '';
print "b: "; print for split //, $b; say ' <== *** DIFFERENT! ***';
say '------- split with spaces ---------';
print "a: "; print "[$_] " for split //, $a; say '';
print "b: "; print "[$_] " for split //, $b; say '';
say '------- split with properties ---------';
print "a: "; print "[$_ is_utf=" . Encode::is_utf8 ($_) . " length=" . length ($_) . "] " for split //, $a; say '';
print "b: "; print "[$_ is_utf=" . Encode::is_utf8 ($_) . " length=" . length ($_) . "] " for split //, $b; say '';
say '------- ord() ---------';
print "a: "; print ord, " " for split //, $a; say '';
print "b: "; print ord, " " for split //, $b; say '';
and here is its output on the console:
------- length and utf8 ---------
Length (a)=7, is_uft8(a)=.
Length (b)=3, is_uft8(b)=1.
------- as a variable---------
a: ├íÔÇô├í
Wide character in say at x.pl line 16.
b: ├íÔÇô├í <== *** WHY?! ***
------- split ---------
a: ├íÔÇô├í
Wide character in print at x.pl line 19.
b: ßÔÇôß <== *** DIFFERENT! ***
------- split with spaces ---------
a: [├] [í] [Ô] [Ç] [ô] [├] [í]
Wide character in print at x.pl line 22.
b: [ß] [ÔÇô] [ß]
------- split with properties ---------
a: [├ is_utf= length=1] [í is_utf= length=1] [Ô is_utf= length=1] [Ç is_utf= length=1] [ô is_utf= length=1] [├ is_utf= length=1] [í is_utf= length=1]
Wide character in print at x.pl line 25.
b: [ß is_utf=1 length=1] [ÔÇô is_utf=1 length=1] [ß is_utf=1 length=1]
------- ord() ---------
a: 195 161 226 128 147 195 161
b: 225 8211 225
The difference is whether the string being printed contains any characters >255. print only knows you did something wrong in that situation[1].
Given a handle with no :encoding, print expects a string of bytes (string of characters ≤255).
When it doesn't receive bytes (the string contains characters >255), it notifies you of the error ("wide character") and guesses that you meant to encode the string using UTF-8.
You can think of print on a handle with no :encoding as doing the following:
if ($s =~ /[^\x00-\xFF]/) {
warn("Wide character");
utf8::encode($s);
}
my $b = decode 'utf8', "\x{C3}\x{A1}\x{E2}\x{80}\x{93}\x{C3}\x{A1}";
is the same as
my $b = "\xE1\x{2013}\xE1";
As such, you are doing
print "\xE1\x{2013}\xE1";
print "\xE1";
print "\x{2013}";
print "\xE1";
print "\xE1\x{2013}\xE1"; # Wide char! C3 A1 E2 80 93 C3 A1
Perl notices you forgot to encode, warns you, and prints the string encoded using UTF-8.
print "\xE1"; # E1
Perl has no way of knowing you forgot to encode, so it prints what you asked it to print.
print "\x{2013}"; # Wide char! E2 80 93
Perl notices you forgot to encode, warns you, and prints the string encoded using UTF-8.
Footnotes
The choice of storage format (as returned by is_utf8) should never have an effect. print is correctly unaffected by it.
utf8::downgrade( my $d = chr(0xE1) ); print($d); # UTF8=0 prints E1
utf8::upgrade( my $u = chr(0xE1) ); print($u); # UTF8=1 prints E1

Multiple strings, Truncate line at 80 characters

I'm new to awk and sed, and I'm looking for a way to truncate a line at 80 characters, but I'm printing several strings in that line using printf. The last two strings are the ones that give me problems because they vary in size on each iteration of my code. Here's my current code:
printf "%5d %3s%.2s %4s %s %s \n" "$f" "$month" "$day" "$year" "$from" "$subject"
This code is being used to create a summary of email messages that get passed through a Bash script. What I do know, is that with the spaces and requirements of my other strings, I have room for 60 characters between the $from and $subject strings.
Any help is appreciated.
I'm looking for a way to truncate a line at 80 characters ...
You could pipe the output to cut:
printf ... | cut -c 1-80
If you wanted to ensure that each line isn't more than 80 characters (or wrap lines to fit in specified width), you could use fold:
printf ... | fold -w 80
Another way to solve this just using Bash (syntax: ${var:0:80}), e.g.:
printf "%5d %3s%.2s %4s %s %s \n" "$f" "$month" "$day" "$year" "$from" "${subject::80}"
This truncates the string before it gets to printf. This method would also allow you to specify different maximum widths for each printed column individually.
I had the same issue trying to customize my bash prompt with a truncated directory name. What finaly worked was:
PS1='\u#\h:`echo $(basename $PWD) | cut -c 1-15`\$ '
You could use substr to only grab the 1st n characters of from and subject, since you know you only have a max of 60 characters to play with you could grab the 1st 25 of 'from' and the 1st 35 of 'subject'.
#!/usr/bin/gawk -f
BEGIN {
# set ouput delimiter to comma
OFS=","
# set input delimiter to bar
FS="|" }
{
f=$1
month=$2
day=$3
year=$4
from=$5
subject=$6
from=substr(from,1,25)
subject=substr(subject,1,35)
printf ("%5d,%3s%.2s,%4s,%s,%s\n",f,month,day,year,from,subject)
}
Running the above on this file
12123|Jan|14|1970|jack#overthehill.com|"Happy birthday"
14545|Jan|15|1970|jill#thewell.com|"Hope your head is ok"
27676|Feb|14|1970|jack#overthehill.com|"Still on for tonight?"
29898|Feb|14|1970|jill#thewell.com|"Sure, if you bring the chocolate."
34234|Feb|15|1970|jack#overthehill.com|"Had a great time last night,
hope you did too. Can't wait for the weekend, love Jack"
Returns
12123,Jan14,1970,jack#overthehill.com,"Happy birthday"
14545,Jan15,1970,jill#thewell.com,"Hope your head is ok"
27676,Feb14,1970,jack#overthehill.com,"Still on for tonight?"
29898,Feb14,1970,jill#thewell.com,"Sure, if you bring the chocolate."
34234,Feb15,1970,jack#overthehill.com,"Had a great time last night, hope
How about a C version?
#include <stdio.h>
int maxline = 80;
int main(int argc, char* argv[])
{
char line[2048];
if ((argc>1) && (atoi(argv[1]) > 0)) {
maxline = atoi(argv[1]);
}
while (fgets(line, sizeof(line), stdin)) {
line[maxline] = '\0';
printf("%s\n", line);
}
}

Assembly GDB Print String

So in assembly I declare the following String:
Sample db "This is a sample string",0
In GDB I type "p Sample" (without quotes) and it spits out 0x73696854. I want the actual String to print out. So I tried "printf "%s", Sample" (again, without quotes) and it spits out "Cannot access memory at address 0x73696854."
Short version:
How do I print a string in GDB?
My teacher just emailed me back. For anyone wondering:
p(char[20]) Sample
Where 20 is the number of characters to print out.
To print a C-style NUL-terminated string, you should also be able to do this:
print (char*) &Sample
printf "%s", &Sample
I had the same issue! Try this one:
x/s &Sample # prints the whole string
"x" - Stands generally for examining data.
For a signle character you could youse this code
x/c &Sample # prints: "T"
And if you want see multiple characters you could give the number of wished characters
x/3c &Sample # prints: "T" "h" "i"

Resources