« first day (290 days earlier)      last day (2554 days later) » 
01:00 - 18:0018:00 - 22:00

1:18 AM
@milleniumbug Thanks!
 
 
12 hours later…
1:47 PM
hi there
i have only one question
can data race cause memory leak?
 
I could answer this directly, but I'm guessing you have some reason for this question
pedantically speaking, data race can cause a memory leak, since data races are undefined behaviour in C++
 
@milleniumbug You're right :P
@milleniumbug Thanks for the answer, I just wanted to make sure that data races may cause memory leak
 
they can cause all kinds of memory issues like dangling pointers and double free as well
 
of course whether something is possible and whether something is likely is a different matter
 
@SzymonMarczak In theory, yes. In reality, unless you're dealing with an implementation that provides garbage collection (and I doubt you are, since I'm pretty sure it doesn't exist) there's nearly no realistic chance of its actually happening.
 
1:58 PM
@milleniumbug I'm developing my first game in C++
 
if I knew I have a memory leak, searching for data races wouldn't be the first thing I would do
 
@milleniumbug What would you do?
 
first thing is unleashing a tool to track allocations and frees
and then seeing what leaked and who allocated it
then start tracking from there
 
valgrind works for tracking memory allocations on linux
there's "dr. memory" on windows, but I've never tried it
 
@milleniumbug Valgrind slows down my server so much, that I have to wait 30m to connect...
 
2:01 PM
so I can't tell whether it's reliable or not
 
@ratchetfreak Thank you. You have just answered to my all questions! That's why I got double frees or segmentation faults.
@milleniumbug If I have some time I use Valgrind too, ASAN is a bit faster than Valgrind, but ASAN doesn't see all the mistakes.
 
yes, that's a tradeoff between speed and the number of false negatives
 
you can also instrument your allocations which may be a bit faster
 
yes, it goes something like this
(faster, less effective) --------- debug allocators ---------- asan ------------------------- valgrind -----> (slower, more effective)
 
is there anyway that i could typedef something inside an object from a member function?
 
2:10 PM
1 message moved from Lounge<C++>
you can declare the typedef in a class
 
I dislike the malloc api though, giving only a memory location to track your allocations even though you often need a bit more
 
@milleniumbug can't i, for example, have objects from the same class to have different typedefs for the same alias inside each of them?
 
typedefs are compile time things
 
gotcha
is there anyway, then to take any template instantiation from a certain template class as an argument for a function?
 
2:13 PM
you make the function a template
 
let's say i want to store Type<int> and Type<long> in the same vector, let's say
i can't declare the std::vector as std::vector<Type>
that's not a complete type
 
well, you can't
 
cause they are completely different types, right?
 
you can make Type<T> derive from a common class
 
possibly this all sounds xy problem-like, and it is
and store Base objects?
 
2:16 PM
but otherwise it's hard to describe what you could do if I don't know what are you trying to achieve
 
i'm trying to make a regex callback that runs a function once a regex pattern is met
 
yes, this is XY
make the matching function a template
 
the problem is, i wish to store the param types for the function inside a Function object
 
@SzymonMarczak I'd start with Clang's sanitizers--they do cause some slowdown, but not nearly as much as valgrind. IMO, memory leaks, however, are mostly a by-product of poor design. They way to fix them is to design the code better. RAII: it's not just a pattern; it's a way of life.
 
@ChemiCalChems what for
 
2:18 PM
@milleniumbug Sounds like he wants to create a partially bound function (but maybe not).
 
so that i can have type safety when passing parameters to the function
 
@JerryCoffin this is possibly what he wants, but I want to confirm
 
i have no clue what a partially bound function is, though
lemme google it
 
@ChemiCalChems This sounds a bit like somebody asking: "Why do you need a water bottle?" and getting an answer of: "because my dog is brown."
 
@JerryCoffin Yeah, only that I do need a water bottle because my dog is brown, because he gets hotter in the sun and he gets thirsty.
 
2:21 PM
@ChemiCalChems It's where you bind some (but not all) the parameters to a function. It's what std::bind was used for, and a frequent use of lambdas.
 
oh, gotcha
 
@ChemiCalChems std::vector<std::variant<Type<int>, Type<long>>>
 
@ratchetfreak hmmm
i gotta redesign the way i wanna code this, it seems like
 
