Metro style apps written in JavaScript or C++, do they load the CLR? - clr

If not, then does WinRT have its own Garbage Collector?
I ask this because I read this: "There's no need to manage the lifetime of underlying object. Windows releases the object when you're finished with the last of its class instances that you've activated." from MSDN.

They don't. WinRT doesn't use a garbage collector. Memory is managed with reference counting, IUnknown::AddRef() and IUnknown::Release(). Just like COM. And no, it isn't Windows that takes care of the counting, it is the language runtime support library. Javascript always used reference counting, C++ gets it from the C++/CX language extensions or by using smart pointer classes.

Related

How to Choose Sharemem vs SimpleSharemem in 64-bit XE2

I am having trouble get a clear picture of the advantages/disadvantages of using Sharemem vs SimpleSharemem to pass strings between a main program and DLLs.
Here is the background for why I am looking into this: My main program is developed in XE2 64-bit (although I may upgrade to XE7) and the DLLs will either be developed with XE2 (or higher) or FPC 64-bit (the ideal scenario). The main program and the DLLs need to be able to pass strings contained in records. Ideally I would like to have the DLLs developed by people who may not have Delphi and it appears as though FPC supports Sharemem but not SimpleSharemem.
Are there performance differences between Sharemem vs SimpleSharemem? Are there other reasons to prefer one vs the other in the scenario I describe (aside from the apparent FPC support for Sharemem)?
Thank you!
Sharemem is the old way of sharing memory managers. It relies on a DLL being deployed alongside your application. SimpleSharemem is designed to work with FastMM which handles sharing in a different way from Sharemem. So for modern versions of Delphi that use FastMM as their memory manager, use SimpleSharemem.
Shared memory managers allow you to allocate memory in one module and deallocate in another, using the standard language heap functions like GetMem, New etc.
However, you are attempting rather more than this. You are hoping to share a memory manager between modules compiled by different compilers. Specifically Delphi and FPC. Delphi's ShareMem is designed to allow sharing between modules compiled in Delphi. Likewise FPC's ShareMem is designed to allow two FPC compiled modules to share memory manager. There is no support for sharing between Delphi and FPC modules.
Even further, you are hoping to be able to pass string objects between modules. This requires the implementation of the string object to be compatible across the interop boundary. That might be the case for Delphi's UnicodeString for XE2 and XE7 modules, but if so it is only by chance. There is no guarantee of that. Future releases of Delphi may change the implementation. And as for mixing Delphi and FPC strings, there's no reason to believe that they can be mixed. I doubt they can.
So my advise is to stop trying to use shared memory managers and stop trying to pass native language strings between modules compiled with different languages.
In order to pass text between modules in a mixed compiler environment you need to use valid types for binary interop. Typical approaches include:
Passing null-terminated C strings, PWideChar. Trivial when passing text in. When passing text out this becomes more difficult because you may need callee to allocate but caller to deallocate. Either export a deallocator or allocate on a shared heap, e.g. the COM heap.
Use the COM BSTR type. In both Delphi and FPC this is wrapped by WideString. This type is designed for binary interop.
My choice would be to use WideString.

What are managed types? Are they specific to Delphi? Are they specific to Windows?

Summarization:
Please check the knowledgeable comments below.
==============================================================
I have seen the term of managed types mentioned in quite a few stackoverflow Delphi topics. For example, it is mentioned in topics of correctly initializing/finalizing. However, when I google managed types, it seems most links are related to C++, or .NET. For example, see the MSDN page. Could some one help to comment what are managed types defined in Delphi? Given that Delphi for POSIX/MacOS is being born, are managed types specific to Windows? Thanks for your effort and time in advance!
PS: Topics of correctly initializing/finalizing:
Which variables are initialized when in Delphi?
Are delphi variables initialized with a value by default?
How should I free an array of objects in a Delphi 7 destructor?
In Delphi 2009 do I need to free variant arrays?
In the context of Delphi, managed types are those types for whom the Delphi Compiler automatically generates lifecycle management code. This includes:
Strings
Open Dynamic Arrays
Records containing other managed types
Interfaces
(later edit) Anonymous methods
(later edit) Variants
Because managed types in the Delphi context are defined in terms of what the Delphi compiler generates, they're delphi-specific.
In the .NET world the developer doesn't need to manage the lifecycle of allocated memory because .NET provides an automatic mechanism for doing this: The Garbage Collector. But .NET includes the ability to work with things outside the CLR (example: using native DLL's that don't target the CLR). That code is usually called unamanged and unsafe.
In the context of .NET managed relates to what the CLR automatically manages, so that's .NET specific term!
See Barry Kelly's answer to a releated thread.
Since managed types are a language feature there shouldn't be significant changes on Mac OS et al.

