Re: Whats the correct way to pass a D array type to a win32 api function wanting a buffer?

2017-07-12 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 13 July 2017 at 01:15:46 UTC, FoxyBrown wrote:

auto EnumServices()


I wouldn't use auto here. The reason you get mismatch types on 
return here since you don't return consistent types inside.




ENUM_SERVICE_STATUS_PROCESS[5000] services;


Are you sure you are getting the same A vs W version there? You 
explicitly call the A version of the function, but do not specify 
it here.



auto s = services[i].lpServiceName;
writeln(*s);


Like the other user above said, you should be treating that like 
a C string anyway. Use printf or fromStringz or slice it 
yourself... just make sure you tend to char vs wchar like above.



return services;


That's kinda hideous, returning the entire buffer by value. I do 
NOT recommend you attempt to slice and dup though, since the 
win32 function uses space at the end of the buffer to store the 
strings referenced by the structs.


Ideally, you'd avoid returning this thing at all and just use it 
locally. Perhaps pass a callback function/delegate that takes 
each item as you iterate through.


Re: Whats the correct way to pass a D array type to a win32 api function wanting a buffer?

2017-07-12 Thread Nicholas Wilson via Digitalmars-d-learn

On Thursday, 13 July 2017 at 01:15:46 UTC, FoxyBrown wrote:
Everything I do results in some problem, I've tried malloc but 
then converting the strings resulted in my program becoming 
corrupted.



Heres the code:

auto EnumServices()
{

auto schSCManager = OpenSCManager(null, null, 
SC_MANAGER_ALL_ACCESS);

if (NULL == schSCManager)
{
print("OpenSCManager failed (%d)\n", GetLastError());
return null; // Why can't we return a null? Surely we 
don't have to cast a null in to a typeof null?

}

import core.stdc.stdlib;

DWORD dwBytesNeeded, dwCount, lpResumeHandle, resume;

	auto servicesType = (SERVICE_DRIVER | 
SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER | 
SERVICE_WIN32 | SERVICE_WIN32_OWN_PROCESS | 
SERVICE_WIN32_SHARE_PROCESS);




ENUM_SERVICE_STATUS_PROCESS[5000] services;
	auto res = SVC.EnumServicesStatusExA(schSCManager, 
SC_ENUM_TYPE.SC_ENUM_PROCESS_INFO, servicesType, 
SERVICE_STATE_ALL, cast(ubyte*)services.ptr, 
5000*ENUM_SERVICE_STATUS_PROCESS.sizeof, , 
, , cast(const(char)*)null);



for(int i = 0; i < dwCount; i++)
{
auto s = services[i].lpServiceName;
writeln(*s);
}


return services;
}


What is the signature of the function you are trying to call? 
(make sure its correct :))

Allocating 5000 services may blow your stack.
you can just use `services.sizeof` instead of 
`5000*ENUM_SERVICE_STATUS_PROCESS.sizeof`.
You are returning that massive static array, remember that static 
arrays are value types in D so that will get copied. Consider 
allocating the array and returning a slice of it.


Note also that `lpServiceName` is probably a `char*` so 
drefferencing will give you a char, not a string. Use 
std.string.fromStringz(?) for that.


Whats the correct way to pass a D array type to a win32 api function wanting a buffer?

2017-07-12 Thread FoxyBrown via Digitalmars-d-learn
Everything I do results in some problem, I've tried malloc but 
then converting the strings resulted in my program becoming 
corrupted.



Heres the code:

auto EnumServices()
{

auto schSCManager = OpenSCManager(null, null, 
SC_MANAGER_ALL_ACCESS);

if (NULL == schSCManager)
{
print("OpenSCManager failed (%d)\n", GetLastError());
return null; // Why can't we return a null? Surely we 
don't have to cast a null in to a typeof null?

}

import core.stdc.stdlib;

DWORD dwBytesNeeded, dwCount, lpResumeHandle, resume;

	auto servicesType = (SERVICE_DRIVER | SERVICE_FILE_SYSTEM_DRIVER 
| SERVICE_KERNEL_DRIVER | SERVICE_WIN32 | 
SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS);




ENUM_SERVICE_STATUS_PROCESS[5000] services;
	auto res = SVC.EnumServicesStatusExA(schSCManager, 
SC_ENUM_TYPE.SC_ENUM_PROCESS_INFO, servicesType, 
SERVICE_STATE_ALL, cast(ubyte*)services.ptr, 
5000*ENUM_SERVICE_STATUS_PROCESS.sizeof, , 
, , cast(const(char)*)null);



for(int i = 0; i < dwCount; i++)
{
auto s = services[i].lpServiceName;
writeln(*s);
}


return services;
}