so, with your matcher example, if you planned to do this: match(needle, haystack, callback, other, arguments, and, their, types), you can instead do this match(needle, haystack, function), and make the user pass a lambda
 
that's nice and all, but not quite what i want to do
 
2:24 PM
for example [](regex_match m){ return f(m, 42, other, argument); }
 
i want to make a modable and modular console
 
yes, which is why we're asking
you're not helping much atm
 
i don't even know where to start asking
i'm really sorry
 
let's continue with
7 mins ago, by ChemiCalChems
the problem is, i wish to store the param types for the function inside a Function object
ok, what now
let's say you have the types stored
what now
what are you planning to do with them
 
then, once a regex is met, i could call the function, and assure that the arguments which i caught from the regex match the function parameters type
but that has me asking myself, aren't you gonna catch them from the regex as ints, for example, anyway?
say i give my program the input 3 + 5
 
2:27 PM
what you get out of the regex would be strings, no?
so make the function parse the strings
 
true, but after some processing i could get ints, yeah
my program would detect 3 + 5 as matching a regex a + b, which would call a function that returned a + b
only that it's not that simple, cause then i would want to implement logarithms, equation solving, and more importantly, modularity
 
so you're making an interpreter of some sort
 
as in, i'm a user, and i want to make a module for the console
let's say so, yes
 
if it's a dynamic language then tagged unions for the variables is best and then at runtime check the runtime types are proper
or convertable
 
you'd probably want to do proper lexing and parsing
 
2:30 PM
yes and no
i just want to see if the input matches any regex provided by any of the loaded modules, and if so, execute a callback
but i can't really store functions of different parameters inside a vector, can i?
 
then make that callback handle just the string
 
then the base class approach is applicable
 
@ratchetfreak seems rather tempting, but i want type safety
 
that's what the parsing is for
 
@milleniumbug make a Function base class, and store std::function<whatever> inside?
 
2:34 PM
also note that the more complicated will your language be, the more batshit insane is the regex approach
 
exactly, that's why i want regexs
 
the moment you'll want to evaluate things with parentheses you can't use regexes
 
in a typical console the method is first word is the command and the rest of the string gets passed into the command's callback
 
@milleniumbug why is that?
 
because regexes can't handle nesting
 
2:35 PM
hmmm
i could manage the nesting myself, and then give regexs individual "commands"
let's say i pass 3 + (1+2) to my program
i could say ok, i found two commands, 3 + a and 1+2
i pass 1+2 to my interpreter, get 3, and then do 3+3 in the interpreter again
 
that is, reinventing context-free grammar
 
@milleniumbug i guess so
 
regexes aren't the best solution for that, I've done a expression parser like that and I've used a shunting yard for that
 
@ratchetfreak well, i could substitute regexs for anything
the point is, once i detect a specific format of input, i want to call a callback based on the format of the input
i could do regex, or whatever i like
21
Q: C++ One std::vector containing template class of multiple types

jteditI need to store multiple types of a template class in a single vector. Eg, for: template <typename T> class templateClass{ bool someFunction(); }; I need one vector that will store all of: templateClass<int> t1; templateClass<char> t2; templateClass<std::string> t3; etc As far as I kn...

i guess that's what i want, yeah
 
what's c++ equivalent for this c code
char*f() {return NULL;}
int main() { char *str; if ((str=f()) == NULL) {...}}
basically looking for assignment return alternative.
 
2:44 PM
that should run in c++
 
ahh forgot to mention that, I want to replace char* with string
is this the most prevalent sematics for this
bool f(string &s)..
 
const reference for passing the argument
if you want "optional" return value, use boost::optional or std::optional
 
but i would want to change string s.
 
in that case, yes, you either want to pass a non-const reference to modify it in-place, or accept one and return a modified one
 
is it normal that i've been coding in c++ for 5 years and these questions keep coming up?
 
2:48 PM
@RaviUpadhyay empty string isn't sufficient?
 
but isn't that overkill. creating a string. putting some value in it (setting lengh or whatever goes on behind).
 
passing around an empty string doesn't have any real overhead
it's not much more than using std::optional
 
nwp
@ChemiCalChems your questions seem to be more about application design than C++
 
but, then it assumes that empty string is not a vaild return value.
 
@RaviUpadhyay yes, that's a problem
 
2:51 PM
@nwp i hope so
 
4 mins ago, by milleniumbug
if you want "optional" return value, use boost::optional or std::optional
 