Fastmm4 and leaking handles

I have just used FastMM4 to detect leaks. I did not realise our application was using a DLL which was leaking event handles and so I fixed any leaks reported by FastMM4 but not the handles as it was not reported.
My questions are, would have FastMM4 have reported the leaking event handles? Would this require me to rebuild the dlls with FastMM4 included? I also heard someone mention ShareMM, do I need to add that?
I am using Delphi2007, which I think is using the borland memory manager and if so, should I replace it with the fastMM4 one? What are the steps to do that?
Sorry for asking so many questions, I am looking at delphi after a few years of doing .net development.
JD.
No. FastMM is a memory manager, and it can only report leaks of memory that the application allocated via FastMM. Handles are opaque references to system objects that are allocated by Windows, so FastMM can't track them, and neither can any other Delphi memory manager.
And this isn't really a Delphi vs. .NET thing either, since .NET's garbage collection couldn't have solved this problem any better than FastMM can. Handles are non-memory resources and you have to keep them from leaking the same way you would in .NET: make sure that anything that allocates one releases it when you're done with it.
Do you know what type of handle you're leaking? If it's something less common than the ubiquitous HWND, that would be a good starting point in tracking down the problem: find where you're allocating that type of handle.
As for your other question, about Delphi 2007, it comes with FastMM built in, not the old BorlandMM. But it's sort of a basic version. For access to the FullDebugMode functionality, you need to download FastMM from SourceForge and add it at the top of your uses list and rebuild with the FullDebugMode compiler define on.

Why isn't operator overloading available for classes in Delphi?

I've always wondered about this for a bit, but why is operator overloading not available for classes in Delphi?
I remember reading an answer once while on the run, and it said it would come in conflict with something, but I can't remember much. As far as I can tell, only the implicit operator may cause a bit of problems since classes are stored on the heap and assigning is actually a copy of the heap address (basically copying pointers).
Close. It's because objects are reference types and the memory is managed manually. So if you said myResult := myObject1 + myObject2 + myObject3;, you'd have to create an intermediate object somewhere in there, and there's no code to free it, so you get memory leaks.
You can have operator overloading for classes, but only for the NextGen compiler where classes use ARC.
See: http://blog.marcocantu.com/blog/class_operators_delphi.html
This was introduced in XE5, see: List of Delphi language features and version in which they were introduced/deprecated
Mason Wheeler says that it's impossible because intermediate object management.
But according to Embarcadero documentation, class operators where possible with Delphi 2009.
RAD Studio 2009 - Operator Overloading
New Delphi language features since Delphi 7

How to pass and return objects to and from a DLL?

I need to return objects from a DLL made in Delphi, to an app made in Delphi, too. The objective is to do a subsystem that can be modify in the future without to modify the main app. So, I imagine developing the subsystem in a DLL is a (good??) idea... i am programming in Windows XP, Delphi 7. I did read DLLs only return basic data type, but there must to be a way to do that...
Best regards.
I prefer to apply COM to such a model, which allows you to create external "objects" which you then can direct from within your application. Creating COM objects in Delphi is extremely simple, use your ActiveX creation methods to create an ActiveX library and then create COM objects. You then use the interface unit in your main application, and when you CoCreate an instance of the object it loads the appropriate DLL. The only tricky part of this is that your com objects must be registered on the system to function properly... which in the Win7/Vista world requires elevated access... although once this is done, it is seamless.
The BEST way is to use a COM wrapper as suggested by skamradt.
It is possible, but not a good idea to pass object references as pointers to DLL's. Refer particularly to Peter Haas's comments.
If you do pass an object from a Delphi DLL to a Delphi app you will have the following problems:
You must use the same version of Delphi for the app and DLL.
Both app and DLL MUST have the same implementation of the object - or at least identical layout of all fields in the class - OK if you are using standard objects such as TStringList.
You should use shared memory or runtime packages, otherwise you will get weird access violations.
I have had to maintain code where this was done - it was nightmarish as you couldn't change classes without a lot of re-compiling.
You can use interfaces and most of your problems, wrt compiler/rtl versions or even other languages just go away.
Delphi's interfaces are always IUnknown-compatible, which makes them compatible with most OO-enabled languages on Windows.
One thing to keep in mind, though: Don't use AnyString, stick to WideString, which is the Stringtype used by COM.
A platform- and language independent way could be to exchange serialized objects.
It has a performance impact, but it has advantages too: the DLL works without modifications with other languages and platforms like .Net or Java (via JNA Java Native Access). It does not depend on any special features of the operating system so it can as well be used on Linux or MacOS if you compile the library with Free Pascal.
For the serialization, you could use JSON or XML. There are open source libraries for Delphi, for example SuperObject and OmniXML.

Resources