DLL & pointers problem

B

Benek

Hello
I writing dll and application in C++, and I have a following problem:

In dll I have function:
void get_list(BYTE *&Buffer)
{
BYTE* Buf = new BYTE[5];
for(int i = 0; i < 5; ++i) {
Buffer = Buf; // Error
}
delete[] Buf;
}

Application use this function (from dll) like in following example:
...
List = new BYTE[5];
get_list(List);
delete[] List;
...

The problem is, that presented code doesn't work correctly.
When I run my application I've got an exception like: 'Access violation at
address 004604E3 in module 'Paluch.exe'. Write of address 00000005'.
Can you give me some hints? I have no idea what's wrong. If presented code
is located in one application (without dll) everything seems to be ok. How
can I resolve it?

Thank you
Pawel
 
J

John Harrison

Benek said:
Hello
I writing dll and application in C++, and I have a following problem:

In dll I have function:
void get_list(BYTE *&Buffer)
{
BYTE* Buf = new BYTE[5];
for(int i = 0; i < 5; ++i) {
Buffer = Buf; // Error
}
delete[] Buf;
}

Application use this function (from dll) like in following example:
...
List = new BYTE[5];
get_list(List);
delete[] List;
...

The problem is, that presented code doesn't work correctly.
When I run my application I've got an exception like: 'Access violation at
address 004604E3 in module 'Paluch.exe'. Write of address 00000005'.
Can you give me some hints? I have no idea what's wrong. If presented code
is located in one application (without dll) everything seems to be ok. How
can I resolve it?


Well I don't know. DLLs are not part of the C++ language, maybe you should
ask on a Windows programming group like
However one thing to try would be
to change this

void get_list(BYTE *&Buffer)

to this

void get_list(BYTE *Buffer)

There is no reason at all to use a reference in the code you quoted.

john
 
S

Sharad Kala

Benek said:
Hello
I writing dll and application in C++, and I have a following problem:

In dll I have function:
void get_list(BYTE *&Buffer)
{
BYTE* Buf = new BYTE[5];
for(int i = 0; i < 5; ++i) {
Buffer = Buf; // Error
}
delete[] Buf;
}

Application use this function (from dll) like in following example:
...
List = new BYTE[5];
get_list(List);
delete[] List;
...


I don't see any apparent problem in the code you have shown. DLLs etc are
offtopic here. But I do remember I had a similar problem some time back and it
had something to do with making it a multithreaded dll...just a wild guess
though. Right place to ask this is some MS newsgroup.

Best wishes,
Sharad
 
B

Bob Hairgrove

Hello
I writing dll and application in C++, and I have a following problem:

In dll I have function:
void get_list(BYTE *&Buffer)
{
BYTE* Buf = new BYTE[5];
for(int i = 0; i < 5; ++i) {
Buffer = Buf; // Error
}
delete[] Buf;
}

Application use this function (from dll) like in following example:
...
List = new BYTE[5];
get_list(List);
delete[] List;
...

The problem is, that presented code doesn't work correctly.
When I run my application I've got an exception like: 'Access violation at
address 004604E3 in module 'Paluch.exe'. Write of address 00000005'.
Can you give me some hints? I have no idea what's wrong. If presented code
is located in one application (without dll) everything seems to be ok. How
can I resolve it?

Thank you
Pawel


Technically, this has nothing to do with the C++ language itself, but
more with memory management / linker issues which are specific to the
MS-Windows platform. Therefore, it is off-topic in this newsgroup.

Usually this kind of problem arises when allocating memory in one
module (i.e., the DLL) and deleting it in another (your EXE).
Otherwise, there would be no need to declare the argument for
get_list() as reference to a pointer.

Are you sure this is the same code that is causing your problem??
 
K

Kapt. Boogschutter

Bob Hairgrove said:
Hello
I writing dll and application in C++, and I have a following problem:

In dll I have function:
void get_list(BYTE *&Buffer)
{
BYTE* Buf = new BYTE[5];
for(int i = 0; i < 5; ++i) {
Buffer = Buf; // Error
}
delete[] Buf;
}

Application use this function (from dll) like in following example:
...
List = new BYTE[5];
get_list(List);
delete[] List;
...

The problem is, that presented code doesn't work correctly.
When I run my application I've got an exception like: 'Access violation at
address 004604E3 in module 'Paluch.exe'. Write of address 00000005'.
Can you give me some hints? I have no idea what's wrong. If presented code
is located in one application (without dll) everything seems to be ok. How
can I resolve it?

Thank you
Pawel


Technically, this has nothing to do with the C++ language itself, but
more with memory management / linker issues which are specific to the
MS-Windows platform. Therefore, it is off-topic in this newsgroup.

Usually this kind of problem arises when allocating memory in one
module (i.e., the DLL) and deleting it in another (your EXE).
Otherwise, there would be no need to declare the argument for
get_list() as reference to a pointer.

Are you sure this is the same code that is causing your problem??

