473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

cloning at the interface level?

I have a component which basically stores objects shared across other
parts of the application. the function accepts a pointer to an
interface. to make things a bit more clear...

// StorageComponent.h
.....
map< string, INode* m_resources;

// StorageComponent.cpp
....
StorageComponent::AddResource( string key, INode* resource, int
resourceSize )
{
// I need to 'new' the sourceObject here so that the memory is
moved to this class
// but, I only have an interface to work with

void* copy = new char[ resourceSize ];
memcopy( copy, resource, resourceSize );

m_resources[ key ] = (INode*) copy;
}

Does anyone know of a better way to do this? is the way this is done
post any problems. I do use stl, and to my surprise, the object can
be cast back to its derived type and everything translates ok.

Thanks very much for any assistance!
-Velik
Jul 31 '08 #1
6 1101
Do******@gmail.com wrote:
I have a component which basically stores objects shared across other
parts of the application. the function accepts a pointer to an
interface. to make things a bit more clear...

// StorageComponent.h
....
map< string, INode* m_resources;

// StorageComponent.cpp
...
StorageComponent::AddResource( string key, INode* resource, int
resourceSize )
{
// I need to 'new' the sourceObject here so that the memory is
moved to this class
// but, I only have an interface to work with

void* copy = new char[ resourceSize ];
memcopy( copy, resource, resourceSize );

m_resources[ key ] = (INode*) copy;
}

Does anyone know of a better way to do this? is the way this is done
post any problems. I do use stl, and to my surprise, the object can
be cast back to its derived type and everything translates ok.
Couple of things:

1. Are you intending to copy the resource, or transfer ownership?
2. Are resources copyable?
3. If resoures are copyable, and your intent is to copy the resource,
rather than transfer ownership, your best bet for this sort of thing
is to add a virtual Clone() method to INode. (See the FAQ).
4. Odds are that memcpy won't work properly given that you have
virtual functions.
Jul 31 '08 #2
Actually I just want to transfer ownership. I have a file loading
subsystem, and a resource storage subsystem ( these are separate dll's
if it matters ). the data is instantiated within the loader, and
should be transferred to storage. I actually tried to virtual clone
method yesterday, and I could have sworn it didnt work, I just tried
it again, and works perfectly : ) But, is there a way to say some
memory belongs to another process?

Thanks!
Jul 31 '08 #3
On 2008-08-01 00:54, Do******@gmail.com wrote:

Please quote the text you are replying to.
Actually I just want to transfer ownership. I have a file loading
subsystem, and a resource storage subsystem ( these are separate dll's
if it matters ). the data is instantiated within the loader, and
should be transferred to storage. I actually tried to virtual clone
method yesterday, and I could have sworn it didnt work, I just tried
it again, and works perfectly : ) But, is there a way to say some
memory belongs to another process?
If all you need to do is to transfer ownership then simply storing the
pointer might work, note though that you might not be able to deallocate
the memory they point to in any other DLL than the one you allocated it
it, but that could be solved by exporting a function you can call. The
advantage of such a scheme is that you do not have to pay the overhead
of copying all the INode objects.

Note that different DLLs are not the same thing as different processes,
(and that neither of those things are on topic in this group, a Windows
group might be a better place). You might however be able to create a
third, shared heap in which both DLLs may allocate and deallocate.

--
Erik Wikström
Jul 31 '08 #4
On Aug 1, 1:12 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-08-01 00:54, DomoC...@gmail.com wrote:
Please quote the text you are replying to.
Actually I just want to transfer ownership. I have a file
loading subsystem, and a resource storage subsystem ( these
are separate dll's if it matters ). the data is
instantiated within the loader, and should be transferred to
storage. I actually tried to virtual clone method
yesterday, and I could have sworn it didnt work, I just
tried it again, and works perfectly : ) But, is there a
way to say some memory belongs to another process?
If all you need to do is to transfer ownership then simply
storing the pointer might work, note though that you might not
be able to deallocate the memory they point to in any other
DLL than the one you allocated it it, but that could be solved
by exporting a function you can call. The advantage of such a
scheme is that you do not have to pay the overhead of copying
all the INode objects.
Well, the standard doesn't say anything about DLL's, so
technically, it's implementation defined. But in practice, I've
never had any problems with it (although you have to be careful
how you link under Windows).
Note that different DLLs are not the same thing as different
processes, (and that neither of those things are on topic in
this group, a Windows group might be a better place). You
might however be able to create a third, shared heap in which
both DLLs may allocate and deallocate.
According to the collegues here who develope under Windows,
there's a simple way to make malloc (and thus new) use a shared
heap, at link time. (I think it involves linking against a C
runtime which puts malloc/free in a shared library, but I'm not
too sure.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 1 '08 #5
James Kanze <ja*********@gmail.comkirjutas:
On Aug 1, 1:12 am, Erik Wikström <Erik-wikst...@telia.comwrote:
[...]
>might however be able to create a third, shared heap in which
both DLLs may allocate and deallocate.

According to the collegues here who develope under Windows,
there's a simple way to make malloc (and thus new) use a shared
heap, at link time. (I think it involves linking against a C
runtime which puts malloc/free in a shared library, but I'm not
too sure.)
Yes, it's quite easy to do (use DLL version of runtime library, this is
default in later MSVC versions), however, this is quite fragile. One can
really do this only if one has source code of all libraries and can
recompile everything to use different version of runtime library when
needed (for Debug build, or when upgrading to the next compiler version,
for example).