Re: 2D game physics, macOS

2017-07-12 Thread Joel via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 12:05:33 UTC, Jacob Carlborg wrote:

On 2017-07-12 12:18, Joel wrote:

Is there a 2D physics library I can use on macOS, with D?

I already use a multimedia library for graphics, sound and 
input.


Box2D [1] perhaps. I think I've seen bindings for it, somewhere.

[1] http://box2d.org


It doesn't look like there's any thing I can use. I've come 
across: dbox, dchip, and blaze.  Blaze is dsource. dbox is alpha 
and hasn't been updated for 3 years. dchip [1] hasn't been 
updated for 2 years and doesn't compile (that's with out using 
any thing, just import and dub dependency).


[1] https://github.com/d-gamedev-team/dchip


Re: Help me fix my compiler

2017-07-12 Thread Andrew Edwards via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 12:05:17 UTC, Namal wrote:

Hello,

I used the Install Script command line to install the newest 
dmd compiler (Ubuntu 16.04.2 LTS). Now I have to type 'source 
~/dlang/dmd-2.074.1/activate' before I can use it and it is 
also not show in the software center like it used to be. How 
can I fix it or how can I remove it?


Thx


You are using the script directly from dlang.org, as such you can 
manipulate (manage) your installed compilers directly from the 
shell. And not just DMD, it also manages LDC and GDC.


dmd|gdc|ldc   latest version of a compiler
dmd|gdc|ldc- specific version of a compiler (e.g. 
dmd-2.071.1, ldc-1.1.0-beta2)

dmd|ldc-beta  latest beta version of a compiler
dmd-nightly   latest dmd nightly
dmd-2016-08-08specific dmd nightly

To uninstall any compiler previously installed via this script 
simply type ~/dlang/install.sh uninstall 


install.sh uninstall dmd
install.sh uninstall dmd-2.071.1
install.sh uninstall ldc-1.1.0-beta2

To install a different version of the compiler type 
~/dlang/install.sh uninstall 


install.sh install dmd
install.sh install dmd-2.071.1
install.sh install ldc-1.1.0-beta2

To make the 'source ~/dlang/dmd-2.074.1/activate' persistent, 
simply store it in ~/.bash_profile. If this file does not already 
exist, simply create it.


Best of luck.
Andrew


Re: Static array with parameter based size?

2017-07-12 Thread Jack Applegame via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 05:45:13 UTC, Miguel L wrote:
Also what is it possible in D to write a function that accepts 
an static array of any size?


void foo(size_t N)(ref int[N] arr) {
...
}

int[10] arr;
foo(arr);



Re: Foreign threads in D code.

2017-07-12 Thread Igor Shirkalin via Digitalmars-d-learn
On Wednesday, 12 July 2017 at 09:49:32 UTC, Guillaume Piolat 
wrote:

On Tuesday, 11 July 2017 at 22:59:42 UTC, Igor Shirkalin wrote:

[...]


--
  Biotronic


Thanks for very useful information!


Just one small note.
If you don't know the foreign thread lifetime, it's cleaner to 
detach it from the runtime upon exit.


Else may fall in the following scenario.
1. you register thread A
2. thread A is destroyed later on, in the C++ code
3. another thread B come into your callback and allocate. The 
GC triggers and try to pause a non-existing thread A.


This is important note. Yes, usually the lifetime of foreign 
thread is unknown.


You, guys, helped me a lot.



Anything like quote?

2017-07-12 Thread Jean-Louis Leroy via Digitalmars-d-learn
I want to create a string while making sure it qualifies as an 
identifier. Like this:


struct quote
{
  static @property string opDispatch(string str)() { return str; }
}

unittest
{
  assert(quote.foo == "foo");
}

Does it already exist somewhere in the language or the library?

J-L



Re: Having a strange issue with std.net.curl.HTTP as a struct dependency