yeah. will looking it up.
 
i'm gonna go for the base class and manage polymorphism later, yeah
seems like it could work
oh well, let's hope
 
nwp
@ChemiCalChems you might want to consider compiling your language to Lua and let Lua do everything for you
 
@nwp note taken
 
2:57 PM
a general question to all long time c++ coders, do you miss c?
i worked on a c# project for 6 months and i missed c badly. every time i would write a code in c#, I would think, ahh, it could also have been done in c in the same exact manner (except the long list of frees at the end of function and terrible string handling).what i noticed was
i was spending a lot of time on the designing part (class inheritance etc)
 
wait, is it C, C++, or C#
@RaviUpadhyay generally that's a problem when you migrate from a less rich environment to a richer one
learning idioms helps
 
nwp
@RaviUpadhyay inheritance is problematic, use it only if you must
 
but it becomes too tempting not to use when using c++ and related languages.
btw std::optional seems good. going to use it :D
 
it's very new so you may not be able to use it if you don't have the latest compiler
there's Boost.Optional which existed pretty much since forever
but it would mean using an external library
 
yeah. and i don't have boost right now.
 
3:04 PM
there are also other implementations of the same idea
 
so will give std a try
 
so it may be possible to simply copy an implementation from somewhere
 
@milleniumbug yup. don't have it.
installing boost. going to use it in the future anyways.
 
nwp
@RaviUpadhyay you can try #include <experimental/optional>
 
is it safe if I do only vector.size() in second thread?
or I have to lock mutex too?
 
nwp
3:13 PM
You must ensure that no other thread modifies vector while you do vector.size(). You can do that with a mutex or by knowing that no thread modifies vector ever.
 
okay, thanks
 
sup? How do I pass constexpr to lambda? When I have a function I template it and template arguments is a compile-time expression, but what to do if I want to do it with generic lambdas?
 
nwp
@EuriPinhollow You probably just use it in the lambda. Since it is known at compile time there is not passing necessary. Maybe make an example.
 
@nwp Think about template: "Since it is known at compile time passing is not necessary, hardcode it".
Wait a minute.
 
nwp
I didn't say "hardcode it", I said "use the constexpr value in the lambda"
 
3:29 PM
Constexpr value which I want to pass to lambda can differ. :D
coliru.stacked-crooked.com/a/df7e5addd666ab89 - how do I pass dim to lambda?
 
nwp
@EuriPinhollow just use it
 
are shared pointers thread safe?
 
nwp
@SzymonMarczak only the ref-counter
 
@nwp OK fine. How do I assign value to dim? :D
 
what's ref-counter?
 
3:33 PM
@nwp wouldn't returning string object create a construct/copy overhead (if somehow move semantics is not applied to it?)
 
@SzymonMarczak the pointer itself isn't thread safe but each thread accessing the object through their own pointer and passing the pointers around is as thread-safe as the object itself is.
 
nwp
@EuriPinhollow either the way I did with constexpr auto dim = 42; or not at all because constexpr and assigning new values kinda sometimes is a contradiction
 
@ratchetfreak so summarizing: accessing the pointer by shared pointers if thread safe?
 
Check last snippet.
 
nwp
3:36 PM
@SzymonMarczak shared_ptr keeps a reference counter that counts how many shared_ptr point to an object so it knows when the last one disappeared so it can delete the object. If you have a shared_ptr per thread and they both get destroyed at the same time and both decrement the counter at the same time everything is fine.
 
@SzymonMarczak if you have a std::shared_ptr<Foo> f; std::shared_ptr<Foo> f2; then both threads accessing/assigning to f is not thread safe
 
but if thread 1 accesses f and thread 2 accesses f2 then it is thread safe
* assuming the operation they end up doing on the contained Foo is thread safe
 
nwp
@EuriPinhollow That compiles, I don't really know what you are getting at.
 
@nwp I want to use blambda in same way as bfunction: I want to pass various constexprs to it.
I can pass any constexpr to bfunction as template parameter, I can only pass one value they way you provided.
 
nwp
3:40 PM
@EuriPinhollow you already have access to all the constexprs. Make an example that fails because you used the constexprs and it fails because you didn't pass them so I know what you mean.
@EuriPinhollow why only 1? What's stopping you from using another?
 
