problem in InterprocessConnection if created it as pointer

7 posts / 0 new
Last post
acn
Offline
Last seen: 2 weeks 3 days ago
Joined: 15 Sep 2011 - 08:16
problem in InterprocessConnection if created it as pointer

Hey Jules.....

I am using InterprocessConnection in following scenario, As before i was using connector as only object but now i need to make it as pointer, and i am encountered with some errors. I hope some one can put some light to the error i am getting here.

class Control : public Compoenent, public ButtonListenr
{
private:
ScopedPointer<Button> connect;
Connector    * connector;
void resized()
{
connect ->setbound
}
void paint(Graphics & g)
{
}
void buttonClicked(Button * buttonThatWasClicked)
{
if(buttonThatWasClicked == connect )
{
if(!connector)
    connector = new Connector(*this);
if(connector)
     if(connector->connecToServer())
          // success do some work
}
}
public:
Control() : connect(nullptr)
{
AddAndMakeVisible(connect = new Button("connect"));
}
~Control ()
{
if(connector)
{
connector->disconnect();
connector = nullptr;
delete connector;
}
}
}

class Connector : public InterprocessConnection
{
private:
Control & control;
public:
connector(Control & control):control(control){}
~connector(){}
void 	connectionMade ();
void 	connectionLost ();
void 	messageReceived (const MemoryBlock &message);
bool connecToServer()
{
bool serverResponse = connectToSocket(control->getServerIPAddress(), control->getPortNumber(), 1000);
            if(serverResponse)
                return true;
            else
                return false;
}
}

Now when connectToServer return successfully true , i do my work. and than it is also getting disconnect() also. Though i check in destructor, and disconnect and delete connector in my Control's distructor. But I get following errors.

f:\dd\vctools\crt_bld\self_x86\crt\src\dbgmalloc.c (56): malloc
    d:\onrepowork\csworking\csplayer\ccsplayer\jucelibrarycode\modules\juce_core\memory\juce_weakreference.h (174): juce::WeakReference<juce::InterprocessConnection,juce::ReferenceCountedObject>::Master::getSharedPointer
    d:\onrepowork\csworking\csplayer\ccsplayer\jucelibrarycode\modules\juce_core\memory\juce_weakreference.h (206): juce::WeakReference<juce::InterprocessConnection,juce::ReferenceCountedObject>::getRef
    d:\onrepowork\csworking\csplayer\ccsplayer\jucelibrarycode\modules\juce_core\memory\juce_weakreference.h (88): juce::WeakReference<juce::InterprocessConnection,juce::ReferenceCountedObject>::WeakReference<juce::InterprocessConnection,juce::ReferenceCountedObject>
 d:\onrepowork\csworking\csplayer\ccsplayer\jucelibrarycode\modules\juce_events\interprocess\juce_interprocessconnection.cpp (193): juce::ConnectionStateMessage::ConnectionStateMessage
 d:\onrepowork\csworking\csplayer\ccsplayer\jucelibrarycode\modules\juce_events\interprocess\juce_interprocessconnection.cpp (218): juce::InterprocessConnection::connectionMadeInt
d:\onrepowork\csworking\csplayer\ccsplayer\jucelibrarycode\modules\juce_events\interprocess\juce_interprocessconnection.cpp (57): juce::InterprocessConnection::connectToSocket
    d:\onrepowork\csworking\csplayer\ccsplayer\source\network\networkconnection.cpp (97):
jules
Online
Last seen: 50 sec ago
Joined: 29 Apr 2013 - 18:37
Re: problem in InterprocessConnection if created it as point

connector = nullptr;
delete connector;

Yep. That ain't gonna fly.

acn
Offline
Last seen: 2 weeks 3 days ago
Joined: 15 Sep 2011 - 08:16
Re: problem in InterprocessConnection if created it as point

Ahhh,,

then what shall I do ? please suggest .... How can I solve the things here ?

jules
Online
Last seen: 50 sec ago
Joined: 29 Apr 2013 - 18:37
Re: problem in InterprocessConnection if created it as point

TBH what you've posted there just looks like a jumble of random bits of pseudo-code.. Surely that's not the actual code you're trying to compile, is it?

acn
Offline
Last seen: 2 weeks 3 days ago
Joined: 15 Sep 2011 - 08:16
Re: problem in InterprocessConnection if created it as point

Yes jules ...

You are right, As you said I have just created pseudo-code from the actual code,
But that is the exact code scenario I have put, One component with button create the member connector pointer and initialize it, and try to connect to server also. And in destructor the problem arise as you might already know it.

Or you can suggest what other way shall i try to use in my actual code that can solve such memory leak and heap corruption problem ?

cpr
cpr's picture
Offline
Last seen: 18 hours 27 min ago
Joined: 25 Apr 2008 - 23:49
Re: problem in InterprocessConnection if created it as point

This is not a JUCE issue, it's seems you don't have a firm grasp on pointers. You should study those two lines of code Jules posted, and understand what is wrong with that. I'm sure many people are willing to help you with it, but I think there is great value in putting the effort into figuring it out on your own.

acn
Offline
Last seen: 2 weeks 3 days ago
Joined: 15 Sep 2011 - 08:16
Re:problem in InterprocessConnection as pointer[Solved]

Hey cpr , you may be right, I am not that great with pointers may be.
But i really tried hard and after that i posted for help.

Anyways ... thanks guys. for making me sure that it was problem just because of pointers i was using.
I have solved the memory leak problem by making an object of connector class, and managing my control other way for connector.

So now my code is like following . and that works well, I hope this is good way i have followed, but still there is a better way to do it, please suggest, I would love to know such things.

class Control : public Compoenent, public ButtonListenr
{
private:
ScopedPointer<Button> connect;
Connector    connector;
void resized()
{
connect .setbound
}
void paint(Graphics & g)
{
}
void buttonClicked(Button * buttonThatWasClicked)
{
if(buttonThatWasClicked == connect )
{
     if(connector.connectToServer())
          // success do some work
}
}
public:
Control() : connect(nullptr)
{
connector.setControl(this);
AddAndMakeVisible(connect = new Button("connect"));
}
~Control ()
{

}
}
}

class Connector : public InterprocessConnection
{
private:
Control * control;
public:
connector(Control & control):control(nullptr)
{
}
~connector(){}
void setControl(Control * control_) 
{
control = control_;
}
void    connectionMade ();
void    connectionLost ();
void    messageReceived (const MemoryBlock &message);
bool connecToServer()
{
if(control)
{
bool serverResponse = connectToSocket(control->getServerIPAddress(), control->getPortNumber(), 1000);
            if(serverResponse)
                return true;
            else
                return false;
}
}
}