2017-07-12 Thread crimaniak via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 13:47:06 UTC, Adam D. Ruppe wrote:


This tells me the problem is in the collection order at the end 
of the program.

...
So I'd say the answer is prolly to keep HTTP away from the GC. 
Manually free its arrays, or keep them outside arrays in the 
first place.


I'd try also calling GC.collect() manually before the exit.



Re: Struct Constructor Lazy

2017-07-12 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 11:18:08 UTC, Biotronic wrote:

The traditional solution is static opCall:



That's bug city... the dummy argument is better, or a named 
static factory function.


Foo foo = Foo;
Foo foo = Foo();

those are different with static opCall. It also conflicts with 
constructors and non-static opCall.


Re: Struct Constructor Lazy

2017-07-12 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 11:57:33 UTC, Biotronic wrote:

Debug = no optimization.


That's not really true, you can have an optimized debug build.

dmd's -debug, -g, and -O switches are all independent. -debug 
turns on blocks labeled with the `debug` switch in code. -g adds 
info for a debugger to read. -O turns on optimizations.


You can use any combination of those.


Looking at the generated assembly in a debug build is worthless.


I don't agree - looking at the generated assembly would put to 
rest all these questions.


But before doing that, you should show that the performance is 
actually bad. I can't imagine sending a zero argument (whether in 
a register or pushed to the stack) would ever be a serious 
problem.


Re: Cannot dup an associative array but why?

2017-07-12 Thread Jean-Louis Leroy via Digitalmars-d-learn

On Tuesday, 11 July 2017 at 21:23:28 UTC, Ali Çehreli wrote:
Default template and function arguments are resolved at 
instantiation site, which means __MODULE__ would resolve 
automatically to the caller's module. For example, if you have 
this module:


__MODULE__ is a string so I cannot pass it to 
__traits(allMembers). I worked around it via a string mixin that 
creates an alias for the module:


template RegisterMethods()
{
  import std.array;
  mixin("alias MODULE = " ~ __MODULE__ ~ ";");
  auto RegisterMethods() {
string[] s;
foreach (m; __traits(allMembers, MODULE)) {
  // do stuff
}
return join(s, "");
  }
}

mixin(RegisterMethods);

I wonder if there is a better way.

Also, I am using the outer string mixin (as opposed to a plain 
mixin) because I will need to produce a series of aliases:


mixin(RegisterMethods);
// injects:
// alias add = Method!("add", Matrix, virtual!Matrix, 
virtual!Matrix).dispatcher
// alias add = Method!("add", Matrix, virtual!Matrix, 
virtual!Matrix).discriminator;

// alias kick = Method!("kick", Animal).dispatcher
// alias kick = Method!("kick", Animal).discriminator
// etc

The only way I can see is with a string mixin.

I thought about the new syntax since yesterday, and hit some 
problems and some solutions. At this point maybe we should 
continue that discussion elsewhere than the Learn forum...


Re: Struct Constructor Lazy

2017-07-12 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 11:00:54 UTC, Jiyan wrote:
when A(0) is called I would want here optimal performance, so 
there doesnt even need to be a value pushed on the stack (i=0), 
what would be like having a constructor with zero arguments (i 
is never used!).


This is so, so irrelevant. Even if it isn't optimized out (which 
it probably is), pushing a zero to the stack is so extremely 
cheap that you'd probably not notice it, especially compared to 
reading something from a file.