Of course this is off-topic but if you export a function with DLL shouldn't
the first line be a macro???
I'm not sure if this is only when you use MFC or always the case.
 
B

Bob Hairgrove

Bob Hairgrove said:
Hello
I writing dll and application in C++, and I have a following problem:

In dll I have function:
void get_list(BYTE *&Buffer)
{
BYTE* Buf = new BYTE[5];
for(int i = 0; i < 5; ++i) {
Buffer = Buf; // Error
}
delete[] Buf;
}

Application use this function (from dll) like in following example:
...
List = new BYTE[5];
get_list(List);
delete[] List;
...

The problem is, that presented code doesn't work correctly.
When I run my application I've got an exception like: 'Access violation at
address 004604E3 in module 'Paluch.exe'. Write of address 00000005'.
Can you give me some hints? I have no idea what's wrong. If presented code
is located in one application (without dll) everything seems to be ok. How
can I resolve it?

Thank you
Pawel


Technically, this has nothing to do with the C++ language itself, but
more with memory management / linker issues which are specific to the
MS-Windows platform. Therefore, it is off-topic in this newsgroup.

Usually this kind of problem arises when allocating memory in one
module (i.e., the DLL) and deleting it in another (your EXE).
Otherwise, there would be no need to declare the argument for
get_list() as reference to a pointer.

Are you sure this is the same code that is causing your problem??

Of course this is off-topic but if you export a function with DLL shouldn't
the first line be a macro???
I'm not sure if this is only when you use MFC or always the case.


Not necessarily. One usually uses the "extern" keyword (which is not a
macro), or 'extern "C"' if the function is to have C linkage. Perhaps
this is what you were thinking of?
 
K

Kapt. Boogschutter

Bob Hairgrove said:
Bob Hairgrove said:
Hello
I writing dll and application in C++, and I have a following problem:

In dll I have function:
void get_list(BYTE *&Buffer)
{
BYTE* Buf = new BYTE[5];
for(int i = 0; i < 5; ++i) {
Buffer = Buf; // Error
}
delete[] Buf;
}

Application use this function (from dll) like in following example:
...
List = new BYTE[5];
get_list(List);
delete[] List;
...

The problem is, that presented code doesn't work correctly.
When I run my application I've got an exception like: 'Access
violation
at
address 004604E3 in module 'Paluch.exe'. Write of address 00000005'.
Can you give me some hints? I have no idea what's wrong. If presented code
is located in one application (without dll) everything seems to be ok. How
can I resolve it?

Thank you
Pawel


Technically, this has nothing to do with the C++ language itself, but
more with memory management / linker issues which are specific to the
MS-Windows platform. Therefore, it is off-topic in this newsgroup.

Usually this kind of problem arises when allocating memory in one
module (i.e., the DLL) and deleting it in another (your EXE).
Otherwise, there would be no need to declare the argument for
get_list() as reference to a pointer.

Are you sure this is the same code that is causing your problem??
Of course this is off-topic but if you export a function with DLL shouldn't
the first line be a macro???
I'm not sure if this is only when you use MFC or always the case.


Not necessarily. One usually uses the "extern" keyword (which is not a
macro), or 'extern "C"' if the function is to have C linkage. Perhaps
this is what you were thinking of?


That was not what I was thinking of,
I looked it up what I was looking for, its:
AFX_MANAGE_STATE(AfxGetStaticModuleState());
But it's only for MFC Extension DLLs .
 
E

Ernesto

Kapt. Boogschutter said:
Bob Hairgrove said:
Hello
I writing dll and application in C++, and I have a following problem:

In dll I have function:
void get_list(BYTE *&Buffer)
{
BYTE* Buf = new BYTE[5];
for(int i = 0; i < 5; ++i) {
Buffer = Buf; // Error
} delete[] Buf;
}

Application use this function (from dll) like in following example:
...
List = new BYTE[5];
get_list(List);
delete[] List;
...

The problem is, that presented code doesn't work correctly.
When I run my application I've got an exception like: 'Access violation at
address 004604E3 in module 'Paluch.exe'. Write of address 00000005'.
Can you give me some hints? I have no idea what's wrong. If presented code
is located in one application (without dll) everything seems to be ok. How
can I resolve it?

Thank you
Pawel


Technically, this has nothing to do with the C++ language itself, but
more with memory management / linker issues which are specific to the
MS-Windows platform. Therefore, it is off-topic in this newsgroup.

Usually this kind of problem arises when allocating memory in one
module (i.e., the DLL) and deleting it in another (your EXE).
Otherwise, there would be no need to declare the argument for
get_list() as reference to a pointer.

Are you sure this is the same code that is causing your problem??

Of course this is off-topic but if you export a function with DLL shouldn't
the first line be a macro???
I'm not sure if this is only when you use MFC or always the case.


Try to use the same CRT library (C Runtime Library)...
Your application and your DLL should use the Single-threaded DLL or
the Multi-threaded DLL CRT library, not the simple Single-threaded or
Multi-threaded.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top