@nwp the name of constexpr variable is written in lambda. coliru.stacked-crooked.com/a/8eea63c8e9999876
I do not want to just use another, I want many of them.
Fixed it to be even more obvious: coliru.stacked-crooked.com/a/defed4930a63378e
 
nwp
@EuriPinhollow you can't. If you want that make a functions or a struct.
 
constexpr is meant to be a constant evaluated at compile time, having it change at all is counter to what it is
 
@milleniumbug This was in my head when I asked that: chat.stackoverflow.com/transcript/message/36838361#36838361
@nwp i.e. template function inside struct? Las time I checked it was impossible because of standard.
 
nwp
@EuriPinhollow I meant use a struct or function instead of the lambda.
maybe some day the committee will fix it and allow auto lambda = [](constexpr int dim){} but for now there is no way to do that with a lambda
 
3:48 PM
@nwp can you demonstrate it? It keeps flying over my head.
 
nwp
@EuriPinhollow What you already did. Use bfunction instead of blambda or possibly make a bstruct (why the bs?).
 
B's because in russian "blamba" kinda means "blot".
So "blambda" entertains me.
BTW, I have thought out a hack to do what I want.
 
4:03 PM
coliru.stacked-crooked.com/a/61ade7b031466f62 - something along these lines: pass a template instance which has value as template argument. Not ought to be a function, it can be class.
 
nwp
@EuriPinhollow how about this?
 
@nwp excellent! But why does my has coliru.stacked-crooked.com/a/20341296c17d76a6 not work?
 
nwp
> error: 'dim' is not a class, namespace, or enumeration
:: only works on those, not on variables
 
nwp
@EuriPinhollow well, read the error message
 
4:10 PM
aaaaaaaaa
Thanks for assistance!
 
how do I move shared pointer from first vector to the second one?
I'm not sure how to do it
 
dest.push_back(std::move(src[i]));
src.erase(src.begin()+i);
 
Nice. I hate normal pointers
I have too much mess with them
 
nwp
@SzymonMarczak Good. In time you will hate smartpointers too and then you can start writing proper C++ :P
 
@nwp I've detected memory leak in my program. ASAN and Valgrind didn't help. I don't know what's causing the memory leak, so I'm gonna switch to smart pointers. And maybe you're right, probably I will hate smart pointers too :P
 
4:23 PM
shared_ptr to avoid memory leak very often means there is a flaw in your design
shared_ptr means that an object hes multiple owners
 
I meant unique_ptr
:P
and I agree with @ratchetfreak
Is there any good tool which can help me detect where's memory leak?
 
@ratchetfreak of course, but is it a fundamental flaw?
@SzymonMarczak valgrind probably.
 
Already tried
Didn't help
says nothing
 
Wait a minute.
 
@EuriPinhollow very often an object really only has 1 actual owner
 
4:30 PM
" The --leak-check option turns on the detailed memory leak detector." - did you use this?
 
yup
valgrind --leak-check=full --track-origins=yes ./Server 1700
 
So you probably do not have a leak, probably some of your containers grows too much.
 
No. I'm sure in 100% that's memory leak
each time I call new and delete the memory usage grows
it should stay same as before calling new
or maybe I closed valgrind too fast, so it didn't detect it
lemme do it again
I hate valgrind slowness
 
What do you check memory usage with? Process memory usage is not indicative for memory leaks because free'd memory does not necessarily goes back to system.
If you create/delete object of same type infinitly memory usage will probably grow in the beginning but will stabilize.
 
int getKb() {
std::string line;
std::ifstream self("/proc/self/status");
int vmRSS = 0;
while (!self.eof()) {
std::getline(self, line, ':');
if (line == "VmRSS") {
self >> vmRSS;
}
std::getline(self, line);
}
return vmRSS;
}
 
4:35 PM
No multiline code here, sorry.
 
you asked me how i check the memory usage
this is how I check it
 
Sure, I see. How high does it grow?
 
@EuriPinhollow you can use std::integral_constant::value instead of perfect::wat
 
@milleniumbug yeah nwp pointed that out.
 
@EuriPinhollow it starts growing from 5MB to 30MB (or maybe much more, I stopped my program in this point)
 
4:40 PM
@SzymonMarczak this is not a 100% reason to suppose that there is memory leak. Program gets memory in brick and if you alloc/free blocks of memory in an unlucky way you may get unlimited memory usage at worst.
You should get really unlucky though.
 