Im pretty new to D, can somebody tell me how i would do this?
Is this(lazy int i){ ... a solution?


That's actually more expensive than just sending the zero!


Re: Having a strange issue with std.net.curl.HTTP as a struct dependency

2017-07-12 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 10 July 2017 at 00:10:36 UTC, NoBigDeal256 wrote:
The error goes away. The error also goes away if 
ThingA.arrayOfThingBs returns a single instance of ThingB 
instead of an array of ThingB.


This tells me the problem is in the collection order at the end 
of the program.


The HTTP inside the struct inside the array gets cleaned up by 
the garbage collector (aka GC) at program termination... and it 
might be doing that AFTER unloading curl, leading to the 
segfault, OR it could be running some GC operation in the 
destructor, leading to InvalidMemoryOperationError (calling a GC 
function while the GC is running is not allowed). Which you get 
is apparently dependent on which system you are on, but both are 
wrong.


If it isn't in the array, the destructor gets called at scope 
exit, which happens before program termination and outside the 
GC, so everything happens as it is supposed to.



HTTP's innards have a RefCounted implementation which has a 
destructor that calls GC.removeRange. I *think* that can do the 
invalid mem operation. And the implementation itself can do 
curl_shutdown, which is your potential segfault.



So I'd say the answer is prolly to keep HTTP away from the GC. 
Manually free its arrays, or keep them outside arrays in the 
first place.


Re: Function with static array as parameter

2017-07-12 Thread Nicholas Wilson via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 12:20:11 UTC, Miguel L wrote:
What is the best way in D to create a function that receives a 
static array of any length?


Do know that static arrays are passed by value in D, so passing a 
static array of a million elements will copy them...


There are two solutions to this:
1) pass the static array by reference. This will pass a pointer 
to the array and won't copy it although modifying it will modify 
the original. consider passing const ref if the function doesn't 
modify it. Although will still create its of template bloat.
2) pass a slice of the static array. this is essentially the same 
as the above, but you also pass the length of the array as well. 
This means it can accept a slice of a static array of any size 
(or any dynamic array) and since it isn't a template the compiler 
won't make lots of copies for different sized array. This is 
probably he better option.


Re: Static array with parameter based size?

2017-07-12 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 05:45:13 UTC, Miguel L wrote:

void f(int x)
{
int[] my_array;
my_array.length=x;

but I don't really need a dynamic array as length is not going 
to change inside f.


Then just don't change the length... this is a correct way to do 
it.


You could also allocate it with `alloca` or `malloc` depending on 
exact use.


I also sometimes like to just slice a static array:

```
void f(int x) {
   int[1000] buffer;
   int[] my_array = x < buffer.length ? buffer[0 .. x] : new 
int[](x);

}
```

Which gives the speed benefits of static without putting a size 
limit on it.


Just make sure you keep track of ownership with any of these 
strategies.


Also what is it possible in D to write a function that accepts 
an static array of any size?


Best option is to just accept a slice:

void f(int[] x);


and call it like so:

int[123] some_static_array;
f(some_static_array[]);


That will work on any size and typically give best performance. 
Again though, just use caution about ownership when using static 
arrays.


Re: Function with static array as parameter

2017-07-12 Thread Rene Zwanenburg via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 12:57:19 UTC, Rene Zwanenburg wrote:

On Wednesday, 12 July 2017 at 12:20:11 UTC, Miguel L wrote:
What is the best way in D to create a function that receives a 
static array of any length?


Templatize the array length:

void foo(size_t length)(int[length] arr)
{

}


That being said, this may lead to template bloat and excessive 
copying. What are you trying to do? Is there a reason you can't 
use a slice?


Re: Function with static array as parameter

2017-07-12 Thread Biotronic via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 12:20:11 UTC, Miguel L wrote:
What is the best way in D to create a function that receives a 
static array of any length?


You will need to use templates:

void foo(size_t N)(int[N] arr) {
}

--
  Biotronic


Re: Function with static array as parameter

2017-07-12 Thread Rene Zwanenburg via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 12:20:11 UTC, Miguel L wrote:
What is the best way in D to create a function that receives a 
static array of any length?


Templatize the array length:

void foo(size_t length)(int[length] arr)
{

}


Function with static array as parameter

2017-07-12 Thread Miguel L via Digitalmars-d-learn
What is the best way in D to create a function that receives a 
static array of any length?


Re: Foreign threads in D code.

2017-07-12 Thread Biotronic via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 12:08:35 UTC, Jacob Carlborg wrote:

On 2017-07-12 11:28, Biotronic wrote:


That's basically what I tried to say


It wasn't very clear to me at least.


Yeah, I see it in retrospect. "might collect memory that the 
thread is referencing on the stack or in non-GC memory" doesn't 
convey that the collected memory is only that which is allocated 
by the GC.


--
  Biotronic


Re: Foreign threads in D code.

2017-07-12 Thread Jacob Carlborg via Digitalmars-d-learn

On 2017-07-12 11:28, Biotronic wrote:

That's basically what I tried to say 


It wasn't very clear to me at least.

- the GC may collect memory *it has 
allocated* if the only pointers to it are in memory the GC doesn't scan 
(i.e. on the stack of an unregistered thread or in memory not allocated 
via the GC).


It will not collect memory allocated by other means, but that Foo* you 
got from D and are using in C++ might point to a Bar soon after the GC 
runs.


Yes, that can happen.

--
/Jacob Carlborg


Re: Struct Constructor Lazy

2017-07-12 Thread Biotronic via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 12:02:37 UTC, Jiyan wrote:

Thank you, one last question:
If i declare the parameter as ref i, then there shouldnt be any 
overhead wouldnt it?


Thanks :)


