Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2005

Tip: Looking for answers? Try searching our database.

what's wrong with this !

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Droopy - 23 Jun 2005 16:17 GMT
I am making a managed C++ wrapper for calling legacy C++ code from C#.
I don't understand what's wrong with following code :

__gc public class SerialBuffer
{
private:
    unsigned char    __nogc*_buffer;
    short            _bufferNr;
    bool            _isUsed;
    int            _length;

public:

    SerialBuffer (short bufferNr);
...
}

__gc public class TrspPortManaged
{
public:
    static const short NbSerialBuffers = 128;
    TrspPortManaged ();
...

private:
    SerialBuffer        *_buffers [];
    UMTrspPort __nogc    *_unmanaged;
...
}

...

TrspPortManaged::TrspPortManaged ()
{
    _unmanaged = new UMTrspPort ();

    _buffers = new SerialBuffer *[NbSerialBuffers];
    SerialBuffer *sb;
    for (int i = 1; i <= NbSerialBuffers; i++)
    {
        sb = new SerialBuffer (i);
        _buffers [i - 1] = sb;
    }
...
}

In TrspPortManaged constructor, the first iteration in for loop seems OK
(sb _buffer var = 0x07253240, keep the same value in _buffers [0])
The second iteration (i = 2), sb _buffer var = 0x07256ee8, buffer [1]->
_buffer = 0x00000000

What am I doing wrong ?

Thanks in advance for your help.

Droopy.
Droopy - 24 Jun 2005 08:36 GMT
Please help me

> I am making a managed C++ wrapper for calling legacy C++ code from C#.
> I don't understand what's wrong with following code :
[quoted text clipped - 52 lines]
>
> Droopy.
Jochen Kalmbach [MVP] - 24 Jun 2005 08:48 GMT
Hi Droopy!
> I am making a managed C++ wrapper for calling legacy C++ code from C#.
> I don't understand what's wrong with following code :

What error do you get?
Can you provide a small, complete repro-code?

> TrspPortManaged::TrspPortManaged ()
> {
[quoted text clipped - 8 lines]
>     }
> }

I can´t see anything bad...

Signature

Greetings
  Jochen

   My blog about Win32 and .NET
   http://blog.kalmbachnet.de/

Droopy - 24 Jun 2005 14:06 GMT
> Hi Droopy!
>> I am making a managed C++ wrapper for calling legacy C++ code from C#.
[quoted text clipped - 4 lines]
>
> I can´t see anything bad...

OK, here it is a sample complete code that illustrate the problem :

1) IprWrapper.h + IprWrapper.cpp in a C++ dll project

// IprWrapper.h

#pragma once

using namespace System;

namespace IprWrapper
{
#pragma once

using namespace System;
using namespace System::Runtime::InteropServices;
__gc public class SerialBuffer
{
private:
    unsigned char    __nogc*_buffer;
    short            _bufferNr;
    bool            _isUsed;
    int                _length;

public:

    SerialBuffer (short bufferNr);

    __property bool get_IsUsed ()
    {
        return _isUsed;
    }

    __property void set_IsUsed (bool value)
    {
        _isUsed = value;
    }

        __property short get_BufferNumber ()
    {
        return _bufferNr;
    }

    __property void set_BufferNumber (short value)
    {
        _bufferNr = value;
    }
};

//----- class TrspPortManaged  -----------

__gc public class TrspPortManaged
{
public:
    static const short BufferSize = 16;
    static const short NbSerialBuffers = 128;

    TrspPortManaged ();

private:
    SerialBuffer        *_buffers [];
};
}

// IprWrapper.cpp

#include "stdafx.h"

#include "IprWrapper.h"

namespace IprWrapper
{

TrspPortManaged::TrspPortManaged ()
{
    _buffers = new SerialBuffer *[NbSerialBuffers];
    SerialBuffer *sb;
    for (int i = 1; i <= NbSerialBuffers; i++)
    {
        sb = new SerialBuffer (i);
        _buffers [i - 1] = sb;
    }
}

// SerialBuffer class

SerialBuffer::SerialBuffer (short bufferNr)
{
    _buffer = new unsigned char [TrspPortManaged::BufferSize];
    IsUsed = false;
    _length = 0;
    BufferNumber = bufferNr;
}
}

2) A simple C# windows application, using the C++ dll (project reference)

private void Form1_Load(object sender, System.EventArgs e)
{
 IprWrapper.TrspPortManaged transport = new IprWrapper.TrspPortManaged ();
}

The problem is that, in the TrspPortManaged constructor, in the for loop,
sb->_buffer is changed  (from 0x07512d40 to 0x00000000) when the following
line is executed :
 _buffers [i - 1] = sb;

Usually, the first iteration works, the second fails.

Thanks in advance for your help.

Droopy.
Willy Denoyette [MVP] - 24 Jun 2005 17:53 GMT
>> Hi Droopy!
>>> I am making a managed C++ wrapper for calling legacy C++ code from C#.
[quoted text clipped - 117 lines]
>
> Droopy.

Works for me, don't know how you are looking at the sb->_buffer pointer but
all of them are correctly set when I run your code.

Willy.
Droopy - 27 Jun 2005 09:25 GMT
>>> Hi Droopy!
>>>> I am making a managed C++ wrapper for calling legacy C++ code from
[quoted text clipped - 124 lines]
>
> Willy.

I just added _buffers in Watch list.
But you are wright, if I make more iterations in the for loop, the _buffer
value is updated with some "delay".
For example, before I execute iteration 10, _buffer = 0x00000000 for all
_buffer in _buffers array with index > 3 (I mean from _buffers [3] to
_buffers [9]).
After the execution of iteration 10, _buffers [3] is updated with a value !
= 0x00000000 !

May be it is the way the Watch window is working.

So I just added a Dump function called just after the TrspPortManaged
constructor.
At that time, all _buffer pointer are set but from item #27 to item #127,
they all have the same value = 0x00936528 !

Thanks for your help.

Droopy.
Droopy - 01 Jul 2005 12:41 GMT
>>>> Hi Droopy!
>>>>> I am making a managed C++ wrapper for calling legacy C++ code from
[quoted text clipped - 144 lines]
>
> Droopy.

I changed some project options and all pointers seem OK now.
I enabled the "Enable Unmanaged Debugging" property from the Windows
application (in Configuration Properties->Debugging).
It reports memory leaks.
There is something I am missing here because I use managed C++.
What should I "delete" or "dispose" ?

Thanks in advance,

Droopy.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.