473,425 Members | 1,842 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,425 software developers and data experts.

Pointer corruption calling across a DLL boundary

I have a problem where I call a method in a C++ class with a pointer
which is a static member in that class. When I use a debugger, I see
the pointer having a certain value, but when I step into the method
(F11), the pointer has a different value and is not pointing at
anything valid.

The small program (complete source below) demonstrates what I am
trying to achieve. The small program works perfectly, but when I
incorporate this logic into a much larger program, I see the pointer
corruption happening. My workaround is that I use a file scope static
pointer declared with the same contents as the class static pointer,
in the style of the commented out workaround in the source code below.

This leaves some problems:
1. I can't reproduce the behaviour in a small program
2. I'm not in a position to put the Big Program source here, and
even if I could, you wouldn't read it
3. The differences between the small program and the Big Program are
a. ... "umm, the size".
b. the Big Program is coded as an older style Windows Service,
which I believe was migrated from Visual C++ ver 6.0
c. the Big Program version of "Nick" has a .DEF file, in debug
mode is generated dynamically, but in release mode is edited by hand,
putting in the mangled names of Nick::OneTwo and Nick::PrintTheList
4. There's SOMETHING about the Big Program that is screwing it up,
but what?

My question is what should I check for?

Nick Bishop
email replies ignored. Additional information below.

(Description of environment starts here)
I am using Visual Studio .NET, the original Enterprise Architect
version from 2002 (or rather it is using me).
This is being done on Windows 2000 Professional.
I created the solution (with TheConsole project) as a Win32 project,
and clicked Console Application in the wizard. I then added another
project (Nick) as a DLL project.

I therefore have one solution, and two projects, and a fully expanded
Solution Explorer for the Small Program looks like this:

Solution 'TheConsole' (2 projects)
- Nick
- Source Files
Nick.cpp
stdafx.cpp
- Header Files
Nick.h
stdafx.h
- Resource Files (with no files)
ReadMe.txt
- TheConsole (as startup project)
- Source Files
TheConsole.cpp
stdafx.cpp
- Header Files
stdafx.h
- Resource Files (with no files)
ReadMe.txt

(Source code starts here)
// ----- Nick.h -----------------------------------------------------
#ifndef NICK_H
#define NICK_H
#ifdef NICK_EXPORT
#define EXPORTTESTER_API __declspec(dllexport)
#else
#define EXPORTTESTER_API __declspec(dllimport)
#endif

class EXPORTTESTER_API Nick
{
public:
// This static pointer is intended to be a convenient, commonly used
list-of-strings
// that can be used by callers as a parameter to PrintTheList, but
they still have
// the freedom to supply their own list.
// I do NOT want to use the C++ default argument feature.
static const char* const OneTwo[];

// This is the (non-static) method.
void PrintTheList(const char* const* theList);
};

#endif

// ----- Nick.cpp ---------------------------------------------------
// Nick.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}

#define NICK_EXPORT
#include "Nick.h"
#include <iostream>

const char* const Nick::OneTwo[] =
{
"One", "Two", 0
};

void Nick::PrintTheList(const char* const* theList)
{
const char* iter;
int i = 0;
for (iter = theList[0]; iter; iter = theList[i])
{
std::cout << iter << std::endl;
++i;
}
std::cout << "Finished" << std::endl;
}

// ----- TheConsole.cpp ---------------------------------------------
// TheConsole.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include "..\Nick\Nick.h"

//In the Big Program, the workaround is to declare a local copy of the
pointer.
//static const char* const myOneTwo[] =
// {
// "One", "Two", 0
// };
int _tmain(int argc, _TCHAR* argv[])
{
Nick n;
// Call the method with the "commonly used list"
n.PrintTheList(Nick::OneTwo);

// In the big program, the workaround is to use the local pointer
// n.PrintTheList(myOneTwo);
return 0;
}

// ----- TheConsole.cpp end -----------------------------------------
Nov 17 '05 #1
1 1903
Make sure they are compiled using the same compile option. One thing could
be wrong is struct packing.

This kind of problem could normally be figured out easily by stepping throug
in assembly code.

--
Feng Yuan (www.fengyuan.com)

This posting is provided "AS IS" with no warranties, and confers no rights.
Nov 17 '05 #2

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

Similar topics

4
by: jarmopy | last post by:
Hi, I have made a service with C# and calling that service class from another C# program with remoting. (Referendes from the calling program) That service class is configured so that garpage...
5
by: amit kumar | last post by:
I am calling a function which returns pointer to a map. The declaration of the map is map<int,vectxyz*>. vectxyz is a vector containing pointer to a class xyz. For map<int,vectxyz*>* p1 In the...
47
by: ship | last post by:
Hi We need some advice: We are thinking of upgrading our Access database from Access 2000 to Access 2004. How stable is MS Office 2003? (particularly Access 2003). We are just a small...
2
by: David Rose | last post by:
I have a DLL (not .NET) that takes a function pointer argument and calls that function with an integer argument. The DLL is being called from C#. So far, it is partially working, but the...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
10
by: Ant | last post by:
Hi, I am having trouble with a member function pointer. It sees to give me the followign error Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. ...
16
by: jimjim | last post by:
#include <stdio.h> #include <stdlib.h> //WARNING: The function's signature should be: f( int ** ) void f(int *ip) { static int dummy = 5; *ip = &dummy; } int main(){
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
2
by: =?Utf-8?B?dmxhZGltaXI=?= | last post by:
Hi, i have big subsystem written in old C and published by dll (and lib). Dll functions do: 1) allocate global memory for internal structures 2) controls dll subsystem (communicate by sockets,...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.