That would be basically the exact equivalent - instead of passing 
an int, you'll be passing a pointer.


--
  Biotronic


Re: 2D game physics, macOS

2017-07-12 Thread Jacob Carlborg via Digitalmars-d-learn

On 2017-07-12 12:18, Joel wrote:

Is there a 2D physics library I can use on macOS, with D?

I already use a multimedia library for graphics, sound and input.


Box2D [1] perhaps. I think I've seen bindings for it, somewhere.

[1] http://box2d.org

--
/Jacob Carlborg


Help me fix my compiler

2017-07-12 Thread Namal via Digitalmars-d-learn

Hello,

I used the Install Script command line to install the newest dmd 
compiler (Ubuntu 16.04.2 LTS). Now I have to type 'source 
~/dlang/dmd-2.074.1/activate' before I can use it and it is also 
not show in the software center like it used to be. How can I fix 
it or how can I remove it?


Thx


Re: Struct Constructor Lazy

2017-07-12 Thread Jiyan via Digitalmars-d-learn

Thank you, one last question:
If i declare the parameter as ref i, then there shouldnt be any 
overhead wouldnt it?


Thanks :)



Re: Struct Constructor Lazy

2017-07-12 Thread Biotronic via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 11:34:45 UTC, Jiyan wrote:

Hey,
yes i did but to be honest i used dmd in debug version.
The thing about the static one, is that it creates a local 
object A isnt that a performance issue itself - or am i wrong - 
im confused actually :P?


Debug = no optimization. Looking at the generated assembly in a 
debug build is worthless.


You raise a valid point. In a debug build, you're probably right 
- it will need to copy the temporary to the target. With 
optimizations enabled, NRVO[0] will populate the target directly, 
resulting in roughly the equivalent of this code:


struct A {
int field;
static void opCall(A* p) {
p.field = getDataFromFile("file.txt");
}
}

A a;
A();

If the function is inlined, the whole problem is of course moot. 
There are probably other optimizations that can interfere with 
what I've described.


--
  Biotronic

[0]: https://en.wikipedia.org/wiki/Return_value_optimization


Re: Struct Constructor Lazy

2017-07-12 Thread Jiyan via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 11:18:08 UTC, Biotronic wrote:

On Wednesday, 12 July 2017 at 11:00:54 UTC, Jiyan wrote:

[...]


The traditional solution is static opCall:

struct A {
int field;
static A opCall() {
A result;
result.field = getDataFromFile("file.txt");
return result;
}
}

A instance = A();

I believe I've heard this is frowned upon these days, but I 
don't know of a better solution.


For optimal speed you might also want to skip default 
initialization of result, by writing A result = void;.


I would be surprised if the optimizer wasn't able to optimize 
away the useless parameter though - have you looked at the 
generated assembly?


--
  Biotronic


Hey,
yes i did but to be honest i used dmd in debug version.
The thing about the static one, is that it creates a local object 
A isnt that a performance issue itself - or am i wrong - im 
confused actually :P?


Re: Struct Constructor Lazy

2017-07-12 Thread Biotronic via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 11:00:54 UTC, Jiyan wrote:

Hey there:)

i want to know whether the following is somehow possible:
structs dont have default constructors, i know so:

struct A
{
int field;
this(int i){field = getDataFromFile("file.txt");}
}

A instance = A(0);

Here comes my issue:
when A(0) is called I would want here optimal performance, so 
there doesnt even need to be a value pushed on the stack (i=0), 
what would be like having a constructor with zero arguments (i 
is never used!).

