Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Pointer nightmare with MapViewOfFile

Posted on 2001-07-05
Medium Priority
Last Modified: 2013-11-20
I am using shared memory to share objects between a dll and another application.
I must use strings as a data member of the instance of a class, in the shared memory.
I use the CreateFileMapping/MapViewOfFile method, to share the memory. It works fine, with basic data type members.
But with strings, I have problems. First I tried to represent my strings as CString object.
It wasn't good, so I tried the std:string stuff.
But Visual C detected memory leaks after running. But none of the strings were declared dinamically!!!!
And only the strings stayed in the memory.
This happens only, if they are in the shared memory.
So I tried char *. It worked at the "server" app, I could allocate char arrays, I could free them, fine.
But when the dll tried to read from the memory, where the char pointer pointed, not the right data was read.
It got the pointer, but it read wrong data.
How can I solve any of these problems?
Or some method to represent character arrays (or string),
that works between the dll and the app?
I have no idea, where to start now. :((
Question by:holdkoros
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Expert Comment

ID: 6256629
If you want to share the data using e.g. a memory mapped file, you'll have to take care that the data you want to share is actually stored in the shared area - if you store a pointer in there that points to a string valid in the 1st app, it'll point to some random memory are in the 2nd app.

See e.g. ('Managing Memory-Mapped Files in Win32') ('MMFILE - Class for Memory-Mapped Files')

Expert Comment

ID: 6256633

Pointers are process specific, you can't pass a pointer to another process that easy

(Your problems with CString and std::string is, they use pointers, too)

Solution a) easy
Use a plain char[maxlen] array in your shared memory structure. Only disadvantage: you need to know a maximum size ahead of time, and you might be wasting space.

LVL 12

Expert Comment

ID: 6256681
As mentoned above, actual data (not pointers and referencies) MUST be placed in the shared memory.
To avoid memory wast you can write "string stream" into the shared memory.
for example first 4 byte in the shared pool will be count of strings;
after that first string started and ended by 0 after first string second will be placed and so on until last string will be placed in to the memory.
or you can provide offsets to the next at the each string entry for speeding.

Accepted Solution

peterchen092700 earned 900 total points
ID: 6256700
More complex Solutions are

b) ReadProcessMemory
use char *'s, allocated at one application (A), share both the process ID and the char * in the share. Wto read the string, use OpenProcess, and ReadProcessMemory. (You can wrap this in a class)

disadvantage: ReadProcessMemory requires debugging priviledges (not available e.g. in normal user/guest accounts). And the string is no longer available if (A) terminates

c) extend a) with a bit more work
reserve some "string pool" in the MMF (e.g. a few 100k, or how much you might need at maximum for all strings)
create (or re-use) some allocation manager for this area, where the offset in the pool is treated as "mmf-pointer". Write a little string-helper class that uses mmf-pointers.
(you couild also provide a custom std::allocator class, so you can use std::string)

d) use the MMF to set up other means of data exchange (WM_COPYDATA, Pipes, DDE, COM, File)
Wouldn't recommend this, as ist makes the MMF somewhat useless. I _could_ imagin using COM, but seems somehow strange anyway.

e) Helper Thread
Each participating application gets a "helper thread" that is responsible for delivering strings on demand.
You set up an exchange area large enough for _one_ string. Basic idea: when a client needs a string from another process, the "exchange area" is locked, helper thread of the source copies string into exchange area, client reads string from there (makes a copy), and unlocks exchange area.

The IPC synchronnization is a bit tricky to set up, but it could make the whole thing damn effective.

P.S. If you're going to implement c) or e), I'd be darn interested...


Author Comment

ID: 6259688
Thanks for the comments!

So the simple char[] solution is good for me,
I will ignore the wasted bytes. ;)

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question