problem in InterprocessConnection if created it as pointer

Discussion and support for general JUCE issues

problem in InterprocessConnection if created it as pointer

Postby acn » Sat Jul 28, 2012 7:19 am

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.

Code: Select all
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.
Code: Select all
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):
User avatar
acn
JUCE Obsessive
 
Posts: 91
Joined: Thu Sep 15, 2011 8:16 am
Location: India

Re: problem in InterprocessConnection if created it as point

Postby jules » Sat Jul 28, 2012 8:48 am

Code: Select all
connector = nullptr;
delete connector;


Yep. That ain't gonna fly.
User avatar
jules
Fearless Leader
 
Posts: 17216
Joined: Mon Sep 06, 2004 9:03 am
Location: London, UK

Re: problem in InterprocessConnection if created it as point

Postby acn » Sat Jul 28, 2012 9:02 am

Ahhh,,

then what shall I do ? please suggest .... How can I solve the things here ?
User avatar
acn
JUCE Obsessive
 
Posts: 91
Joined: Thu Sep 15, 2011 8:16 am
Location: India

Re: problem in InterprocessConnection if created it as point

Postby jules » Sat Jul 28, 2012 9:18 am

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?
User avatar
jules
Fearless Leader
 
Posts: 17216
Joined: Mon Sep 06, 2004 9:03 am
Location: London, UK

Re: problem in InterprocessConnection if created it as point

Postby acn » Sat Jul 28, 2012 6:54 pm

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 ?
User avatar
acn
JUCE Obsessive
 
Posts: 91
Joined: Thu Sep 15, 2011 8:16 am
Location: India

Re: problem in InterprocessConnection if created it as point

Postby cpr » Sat Jul 28, 2012 8:26 pm

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.
User avatar
cpr
JUCE UberWeenie
 
Posts: 131
Joined: Fri Apr 25, 2008 11:49 pm

Re:problem in InterprocessConnection as pointer[Solved]

Postby acn » Mon Jul 30, 2012 6:01 am

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.
Code: Select all
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;
}
}
}
User avatar
acn
JUCE Obsessive
 
Posts: 91
Joined: Thu Sep 15, 2011 8:16 am
Location: India


Return to General JUCE discussion

Who is online

Users browsing this forum: Bing [Bot], Google Feedfetcher and 1 guest