Im pretty new to D, can somebody tell me how i would do this?
Is this(lazy int i){ ... a solution?


The traditional solution is static opCall:

struct A {
int field;
static A opCall() {
A result;
result.field = getDataFromFile("file.txt");
return result;
}
}

A instance = A();

I believe I've heard this is frowned upon these days, but I don't 
know of a better solution.


For optimal speed you might also want to skip default 
initialization of result, by writing A result = void;.


I would be surprised if the optimizer wasn't able to optimize 
away the useless parameter though - have you looked at the 
generated assembly?


--
  Biotronic


Struct Constructor Lazy

2017-07-12 Thread Jiyan via Digitalmars-d-learn

Hey there:)

i want to know whether the following is somehow possible:
structs dont have default constructors, i know so:

struct A
{
int field;
this(int i){field = getDataFromFile("file.txt");}
}

A instance = A(0);

Here comes my issue:
when A(0) is called I would want here optimal performance, so 
there doesnt even need to be a value pushed on the stack (i=0), 
what would be like having a constructor with zero arguments (i is 
never used!).

Im pretty new to D, can somebody tell me how i would do this?
Is this(lazy int i){ ... a solution?




2D game physics, macOS

2017-07-12 Thread Joel via Digitalmars-d-learn

Is there a 2D physics library I can use on macOS, with D?

I already use a multimedia library for graphics, sound and input.


Re: Foreign threads in D code.

2017-07-12 Thread Guillaume Piolat via Digitalmars-d-learn

On Tuesday, 11 July 2017 at 22:59:42 UTC, Igor Shirkalin wrote:

On Tuesday, 11 July 2017 at 06:18:44 UTC, Biotronic wrote:

On Monday, 10 July 2017 at 20:03:32 UTC, Igor Shirkalin wrote:

[...]


If DRuntime is not made aware of the thread's existence, the 
thread will not be stopped by the GC, and the GC might collect 
memory that the thread is referencing on the stack or in 
non-GC memory. Anything allocated by the GC would still be 
scanned.


To inform DRuntime about your thread, you should call 
thread_attachThis:


https://dlang.org/phobos/core_thread.html#.thread_attachThis

As pointed out in the documentation of thread_attachThis, you 
might also want to call rt_moduleTlsCtor, to run thread local 
static constructors. Depending on your usage, this might not 
be necessary.


--
  Biotronic


Thanks for very useful information!


Just one small note.
If you don't know the foreign thread lifetime, it's cleaner to 
detach it from the runtime upon exit.


Else may fall in the following scenario.
1. you register thread A
2. thread A is destroyed later on, in the C++ code
3. another thread B come into your callback and allocate. The GC 
triggers and try to pause a non-existing thread A.


Re: Foreign threads in D code.

2017-07-12 Thread Biotronic via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 09:10:07 UTC, Jacob Carlborg wrote:

On 2017-07-11 08:18, Biotronic wrote:

If DRuntime is not made aware of the thread's existence, the 
thread will not be stopped by the GC, and the GC might collect 
memory that the thread is referencing on the stack or in 
non-GC memory.


Are you sure? Wouldn't that make malloc or any other custom 
allocators completely useless and the D GC would completely 
break the C standard library because it could collect memory 
allocated by the C standard library?


From "How Garbage Collection Works":

"5. Freeing all **GC** allocated memory that has no active 
pointers to it and do not need destructors to run" [1]. I added 
the emphasize on "GC".


From "Interfacing Garbage Collected Objects With Foreign Code"

"If the only pointer to an object is held outside of these 
areas, then the collector will miss it and free the memory. To 
avoid this from happening, either
* reallocate and copy the object using the foreign code's 
storage allocator or using the C runtime library's 
malloc/free." [2].


[1] http://dlang.org/spec/garbage.html#how_gc_works
[2] http://dlang.org/spec/garbage.html#gc_foreign_obj


That's basically what I tried to say - the GC may collect memory 
*it has allocated* if the only pointers to it are in memory the 
GC doesn't scan (i.e. on the stack of an unregistered thread or 
in memory not allocated via the GC).


It will not collect memory allocated by other means, but that 
Foo* you got from D and are using in C++ might point to a Bar 
soon after the GC runs.


--
  Biotronic


Re: Foreign threads in D code.

2017-07-12 Thread Jacob Carlborg via Digitalmars-d-learn