If there is a library which is distributed as a binary DLL, then the
simple approach cannot work in principle (unless the vendor provides a
DLL for each combination of compiler and runtime library in existance,
which is not so simple any more ;-). So if the library allocates any
dynamic objects by new and hands them out to the client, it has to ensure
that the client would be able to delete them properly. The library can
use per-class operator new/operator delete to achive this transparently.

For exposing STL objects from such a library one might think about using
custom allocators, but this will not work anyway as the client code might
use a different STL implementation. So basically a binary DLL library
should not use any STL in its interface (like std::string, which is
really pity).

Regards
Paavo


Aug 5 '08 #6
On Aug 5, 8:18 am, Paavo Helde <nob...@ebi.eewrote:
James Kanze <james.ka...@gmail.comkirjutas:
On Aug 1, 1:12 am, Erik Wikström <Erik-wikst...@telia.comwrote:
[...]
might however be able to create a third, shared heap in which
both DLLs may allocate and deallocate.
According to the collegues here who develope under Windows,
there's a simple way to make malloc (and thus new) use a shared
heap, at link time. (I think it involves linking against a C
runtime which puts malloc/free in a shared library, but I'm not
too sure.)
Yes, it's quite easy to do (use DLL version of runtime
library, this is default in later MSVC versions), however,
this is quite fragile. One can really do this only if one has
source code of all libraries and can recompile everything to
use different version of runtime library when needed (for
Debug build, or when upgrading to the next compiler version,
for example).
If there is a library which is distributed as a binary DLL,
then the simple approach cannot work in principle (unless the
vendor provides a DLL for each combination of compiler and
runtime library in existance, which is not so simple any more
;-). So if the library allocates any dynamic objects by new
and hands them out to the client, it has to ensure that the
client would be able to delete them properly. The library can
use per-class operator new/operator delete to achive this
transparently.
Obviously, if the library furnisher goes out of their way to
make life difficult for you, then life will be more difficult.
My response to that would be to only use libraries developed by
competent teams. (But I know, you don't always have a choice.)
For exposing STL objects from such a library one might think
about using custom allocators, but this will not work anyway
as the client code might use a different STL implementation.
So basically a binary DLL library should not use any STL in
its interface (like std::string, which is really pity).
Excuse me, but the STL is part of the compiler, so there can
only be one. What can make a difference (and not just with the
STL) is compiler options; that's a problem pretty much
everywhere. Some compiler options affect binary compatibility,
and you have to use the same ones in every component you expect
to link together. There is, regretfully, no realy good solution
for this.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 5 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Tom | last post by:
I've a problem. I want to clone an object having a list of other objects (and so on :/). Do you know any other way than ICloneable.Clone() implementation for all classes in the way? Help..
1
by: oDDskOOL | last post by:
I realized today that the Hashtable.Clone only produces a shallow copy... that makes me go mad that M$ doesn't even provide a deep copy ctor for the Hashtable class ! mighty tech ducks might...
3
by: AVL | last post by:
Hi, I've a query in cloning. How cloning is different from creating a new instance of an object.? I suppose cloning also creates a new object and copies the exisitng object's data. Where and when...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
52
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
3
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
1
by: Dan Dorey | last post by:
I've implemented the ICloneable interface on one of my class. I've written this simple code in two different ways and I think both should work but it's not the case and I'm curious to understand...
6
by: greenxiar | last post by:
How to get the function from "rewrited interface implement"? interface I { int Value { get; } } class A : I { int I.Value { get { return 1; } } } class B : I { int I.Value { get { return 2;...
0
by: Chris | last post by:
Hi All. I'm cloning a treenode, like so: TreeNode trNewTemp = new TreeNode(); trNewTemp = ((((ICloneable)trTempNode).Clone()) as TreeNode); Now the cloning works- trNewTemp has the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.