I don't think so? It happens every time
 
It's not because of iteration, it's because of allocation sizes and order in which they are done.
 
maybe...
I know the reason why valgrind does not detect memory leak
2
Q: valgrind (memcheck) tool didnot detect memory-leak

user1358526I introduced memory errors with following piece of C code: #include <stdio.h> #include <stdlib.h> int main(int argc, char** argv){ int i; int *a = (int *)malloc(sizeof(int) * 10); if (!a) return -1; /*malloc failed*/ for (i = 0; i < 11; i++){ a[i] = i; } for (i = 0; i < 11; i++){ printf("a[%...

"And if the program never exits, valgrind thinks you might still want to deallocate it later."
 
Cannot say much more that that without seeing a minimal working example.
@SzymonMarczak I c.
 
nwp
@SzymonMarczak the key to avoiding memory leaks is to never allocate memory
 
4:48 PM
insightful
 
nwp
@SzymonMarczak it doesn't because typical implementations never return memory to the OS, so your process' memory usage will never go down
though if you allocate and deallocate in a loop it should stay constant eventually
 
@nwp I see
@nwp But I doesn't even stay constat
it GROWS
I need to create game actor (by new of course). I perform some actions on it, and then I call delete. Each time I do this, the memory grows.
 
nwp
@SzymonMarczak well, don't use new then. Problem solved.
 
I'd try to replace direct calls to new with containers and smart pointers gradually
or your regular automatic variables
 
@nwp But what if I need only one instance of my game actor? I don't wanna copy instances
 
4:52 PM
std::unique_ptr
 
yeah, I know about that
 
std::make_unique
 
I know about that too
;-)
 
(to avoid actually typing out new)
 
std::make_unique prevents memory leak
 
nwp
4:53 PM
@SzymonMarczak then you delete the copy constructor
 
@nwp Or can I move references from one vector to second vector?
 
unique_ptrs, yes
 
nwp
@SzymonMarczak not sure what you mean by references here
 
@nwp I mean creating actor by GameActor actor(arg1, arg2 ,arg3)
without new
 
then no
only with std::unique_ptr
 
4:56 PM
@milleniumbug I see, the reference would die if actor goes out of scope
 
5:09 PM
how do I store unique ptr in vector?
this way?
std::vector<std::unique_ptr<...>> vec;
??
 
I have a union member with non-trivial constructor. Currently I'm initializing it via placement new, as suggested everywhere I look. But I wonder if it's legal to initialize it in constructor's initialization list instead?
 
@nwp std::vector<std::unique_ptr<...>> vec; is this good way?
 
@SzymonMarczak yes, though you'll have to remember that unique_ptr is non-copyable, so use emplace_back() instead push_back() (also couple other quirks)
 
@Xirdus can I move unique ptrs between vectors?
 
@SzymonMarczak yes, although I forgot exactly how :3
 
5:15 PM
haha
@Xirdus is that how?
dest.push_back(std::move(src[i]));
src.erase(src.begin()+i);
 
5:38 PM
@nwp @milleniumbug how can I proper delete element in vector which is created by new?
for (Entity *e : removals) {
delete e;
}

removals.clear();
is that good?
 
nwp
@SzymonMarczak not good because you used new, but it would be correct if you had to use new
 
@nwp what do you mean by that? I can't undersand your sentence. Should I use unique_ptr ?
 
nwp
@Xirdus you can actually use push_back, but yeah, attempting to copy creates somewhat difficult to read compilation errors
@SzymonMarczak ideally you would use vector<Entity>
 
@nwp I already do that
 
nwp
might not be possible if Entity is a base class and the vector is supposed to hold derived classes
@SzymonMarczak well, in that case you don't need new or delete
 
5:48 PM
@nwp I misunderstood you. I do vector<Entity*>
 
nwp
@SzymonMarczak why?
 
@nwp because I need only one instance of Entity and I don't wanna copy instances
 
nwp
not a good reason. Just make one Entity and delete the copy constructor
 
@nwp What do you mean by "make one Entity and delete the copy constructor"?
 
nwp
you make an Entity with Entity entity; and you prevent copying with Entity(const Entity &) = delete;
 
5:59 PM
YAY ASAN DETECTED MEMROY LEAK!!!
 
01:00 - 18:0018:00 - 22:00

« first day (290 days earlier)      last day (2554 days later) »