On 2017-07-11 08:18, Biotronic wrote:

If DRuntime is not made aware of the thread's existence, the thread will 
not be stopped by the GC, and the GC might collect memory that the 
thread is referencing on the stack or in non-GC memory.


Are you sure? Wouldn't that make malloc or any other custom allocators 
completely useless and the D GC would completely break the C standard 
library because it could collect memory allocated by the C standard library?


From "How Garbage Collection Works":

"5. Freeing all **GC** allocated memory that has no active pointers to 
it and do not need destructors to run" [1]. I added the emphasize on "GC".


From "Interfacing Garbage Collected Objects With Foreign Code"

"If the only pointer to an object is held outside of these areas, then 
the collector will miss it and free the memory. To avoid this from 
happening, either
* reallocate and copy the object using the foreign code's storage 
allocator or using the C runtime library's malloc/free." [2].


[1] http://dlang.org/spec/garbage.html#how_gc_works
[2] http://dlang.org/spec/garbage.html#gc_foreign_obj

--
/Jacob Carlborg


Re: Having a strange issue with std.net.curl.HTTP as a struct dependency

2017-07-12 Thread ketmar via Digitalmars-d-learn

ikod wrote:


On Wednesday, 12 July 2017 at 08:24:12 UTC, NoBigDeal256 wrote:

On Monday, 10 July 2017 at 20:55:19 UTC, ketmar wrote:

NoBigDeal256 wrote:

Do you happen to have a link to the bug report for this specific issue 
that I could look at? I looked at the known bugs list and couldn't 
find anything related to this.


nope. it is a kind of "known bug nobody bothered to report". ;-)


Okay, so I decided to ditch curl and switch over to dlang-requests, 
which doesn't depend on curl, and I'm running into this same issue 
again... I don't think this is a curl issue.


looks like circular dependency between thingA and thingB lead to problems 
on exit. this code fails on osx too.


yet at least with curl there was definitely a bug with "slightly too early 
curl shutdown".


Re: Having a strange issue with std.net.curl.HTTP as a struct dependency

2017-07-12 Thread ikod via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 08:24:12 UTC, NoBigDeal256 wrote:

On Monday, 10 July 2017 at 20:55:19 UTC, ketmar wrote:

NoBigDeal256 wrote:

Do you happen to have a link to the bug report for this 
specific issue that I could look at? I looked at the known 
bugs list and couldn't find anything related to this.


nope. it is a kind of "known bug nobody bothered to report". 
;-)


Okay, so I decided to ditch curl and switch over to 
dlang-requests, which doesn't depend on curl, and I'm running 
into this same issue again... I don't think this is a curl 
issue.


looks like circular dependency between thingA and thingB lead to 
problems on exit. this code fails on osx too.


Re: How to add authentificaion method to request?

2017-07-12 Thread Cym13 via Digitalmars-d-learn

On Wednesday, 12 July 2017 at 07:43:27 UTC, Suliman wrote:
Compiler require libssl32.dll for run dlang-request based app. 
Where I can get it?


I installed, OpenSSL, but can't find this lib in 
C:\OpenSSL-Win64


I don't use windows so it's nothing definitive but I'd bet that 
the "32" in libssl32.dll stands for 32bits while OpenSSL-Win64 is 
for 64bits. You need to get the 32bits version.


Re: Having a strange issue with std.net.curl.HTTP as a struct dependency

2017-07-12 Thread NoBigDeal256 via Digitalmars-d-learn

On Monday, 10 July 2017 at 20:55:19 UTC, ketmar wrote:

NoBigDeal256 wrote:

Do you happen to have a link to the bug report for this 
specific issue that I could look at? I looked at the known 
bugs list and couldn't find anything related to this.


nope. it is a kind of "known bug nobody bothered to report". ;-)


Okay, so I decided to ditch curl and switch over to 
dlang-requests, which doesn't depend on curl, and I'm running 
into this same issue again... I don't think this is a curl issue.


Re: How to add authentificaion method to request?

2017-07-12 Thread Suliman via Digitalmars-d-learn
Compiler require libssl32.dll for run dlang-request based app. 
Where I can get it?


I installed, OpenSSL, but can't find this lib in C:\OpenSSL-Win64